Connecting multiple devices to bluetooth in Android - android

So far what I know that its possible to communicate to multiple devices from your android phone. But my case is that I want to connect two 'similar' devices.
My understanding is that, I need to use different UUID if I need to connect 2 devices, the bluetooth chat application suggest to hardcoding the UUID.
Now scenario is that, I m programing for a game, where physical gun will be connected to phone via bluetooth (to track shots etc). Ok here I can ask gun vendor to hard-code a UUID for all guns and phone can connect to 1 gun at a time.. its easy.
The requirement is that, user may have more then 1 gun! Can somebody tell me how to manage this, if I m hardcoding a single UUID for all guns then I m not sure how to connect multiple guns. And if I m not hardcoding the UUID in gun chip, how I can find the UUID to connect?

A UUID is not a unique device identifier; UUIDs are unique identifiers used by the Bluetooth Service Discover Protocol to identify the protocol a Bluetooth device is using. Bluetooth.org has a list of common UUIDs.
Assuming you know or can control the UUID in the guns (it sounds like you can), then you can set up the phone as the Bluetooth server (using the same UUID that the guns use). The guns would then connect as the clients using the same UUID (I know the guns are probably not using Android, but there's some example code there that might be helpful).
Of course, you may need to flip the client/server model around to fit your specific needs. You should be able to make as many of those server and client threads as you need (within the limits of the number of connected devices that your hardware imposes).

Related

Bluetooth BLE Implementation

I'm trying to implement a Bluetooth (BLE) gateway into a project to transfer data between hardware and a mobile device with an application. The hardware is not unique, it could be mass produced with the controller having the same program flashed into it.
If 100 of the hardware product is made, powered up sitting side by side, what is the best way to set up the information that the BLE broadcasts to advertise so that it can be received by a mobile device, and only 1 hardware and mobile device can be connected at any time.
For example, if you have 3 of the same fitness tracking watches side by side, and you only want to connect to 1 of them, how do you distinguish which is which on say the phones bluetooth pairing menu.
Does each of the manufactured hardware need to have a unique ID that shows on the mobile device (eg ABC11, ABC112, ABC113 etc), and how do you program 100 unique programs into the hardware quickly.
On the other hand, should each of the hardware have the same ID broadcast, and then the user just hopes for the best if the correct hardware is paired.
(BTW, the hardware is an arduino based microcontroller using a BM70 BLE module. neither of which are intended to be modified by the user)
Assistance appreciated.
Thanks. Nunya
You do not have to manually program a different MAC addresses for each device, the BM70 module is the one that provides the BLE MAC address and they are supposed to have unique ones. Sometimes manufacturer recycle (reuse) the addresses, but I think the chance to hit a MAC twice with 100 devies is pretty low.
If your devices are all the same and use the same program they all offer and advertise the same BLE service. The service and containing characteristics are the feature that distinguishes your devices from other BLE devices. A true BLE device that does not also use Bluetooth Classic will not show up in your phones bluetooth settings or pairing menu. BLE requires a dedicated app where you scan for BLE devices and idealy filter for the special service you are using to only find your own devices.
To distinguish between 100 devices placed next to each other (if this even matters since they are all the same) you need to either read the MAC address and prit it on a label for each device or use some status LEDs to show the connection status on your arduino. If you connect to one device the LEDs could start blinking to show which device you are connected to.

Is there a way to identify a BLE device in both Android and iOS

I knew how to get the mac address of a BLE device in Android by calling address from BleDevice
bleDevice.address
But I didn't find a way to get the UUID of the device
On the other hand in iOS I can get the UUID of a device by calling identifier from CBPeripheral peripheral.identifier
But I didn't find a way to get the mac address.
Note that the mac address is not advertised in the advertisement data in iOS those are only the fields in advertisement data
kCBAdvDataIsConnectable
kCBAdvDataServiceUUIDs
kCBAdvDataTxPowerLevel
kCBAdvDataRxPrimaryPHY
kCBAdvDataLocalName
kCBAdvDataRxSecondaryPHY
kCBAdvDataTimestamp
My problem is I need an identifier that I can use in both Android and iOS to identify my ble device
In Android, BLE devices are distinguished by their hardware address. CBPeripheral.identifier property seems to be for iOS only and how their peripheral manager identifies and assigns the devices. Probably your implementation should use String as common denominator, for iOS should return identifier and for Android bleDevice.address.
Indeed the iOS MAC-> UUID Issue is a long standing one (and with search you'll see a number of threads on the issue).
Unfortunately there's no simple good news for a solution, and only some potential solutions... here's a summary and hope one of the paths may work for you:
iOS hides the MAC address and randomly generates a UUID for you. You can store the UUID on the phone, but it will be unique to that phone. If we both have iPhones and scan the same peripheral, we'll see different UUIDs. iOS generates the UUID on the device and hides the MAC address.
Indeed the iOS MAC-> UUID Issue is a long standing one (and with search you'll see a number of threads on the issue).
To make life easier, some devices add the some or all of the MAC address in their secondary advertising packet or in a unique device identifier in the Manufacturer data of the device and read this from the advertisement in iOS.... If you're in control of the devices firmware then I'd add this... of course if this is another party's device then you're at the mercy of their implementation.
Another possible route is using the GATT Device Information Service (Service UUID: 0x180a). Under this service there is a Serial Number String (Assigned UUID: 0x2A25) characteristic which can hold a serial number specific to the Bluetooth low energy device. But note, This is optional though and not all BLE profiles will have it, and you have to connect to the device first to retrieve this information which may not fit your usage pattern.
There may be other unique proprietary identifers for a specific device that are exposed either in advertising data or characteristics
If you're talking about an iBeacon then the advertised ibeacon guid and bluetooth address are effectively the same value.
In cases where I'm implementing the firmware for a custom BLE device I always add the 6 byte MAC address to the advertising manufacturer data. Some devices do this (or some other identifier that's not as unique like "Thermometer1020" with the 1020 hopefully being unique). But again you're at the mercy of what the device is advertising.
If you're looking for a solution that works across a wide variety of BLE devices and vendors, then unfortunatly Apple has made life difficult, if not impossible for you in their quest for security, privacy and limiting use cases of connected device applications.

Bluetooth proximity scan while paired

I have been doing a ton of research on this project I want to do, probably read every SO post (bluejacking, RFCOMM, beacons, etc...) and my head is spinning.
I want to create a system at home, using Arduino (with bluetooth le) that will scan bluetooth and detect when my phone or any of my family members phones are in proximity. Whether this is a MAC scanner or simply getting the friendly name. I would also like to get the RSSI. I dont need to do accurate distance measurement but I would like to know approximately how close they are. Does anybody have any suggestions for this approach? I know I can create an app on the phone and have it become a beacon and this may be an option but I am concerned with Android, it would have to be Marshmallow 5.0 at a minimum. I would like support to be a little lower than that, like JB or KK. So, it seems like there should be a simple scan, just looking for the MAC or name. That is all I really need.
That said, the main question I have is, can you scan for and see bluetooth devices in range if they are already paired to something? Like a Bluetooth headset? I understand bluetooth is point to point but does it allow multiple connections or at least broadcast the mac or name when paired?
This question, along with any general suggestions will help me out tremendously!
Thanks!
Bluetooth Discovery from the Listener involves Listening for Bluetooth Inquiry packets(RX) and inquiry response (TX).if done continuously this will deplete the battery. Hence most devices (including Mobile Phones and Headsets) has a policy of when to be discover-able and connectable. Headsets in particular are discoverable only when they are instructed and connectable until one device is connected to it ( There are probably exceptions where the headset has the capability to be connected to multiple phones at the same time). Most Mobile Phones are discoverable only when they are in settings screen ( Android,IOS and Windows Phones), but unlike the headset case they are connectable.
so one option with the MobilePhone is to see if it is possible to connect to them after pairing once ( for example by trying a service search on the remote device and see if it returns the service attributes, this internally makes a connection and indirectly ensures the presence of the device).
if you are specific about a mobile phone which is already paired then you might need to establish a higherlayer connection ( such as SPP) with Authentication and encryption. This would force a two way check between devices to see whether they are already paired. other less secure option is to rely on the Phone (Android and IOS phones doesn't generally Allow a higher layer connection without encryption/pairing) and try to establish an SPP connection to the phone.
Another option is to use Bluetooth-LowEnergy. But Note that security in BT was superior to BLE till BTv4.1 and same on BTv4.2.

Doesn't search any discoverable devices

Can any one describe what is wrong with this code. It didn't search devices, and it showed some paired devices.
Project hosted here in Github.
think of the UUID as one identifier for a particular kind of service your device is advertising or accepting. It could have many such identifiers for many different services it can offer or use. It's not an identifier for the actual device.
The UUID you are quoting is the same one I found here and use too. it seems to be the UUID for generic bluetooth SPP service. (but haven't found much actual proof like an official published document quot8ing this number, just posts on here with no refrence) This UUID will allow your android device to connect to things like bluetooth serial port adapters, or anything expecting a generic serial port device... so it's a good one to start with.
If you plan to create a special service that's not the same as bluetooth SPP or has a different reason to exist, like android chat does, then it's a good idea to create your own UUID. Of course all devices to connect your service will have to understand this same special number.

How to pair and connect to a headset

How can I connect and disconnect to/from a headset bluetooth device programmatically?
The application should work for Android 2.1+
In other words: I have a headeset. I can pair, connect, disconnect to it using Settings/Wireless and Networks/Blutooth Settings.
But how can I do all those things (pair, connect, disconnect) from my program?
Please chech this link.
The android bluetooth example (already listed) has a bunch of issues (not the least of which is you need 2 android devices to get it to function).
Take a look at the example at http://luugiathuy.com/2011/02/android-java-bluetooth/ where he is using bt on the android device to hit a server (to do some robotics work).
Be aware of UUID issues (the way that BT decides what connection it can make is defined in the UUID), and check out http://www.avetana-gmbh.de/avetana-gmbh/produkte/doc/javax/bluetooth/UUID.html
Keep in mind that the 16 bits represented in the UUID (ie, 0x1101 for Serial Port) is misleading in that its really the least significant part and needs to be coded 0x00001101.

Categories

Resources