Android BLE中心设备的onCharacteristicChanged()方法没有回调
来源:互联网 发布:mac大智慧交易软件 编辑:程序博客网 时间:2024/05/22 16:59
描述:当设备为 Indication 模式时,设备的值有变化时会主动返回给App,App在 onCharacteristicChanged() 方法中能收到返回的值。
Indication: 从机会先向主机发送一条通知,主机接收到通知后去读取从机数据
Notification:从机直接发送给主机数据
问题:在App中通过如下代码注册监听,注册成功后就能接收到设备主动反馈的值了。然而以下代码执行后依旧收不到反馈。但是对设备的读写都是可行的,并且iOS端可以接收到通知。
bluetoothGatt.setCharacteristicNotification(characteristic, true)
解决: 当上面的方法执行返回true后,还要执行如下的代码才能注册成功。
for(BluetoothGattDescriptor dp: characteristic.getDescriptors()){ if (dp != null) { if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) { dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) { dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); } gatt.writeDescriptor(dp); }}
完整的代码如下:
public boolean enableNotification(BluetoothGatt gatt, UUID serviceUUID, UUID characteristicUUID) { boolean success = false; BluetoothGattService service = gatt.getService(serviceUUID); if (service != null) { BluetoothGattCharacteristic characteristic = findNotifyCharacteristic(service, characteristicUUID); if (characteristic != null) { success = gatt.setCharacteristicNotification(characteristic, true); if (success) { // 来源:http://stackoverflow.com/questions/38045294/oncharacteristicchanged-not-called-with-ble for(BluetoothGattDescriptor dp: characteristic.getDescriptors()){ if (dp != null) { if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0) { dp.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); } else if ((characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0) { dp.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); } gatt.writeDescriptor(dp); } } } } } return success;}private BluetoothGattCharacteristic findNotifyCharacteristic(BluetoothGattService service, UUID characteristicUUID) { BluetoothGattCharacteristic characteristic = null; List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); for (BluetoothGattCharacteristic c : characteristics) { if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_NOTIFY) != 0 && characteristicUUID.equals(c.getUuid())) { characteristic = c; break; } } if (characteristic != null) return characteristic; for (BluetoothGattCharacteristic c : characteristics) { if ((c.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0 && characteristicUUID.equals(c.getUuid())) { characteristic = c; break; } } return characteristic;}
0 0
- Android BLE中心设备的onCharacteristicChanged()方法没有回调
- Android BLE 中心和外设通信时 中心设备的onCharacteristicChanged() 没有回调
- Android ble onCharacteristicChanged()无法触发
- Android 蓝牙BLE 中onCharacteristicChanged不走回调的解决方案
- Android ble onCharacteristicChanged()无法触发BluetoothGattCallback
- onCharacteristicChanged 回调问题
- Android BLE 中心设备与外设进行大数据的读写
- Android BLE设备扫描
- 对于ble ios作为中心设备收不到蓝牙外设广播信息的解决办法
- android 蓝牙ble设备开发
- Android BLE虚拟设备端
- android蓝牙--不支持ble设备的旧api
- android蓝牙--不支持ble设备的旧api
- Android开发蓝牙与ble设备的通讯
- Android 蓝牙4.0 Bluetooth BLE 写数据(修改BLE设备的属性)
- BLE的设备各种状态
- Android BLE学习(一): Android搜索BLE设备
- Android BLE学习(一): Android搜索BLE设备
- Hive 11. 关于压缩的配置
- 欢迎使用CSDN-markdown编辑器
- Spring入门篇之单元测试
- typedef char * pStr的问题和const char*, char const*, char*const的区别
- Hibernate
- Android BLE中心设备的onCharacteristicChanged()方法没有回调
- 【Derivation】采样定理证明
- 兼容IE8图片下载
- iOS -- 应用程序启动时调用方法顺序分析
- 用Intellijidea建立简单javaWeb以及Servlet的步骤
- 工作经验积累
- editview password字体与其他字体不同
- 加载jquery不用愁,所有版本都在这了
- BLE 4.0 CC2540 协议栈架构