How does Bluetooth SDP and UUID work? (specifically for Android)

I understand that SDP is a list of UUIDs that other devices can get.

According to this PDF from MIT, "The more general way of thinking SDP is database information." Does this mean that I can add multiple values ​​to the SDP? Since Android has BluetoothDevice.fetchUuidsWithSdp()

, how do I set the UUID of the device?

Also, what does each UUID section mean? The UUIDs look like 00000000-0000-1000-8000-00805F9B34FB

, but what information does this message convey?

+3


source to share


1 answer


The UUID identifies the service available on a specific device. So if you call BluetoothDevice.fetchUUidsWithSdp()

, your BroadcastReceiver will get the corresponding Intent ACTION_UUID containing the device and service UUID. The bluetooth specification defines some common UUIDs .

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

.

So if you are building a server side of your bluetooth app on Android you usually 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 an SDP record consisting of YOUR app UUID and a name for you. Other devices can now receive this entry. The android bluetooth stack will now associate the bluetooth channel with your BluetoothServerSocket. If you want to connect to this server, the connecting side usually connects to 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 do the heavy lifting for you again: It checks the SDP entries on the remote device, looks at the Bluetooth channel that matches your service UUID, and connects using that information.

There is a generic piece of code in SO that advises you to use "reflection" to get a hidden API 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 it and it "just works" in its environment, but you should know what you are doing using it. You are actively bypassing the SDP lookup which extracts the right bluetooth channel to be used with your service and you will eventually connect to channel 1. If multiple services are installed on the device, things will go sideways in this case and you will end up in the debugging of 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 for testing (12 models). It often happened that the bluetooth stack was no longer fully functional in cases where it received some load or after many connections / disconnections (which you will usually have if you are developing an application). In these cases it device.createRfcommSocketToServiceRecord(uuid)

is interrupted from time to time and turning the bluetooth adapter on and off again helps to revive the bluetooth adapters (in some cases only after a full power cycle). If this happens and you are using the reflection method, you probably won't like Bluetooth very much.

But if you know this and support simultaneous calls to the BluetoothAdapter within the boundaries, the Bluetooth and adapter connections will be pretty stable.

+7


source







All Articles