如果我们在一个字符上调用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?

}

标签:android,bluetooth,bluetooth-lowenergy,gatt

来源: https://codeday.me/bug/20190713/1453637.html

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐