Android: How Do Bluetooth Uuids Work

Android: How do bluetooth UUIDs work?

In Bluetooth, all objects are identified by UUIDs. These include services, characteristics and many other things. Bluetooth maintains a database of assigned numbers for standard objects, and assigns sub-ranges for vendors (that have paid enough for a reservation). You can view this list here:

https://www.bluetooth.com/specifications/assigned-numbers/

If you are implementing a standard service (e.g. a serial port, keyboard, headset, etc.) then you should use that service's standard UUID - that will allow you to be interoperable with devices that you didn't develop.

If you are implementing a custom service, then you should generate unique UUIDs, in order to make sure incompatible third-party devices don't try to use your service thinking it is something else. The easiest way is to generate random ones and then hard-code the result in your application (and use the same UUIDs in the devices that will connect to your service, of course).

How do Bluetooth SDP and UUIDs work? (specifically for Android)

An UUID identifies a service that is available on a particular device. So if you call BluetoothDevice.fetchUUidsWithSdp() your BroadcastReceiver will receive the relevant Intent ACTION_UUID containing the device and the service UUID.
The bluetooth specification defines some common UUIDs.

If you don't want to connect to one of these well known services but intent to implement your own bluetooth application, then you have to just generate your own UUID (use uuidgen from a unix console or an online generator) that identifies your application/service.
You can create an UUID instance in java like this UUID uuid = UUID.fromString("785da8ea-1220-11e5-9493-1697f925ec7b");.

So if you create the server side for your bluetooth application on Android you typically do this

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket serverSocket = adapter.listenUsingRfcommWithServiceRecord("YourHumanReadableServiceName", uuid);

And this is where you "set" your UUID. The Android bluetooth API creates the SDP-entry consisting of YOUR application's UUID and name for you. Other devices can now retrieve this entry. Androids bluetooth stack will now associate a bluetooth channel to your BluetoothServerSocket. If you want to connect to this ServerSocket, the connecting side usually connects doing this:

// you will most likely already have this instance from a discovery or paired device list
BluetoothDevice serverDevice = adapter.getRemoteDevice(bluetoothMacAddress);
// connect to your ServerSocket using the uuid
BluetoothSocket socket = serverDevice.createRfcommSocketToServiceRecord(uuid);
socket.connect();

Android will again do the heavy lifting for you: It checks the SDP-Records on the remote device, looks up the bluetooth channel that corresponds to your service's UUID and connects using this information.

There is a common code snippet spooking around here on SO that advices you to use "reflection" to get to a hidden API looking similar to this code:

 try {
// this is the way to go
socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect( );
} catch ( IOException exception ) {
// don't do that! You will bypass SDP and things will go sideways.
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
socket.connect();
}

Most people try this and it "just works" in their dev environment but you should know what you do using this. You actively bypass the SDP lookup that retrieves the right bluetooth channel to be used with your service and you will end up connecting to channel 1. If you have more than one Service running on the device, things WILL go sideways in this cases and you will end up in debugging hell ;-)

I developed a small middleware called Blaubot to create small networks using bluetooth/wifi/nfc and experienced all sorts of problems on the devices I used to test with (12 models). It was often the case that the bluetooth stack was not fully functional anymore in cases where it got some load or after many connects/disconnects (which you usually will have, if you are developing your app). In these cases the device.createRfcommSocketToServiceRecord(uuid) would occasionally fail and only turning the bluetooth adapter off and on again helped to bring the bluetooth adapters back to life (in some cases only after a full power cycle). If this happens and you use the reflection method, you will probably not have much fun with bluetooth.

But if you know this and keep concurrent calls to the BluetoothAdapter within bounds, bluetooth connections and the adapters will be pretty stable.

Android bluetooth UUID connecting APP to ANDROID

You can get the UUID from the BluetoothDevice

    mmDevice = device;

// Get a BluetoothSocket to connect with the given BluetoothDevice. This code below show how to do it and handle the case that the UUID from the device is not found and trying a default UUID.

// Default UUID
private UUID DEFAULT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

try {
// Use the UUID of the device that discovered // TODO Maybe need extra device object
if (mmDevice != null)
{
Log.i(TAG, "Device Name: " + mmDevice.getName());
Log.i(TAG, "Device UUID: " + mmDevice.getUuids()[0].getUuid());
tmp = device.createRfcommSocketToServiceRecord(mmDevice.getUuids()[0].getUuid());

}
else Log.d(TAG, "Device is null.");
}
catch (NullPointerException e)
{
Log.d(TAG, " UUID from device is null, Using Default UUID, Device name: " + device.getName());
try {
tmp = device.createRfcommSocketToServiceRecord(DEFAULT_UUID);
} catch (IOException e1) {
e1.printStackTrace();
}
}
catch (IOException e) { }

Android: obtaining uuid of a bluetooth device

You should be more explicit when you make your question. Do you want to connect to a BT device, or you are already connected and you want to use a particular service?

If you want to just connect to the device you need the BT address (like a MAC).

The UUID are related to the services offered by the BT device: http://www.bluecove.org/bluecove/apidocs/javax/bluetooth/UUID.html Here you have a list of UUIDs.

Using a custom UUID when connecting an Android phone and a Raspberry Pi through bluetooth

I have once again answered my own question. I am glad it didn't take me too long this time.

I was running sdptool add SP as part of my bluetooth setup process on my Raspberry Pi, I had seen that command as a recommendation on multiple different threads. It's interesting because the expected UUID does not get overriden, it just waits for either of them on the same RFCOMM channel.

Anyways, avoid using that command if you are trying to establish a bluetooth connection using your own UUID.

How to implement a custom UUID for bluetooth broadcast

Have you looked at the Android API documentation?

You use this https://developer.android.com/reference/android/bluetooth/le/AdvertiseData.Builder#addServiceUuid(android.os.ParcelUuid) function to set a custom service uuid to be advertised.

A general tutorial for advertising can be found here: https://source.android.com/devices/bluetooth/ble_advertising.

Custom UUID for multiple Bluetooth connection in Android

Answer:

I finally found out that you can use a custom UUID using a generator and it works with multiple devices.
The UUID must be unique and should not collide with the ones that are common & public.
Hopefully someone finds it useful



Related Topics



Leave a reply



Submit