首页 > 解决方案 > 未调用 BLE Gatt onCharacteristicChanged 方法

问题描述

我们正面临 BLE Gatt onCharacteristicChanged 方法的问题。不调用此方法。

我们使用以下代码启用了通知:

mBluetoothGatt!!.setCharacteristicNotification("NOTIFY_SERVICE_CHARACTERISTIC", true)

val descriptor = NOTIFY_SERVICE_CHARACTERISTIC.getDescriptor(convertFromInteger(0x2902))
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE
var status = mBluetoothGatt!!.writeDescriptor(descriptor)

在此之后,我们从 onDescriptorWrite 方法向 BLE 发送代码。

{WRITE_SERVICE_CHARACTERISTIC}.setValue({BYTE_ARRAY}) 
val status = mBluetoothGatt!!.writeCharacteristic(RxChar)

将代码发送到 BLE 后。它将在 onCharacteristicWrite 方法中响应。但是我们没有通过通知服务在 onCharacteristicChanged 方法中得到任何响应。

相同的命令在其他 BLE 扫描仪应用程序中工作,但我的应用程序面临问题。

请帮助我们解决问题。

谢谢

标签: androidbluetooth-lowenergy

解决方案


您正在使用指示设置覆盖描述符的通知设置:

descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE

您可能只需要通知并且可以取消启用指示:

mBluetoothGatt!!.setCharacteristicNotification("NOTIFY_SERVICE_CHARACTERISTIC", true)

val descriptor = NOTIFY_SERVICE_CHARACTERISTIC.getDescriptor(convertFromInteger(0x2902))
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
var status = mBluetoothGatt!!.writeDescriptor(descriptor)

如果您确实需要通知和指示,则必须设置如下值:

descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE | BluetoothGattDescriptor.ENABLE_INDICATION_VALUE

|运算符执行按位或运算,因此将两个标志组合为一个


推荐阅读