【问题标题】:write to BLE char_write_req写入 BLE char_write_req
【发布时间】:2018-02-08 16:06:17
【问题描述】:

我想更改通知,所以我这样做了:

BluetoothGattCharacteristic init_gatt=mConnectedGatt.getService(SERVICE_UUID).getCharacteristic(CHAR_2_UUID);
                mConnectedGatt.setCharacteristicNotification(init_gatt,true);
                BluetoothGattDescriptor descriptor = init_gatt.getDescriptor(
                        UUID.fromString("00002902-0000-1000-8000-00805F9B34FB"));
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                boolean success=mConnectedGatt.writeDescriptor(descriptor);
                init_gatt.setValue(new byte[]{0x19});
                mConnectedGatt.writeCharacteristic(init_gatt);

所以成功是真的,但永远不会调用 onCharacteristicChanged()。 min 的一个同事在一个馅饼上完成了 在这里,我得到了一个 gatttool 写入请求的示例。

gatttool -b <MAC Address> --char-write-req --handle=0x0031 --value=0100 --listen android

其中参数为char-write-req 0x16 01:00

那么有没有办法在android中做同样的事情?

【问题讨论】:

  • 您显示的代码注册了通知的特征,并且由于您在 write 上收到了成功,因此您现在可以接收通知了。 onCharacteristicChanged 将在收到有关该特征的通知时被调用,但您连接的设备会根据自己的条件发送该通知。
  • 如果您也可以写入特征(它具有该属性),那么您也应该在writeCharacteristic 调用中收到真或假结果。

标签: android notifications bluetooth-lowenergy descriptor


【解决方案1】:

问题在于 Android 不处理句柄。我痴迷于尝试获取句柄,但我通过启用通知解决了问题。我构建了一个状态机,在其中逐步启用了每个描述符通知,它现在可以工作了。所以我的代码通常是正确的,但是当我得到一个成功的启用标志时,我不得不重复它。

This video 很好地解释了你必须如何处理它。

【讨论】:

    猜你喜欢
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2018-11-18
    • 2014-08-08
    • 2018-06-13
    • 2018-02-09
    • 1970-01-01
    相关资源
    最近更新 更多