在Android(蓝牙低功耗)中启用蓝牙特征通知不起作用 [英] Enabling Bluetooth characteristic Notification in Android (Bluetooth Low Energy ) Not Working

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

问题描述

如果我们在字符上调用 setCharacteristicNotification ,是否不会在值更改时发出远程通知?如何在Bluetooth 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(特征,启用)是不够的.

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

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

启用通知后,它将具有一个带有句柄 0x2902 的描述符.因此我们需要将 BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE 写入描述符.首先将 0x2902 转换为128位UUID,就像这样的 00002902-0000-1000-8000-00805f9b34fb (基本蓝牙UUID为0000xxxx-0000-1000-8000-00805f9b34fb ).

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?

}

这篇关于在Android(蓝牙低功耗)中启用蓝牙特征通知不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆