我正在使用这个图书馆 NordicSemiconductor/Android-BLE-Library用于BLE通信并成功连接外围设备。我正在获取服务和特征,并能够编写特征。
但是由于某种原因,在写入特征后,不会调用 onCharacteristicChanged 。尽管我先启用通知,然后再启用通知。
这就是我通过新版本的库启用它的方式。
setNotificationCallback(bluetoothGattCharacteristic).with(object : ProfileDataCallback {
override fun onDataReceived(device: BluetoothDevice, data: Data) {
BluetoothNotificationBus.getBus().post(BluetoothNotificationEvents.PostPeripheralValueForCharacteristicChangedNotification(device,bluetoothGattCharacteristic.service,bluetoothGattCharacteristic))
}
override fun onInvalidDataReceived(device: BluetoothDevice, data: Data) {
BluetoothNotificationBus.getBus().post(BluetoothNotificationEvents.PostPeripheralValueForCharacteristicChangedNotification(device,bluetoothGattCharacteristic.service,bluetoothGattCharacteristic))
}
})
enableNotifications(bluetoothGattCharacteristic).enqueue()
也在logcat中:
2019-05-20 14:03:24.110 6148-6148/com.nextmunich.trumaandroidpoc D/BluetoothGatt: setCharacteristicNotification() - uuid: f47b0100-f3b2-11e8-8eb2-f2801f1b9fd1 enable: true
也来自 BleManager.java ,下面的方法返回 true
@MainThread
private boolean internalEnableNotifications(final BluetoothGattCharacteristic characteristic) {
final BluetoothGatt gatt = mBluetoothGatt;
if (gatt == null || characteristic == null || !mConnected)
return false;
final BluetoothGattDescriptor descriptor = getCccd(characteristic, BluetoothGattCharacteristic.PROPERTY_NOTIFY);
if (descriptor != null) {
log(Log.DEBUG, "gatt.setCharacteristicNotification(" + characteristic.getUuid() + ", true)");
gatt.setCharacteristicNotification(characteristic, true);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
log(Log.VERBOSE, "Enabling notifications for " + characteristic.getUuid());
log(Log.DEBUG, "gatt.writeDescriptor(" + CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR_UUID + ", value=0x01-00)");
return internalWriteDescriptorWorkaround(descriptor);
}
return false;
}
请指导以解决此问题。据我了解,stackoverflow上有不同的线程,但是它们都不适合我。
我还使用nRF Connect App测试了我的代码,看来通知正常。