Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling Bluetooth characteristic Notification in Android (Bluetooth Low Energy ) Not Working

If we call setCharacteristicNotification on a character, not giving Remote Notification on value Change? How to enable the remote Notification on a Central Device in Bluetooth LE ?

like image 594
sreekumar Avatar asked Nov 29 '22 10:11

sreekumar


1 Answers

TO enable Remote Notification on Android,

setCharacteristicNotification(characteristic, enable) is not enough.

Need to write the descriptor for the characteristic. Peripheral has to enable characteristic notification while creating the characteristic.

Once the Notify is enabled , it will have a descriptor with handle 0x2902 . so we need to write BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE to the descriptor. First Convert 0x2902 to 128 bit UUID, it will be like this 00002902-0000-1000-8000-00805f9b34fb (Base Bluetooth UUID is 0000xxxx-0000-1000-8000-00805f9b34fb).

Code below

 protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");


 * Enable Notification for characteristic
 *
 * @param bluetoothGatt
 * @param characteristic
 * @param enable
 * @return
 */
public boolean setCharacteristicNotification(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic characteristic,boolean enable) {
    Logger.d("setCharacteristicNotification");
    bluetoothGatt.setCharacteristicNotification(characteristic, enable);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID);
    descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00});
    return bluetoothGatt.writeDescriptor(descriptor); //descriptor write operation successfully started?

}
like image 199
sreekumar Avatar answered Dec 15 '22 21:12

sreekumar