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

前端 未结 1 1164
迷失自我
迷失自我 2021-01-03 00:00

My understanding is that the SDP is a list of UUIDs that other devices can fetch.

According to this PDF from MIT, \"A more general way to think of SDP is as an info

相关标签:
1条回答
  • 2021-01-03 00:53

    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.

    0 讨论(0)
提交回复
热议问题