【问题标题】:Polidea RxAndroidBle Enable notificationPolidea RxAndroidBle 启用通知
【发布时间】:2018-08-13 14:24:08
【问题描述】:

我非常需要这个来在我的应用程序中进一步进行。

我非常熟悉 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);
}

现在,我正在使用 Polidea 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”字节

【问题讨论】:

    标签: android bluetooth-lowenergy rxandroidble


    【解决方案1】:

    这可能是因为您的外围设备在设置Client Characteristic Configuration Descriptor 后立即发送通知。

    默认情况下,RxAndroidBle 在发出Observable<byte[]> 之前完全设置通知,即必须在发出之前成功写入 CCC 描述符。

    由于库版本1.8.0,可以使用NotificationSetupMode.QUICK_SETUP,它会尽快发出Observable<byte[]>——在编写CCC描述符之前允许捕获此类早期通知/指示。

    mRxBleConnection.setupNotification(bluetoothGattCharacteristic, NotificationSetupMode.QUICK_SETUP)
    

    Pre 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 外设引入

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多