如果我们在一个字符上调用setCharacteristicNotification,而不是在值Change上给出远程通知?如何在蓝牙LE中的*设备上启用远程通知?
解决方法:
要在Android上启用远程通知,
setCharacteristicNotification(特性,启用)是不够的.
需要为特征写出描述符.外围设备必须在创建特征时启用特征通知.
启用Notify后,它将具有句柄0x2902的描述符.所以我们需要将BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE写入描述符.首先将0x2902转换为128位UUID,它将如此00002902-0000-1000-8000-00805f9b34fb(基本蓝牙UUID为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?
}