android - Polidea RxAndroidBle 启用通知
问题描述
我非常需要这个在我的申请中进一步进行。
我非常熟悉 Android BLE 并使用多年。
我有下面的代码来启用通知,它与我的外围设备一起工作了多年。onCharacteristicChanged()
启用通知时使用“OK_N1”调用方法。
private void enableNotification(String serviceUUID, String characteristicUUID) {
if (bluetoothGatt == null) {
return;
}
BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(serviceUUID));
if (service == null) {
return;
}
BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(characteristicUUID));
bluetoothGatt.setCharacteristicNotification(characteristic, true);
enableDescriptor(characteristic);
}
private void enableDescriptor(BluetoothGattCharacteristic bluetoothGattCharacteristic) {
if (bluetoothGatt == null) {
return;
}
BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(
UUID.fromString(PodsServiceCharacteristics.CLIENT_CHARACTERISTIC_CONFIG));
if (descriptor == null)
return;
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
bluetoothGatt.writeDescriptor(descriptor);
}
现在,我正在使用Plidea RxAndroidble (ver 1.7.0) 和 RxJava2 来简化事情。
我有以下代码与 Polidea 的 RxAndroidBle 不工作。
public void enableNotifications(@NotNull String[] characteristics) {
if (isConnected()) {
mNotificationSubscriber = mRxBleConnection.setupNotification(UUID.fromString(characteristics[0]))
.doOnNext(notificationObservable -> notificationHasBeenSetUp())
.flatMap(notificationObservable -> notificationObservable)
.subscribe(this::onNotificationReceived, this::onNotificationSetupFailure);
}
}
private void onNotificationReceived(byte[] bytes) {
Log.i(TAG, "onNotificationReceived");
}
private void onNotificationSetupFailure(Throwable throwable) {
Log.i(TAG, "onNotificationSetupFailure" + throwable.getMessage());
}
private void notificationHasBeenSetUp() {
Log.i(TAG, "notificationHasBeenSetUp");
}
notificationHasBeenSetUp()
被调用但onNotificationReceived()
未被调用,我得到“OK_N1”字节
解决方案
这可能是因为您的外围设备在设置后立即发送通知Client Characteristic Configuration Descriptor
。
默认情况下RxAndroidBle
,在发射之前完全设置通知,Observable<byte[]>
即必须在发射之前成功写入 CCC 描述符。
自库版本以来1.8.0
,可以使用NotificationSetupMode.QUICK_SETUP
whichObservable<byte[]>
尽快发出 - 在写入 CCC 描述符之前,允许捕获此类早期通知/指示。
mRxBleConnection.setupNotification(bluetoothGattCharacteristic, NotificationSetupMode.QUICK_SETUP)
预1.8.0
版本回答
为了不错过任何“早期”排放,可以利用NotificationSetupMode.COMPAT
库不处理写入 CCC 描述符*的情况。
(...)
mNotificationSubscriber = mRxBleConnection.discoverServices()
.flatMap(rxBleDeviceServices -> rxBleDeviceServices.getCharacteristic(characteristicUuid))
.flatMapObservable(bluetoothGattCharacteristic -> {
BluetoothGattDescriptor cccDescriptor = bluetoothGattCharacteristic.getDescriptor(PodsServiceCharacteristics.CLIENT_CHARACTERISTIC_CONFIG);
Completable enableNotificationCompletable = mRxBleConnection.writeDescriptor(cccDescriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
Completable disableNotificationCompletable = mRxBleConnection.writeDescriptor(cccDescriptor, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE).onErrorComplete();
return mRxBleConnection.setupNotification(bluetoothGattCharacteristic, NotificationSetupMode.COMPAT)
.doOnNext(notificationObservable -> notificationHasBeenSetUp())
.flatMap(notificationObservable -> notificationObservable)
.mergeWith(enableNotificationCompletable)
.doOnDispose(disableNotificationCompletable::subscribe) // fire and forget
.share(); // this can be omitted but I put it here in case the resulting `Observable<byte[]>` would be shared among multiple subscribers
})
.subscribe(this::onNotificationReceived, this::onNotificationSetupFailure);
(...)
我认为由于这显然是很常见的情况,图书馆会在某个时候处理它。
*NotificationSetupMode.COMPAT
主要针对不遵循 BLE 规范且没有 CCC 描述符但会一直发送通知的 BLE 外设引入
推荐阅读
- python - 如何在 pysimplegui 中调整列表框的大小?
- pandas - 具有自定义聚合函数和 dtype 条件的 pandas agg:列静默消失
- php - html表单不显示值
- python - pandas plot with twiny top xticks 锚调整
- python - 在 matplotlib 中的图形 2D 线上绘制 2D 线
- javascript - 在 Web iframe 窗口中访问 TS 命名空间
- c# - 使愤怒的小鸟在统一上看起来很像。发现了一个错误,仍然无法修复它
- spring - 无法从配置服务器获取属性值
- python - 特定字头 qtableWidget 中的富文本
- python - Python 3 - 从字典对绘制 mapplotlib 图