Android BLE onCharacteristicChanged未触发,读取血糖仪数据遇阻
解决BLE血糖仪通知未触发onCharacteristicChanged的问题
问题描述
要读取血糖仪的记录数据,目标Characteristic仅支持设置通知、无法直接读写。调用writeDescriptor返回true,但始终无法触发onCharacteristicChanged回调,相关实现代码如下:
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { if (status == BluetoothGatt.GATT_SUCCESS) { Log.w("BLE", "onServicesDiscovered ") val service = gatt.getService(GLUCOSE_SERVICE) val characteristic = service.getCharacteristic(RECORDS_CHARACTERISTIC) gatt!!.setCharacteristicNotification(characteristic, true) val descriptor = characteristic!!.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG) descriptor.value = BluetoothGattDescriptor.ENABLE_INDICATION_VALUE gatt!!.writeDescriptor(descriptor) } }
解决步骤
- 匹配通知类型与Descriptor值:代码中开启了通知(
setCharacteristicNotification设为true),但Descriptor使用的是ENABLE_INDICATION_VALUE,两者不匹配。因为目标Characteristic仅支持通知,需将Descriptor值改为BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE。 - 验证Descriptor写入结果:
writeDescriptor返回true仅代表操作已发起,不代表写入成功。需实现BluetoothGattCallback的onDescriptorWrite方法,检查回调中的status是否为BluetoothGatt.GATT_SUCCESS,确认Descriptor配置生效。 - 确认Characteristic属性:调用
characteristic.properties检查是否包含BluetoothGattCharacteristic.PROPERTY_NOTIFICATION,确保设备确实支持通知而非指示,避免类型误用。 - 检查onCharacteristicChanged实现:确保
BluetoothGattCallback中正确实现了onCharacteristicChanged方法,方法签名、权限等配置无误,避免因方法未正确重写导致回调不触发。
内容的提问来源于stack exchange,提问作者a2smoda2i
相关产品推荐
相关产品推荐

