本文介绍了在 Android 中启用蓝牙特性通知(低功耗蓝牙)不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们对一个角色调用 setCharacteristicNotification,而不是远程通知值变化?如何在蓝牙 LE 的中央设备上启用远程通知?

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 ?

推荐答案

要在 Android 上启用远程通知,

TO enable Remote Notification on Android,

setCharacteristicNotification(characteristic, enable) 是不够的.

需要为特征写描述符.外设必须在创建特征时启用特征通知.

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

一旦 Notify 被启用,它将有一个带有句柄 0x2902 的描述符.所以我们需要将 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 写入描述符.首先将 0x2902 转换为 128 位 UUID,它会是这样的 00002902-0000-1000-8000-00805f9b34fb(基本蓝牙 UUID 为 0000xxxx-0000-1005fb0804).

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).

下面的代码

 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 中启用蓝牙特性通知(低功耗蓝牙)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:49