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

后端 未结 3 592
既然无缘
既然无缘 2020-12-19 17:47

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

3条回答
  •  被撕碎了的回忆
    2020-12-19 18:11

    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?
    
    }
    

提交回复
热议问题