Android 4.3: How to connect to multiple Bluetooth Low Energy devices

I suspect everyone adding delays is just allowing the BLE system to complete the action you have asked before you submit another one. Android’s BLE system has no form of queueing. If you do

BluetoothGatt g;
g.writeDescriptor(a);
g.writeDescriptor(b);

then the first write operation will immediately be overwritten with the second one. Yes it’s really stupid and the documentation should probably actually mention this.

If you insert a wait, it allows the first operation to complete before doing the second. That is a huge ugly hack though. A better solution is to implement your own queue (like Google should have). Fortunately Nordic have released one for us.

https://github.com/NordicSemiconductor/puck-central-android/tree/master/PuckCentral/app/src/main/java/no/nordicsemi/puckcentral/bluetooth/gatt

Edit: By the way this is the universal behaviour for BLE APIs. WebBluetooth behaves the same way (but Javascript does make it easier to use), and I believe iOS’s BLE API also behaves the same.

Leave a Comment