首页 > 解决方案 > 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”字节

标签: androidbluetooth-lowenergyrxandroidble

解决方案


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

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

自库版本以来1.8.0,可以使用NotificationSetupMode.QUICK_SETUPwhichObservable<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 外设引入


推荐阅读