我有一个BLE外围设备,可以在iOS和Android 7.0上使用给定的服务/特性UUID正常工作。在Android 6.0 Marshmallow上,尽管在描述符上设置了ENABLE_NOTIFICATION_VALUE,但onCharacteristicChanged方法不会触发。请帮我弄清楚如何让运行Android OS 6.0棉花糖的Android设备获取onCharacteristicChanged。下面是我使用的尝试获得通知工作代码:Android BLE:成功将ENABLE_NOTIFICATION_VALUE值写入BluetoothGattDescriptor,但onCharacteristicChanged从不触发
boolean success = mBluetoothGatt.setCharacteristicNotification(characteristic, true);
Log.e(TAG, "set char notification = " + (success ? "It worked :)" : "It did not work :("));
if (UUID_DATA_PACKET.equals(characteristic.getUuid())) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(EkoCoreGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
success = mBluetoothGatt.writeDescriptor(descriptor);
Log.e(TAG, "set descriptor = " + descriptor.getCharacteristic().getWriteType() + ", success = " + (success ? "It worked :)" : "It did not work :("));
}
在上述两个setCharacteristicNotification和writeDescriptor调用的代码返回成功(1 /真/等)。此外,onDescriptorWrite回调函数返回GATT_SUCCESS。但是,当我们在代码的稍后时间读取描述符时,发现通知值仍被设置为禁用。我们尝试了很多解决方案,比如在setCharacteristicNotification和writeDescriptor调用之间加入延迟,但还没有找到解决这个问题的方法。与外围设备配对是没有问题的,但收到通知似乎不可能。任何提示将不胜感激。