androidbluetoothnotificationscharacteristics

Bluetooth LE listen to multiple characteristic notifications


I'm using BLE application on an Android phone communicating with a custom BLE sensor board. There are two characteristics provided by the board, acceleration and ecg. On the phone side, I'd like to receive the notifications of two characteristics from the sensor board. My code to set notifications:

mGatt.setCharacteristicNotification(ecgChar, true);
            BluetoothGattDescriptor descriptor = ecgChar.getDescriptor(
                    UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mGatt.writeDescriptor(descriptor);
            mGatt.setCharacteristicNotification(accelChar, true);
            descriptor = ecgChar.getDescriptor(
                    UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mGatt.writeDescriptor(descriptor);

However, I can only receive notifications for the first characteristics. When I only register notification for one characteristic, it worked well. The sampling frequency is 100Hz for both ECG and acceleration. So how can I receive notifications from both characteristics? Thanks.


Solution

  • You can only have one outstanding gatt operation at a time. In this case you do two writeDescriptor calls before waiting until the first has completed. You must wait for https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html#onDescriptorWrite(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattDescriptor, int) until you can send the next one.