android - 未调用 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 扫描仪应用程序中工作,但我的应用程序面临问题。
请帮助我们解决问题。
谢谢
解决方案
您正在使用指示设置覆盖描述符的通知设置:
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
该|
运算符执行按位或运算,因此将两个标志组合为一个
推荐阅读
- wordpress - Woocommerce:当有人在购物车中添加东西时更改“添加到购物车”按钮颜色
- python - 使用 ftplib 和 boto3 到 AWS S3 的 FTPS 错误:Fileobj 必须实现读取
- c++ - 如何比较 C++ 中的类型?
- eclipse-mat - Eclipse Memory Analyzer 识别 JDBC 泄漏
- powershell - 遇到下一条记录时,使用 powershell 脚本读取上一条记录中的所有行
- typescript - 排他或打字稿中的类型
- oracle-data-integrator - 在 Odi(oracle 数据集成器)中,使用 odisqlunload 导出带引号的列
- python - 用 pip3 安装了 j2cli 和 jinja2 但找不到如何运行 j2cli
- excel - 单击网页按钮功能在excel宏中不起作用
- random - 散列质量在位上不是恒定的