RxAndroidBLE:启用特征通知,而无需发现服务

时间:2019-12-26 13:21:28

标签: android bluetooth-lowenergy rxandroidble

我最近才一直在使用RxAndroidBLE 2,并且正在寻找一种解决方案,可以在无需调用discoverServices()的情况下启用特征通知。 就我而言,该呼叫需要花费很多时间(5-10秒)。确保该特征存在。 我在Internet上找到了几种解决方案。但是,在每种情况下都将隐式调用discoverServices()。到目前为止,我的实现看起来像...

private void onConnectionReceived(RxBleConnection rxBleConnection) {

        rxBleConnection.discoverServices()
                .flatMap(rxBleDeviceServices -> {
                    return rxBleDeviceServices.getCharacteristic(MY_UUID_RX);
                })
                .flatMapObservable(bluetoothGattCharacteristic -> {
                    BluetoothGattDescriptor cccDescriptor = bluetoothGattCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID);
                    Completable enableNotificationCompletable = rxBleConnection.writeDescriptor(cccDescriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                    Completable disableNotificationCompletable = rxBleConnection.writeDescriptor(cccDescriptor, BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE).onErrorComplete();
                    return rxBleConnection.setupNotification(bluetoothGattCharacteristic, NotificationSetupMode.COMPAT)
                            .doOnNext(notificationObservable -> notificationHasBeenSetUp())
                            .flatMap(notificationObservable -> notificationObservable)
                            .mergeWith(enableNotificationCompletable)
                            .doOnDispose(disableNotificationCompletable::subscribe); // fire and forget
                })
                .observeOn(AndroidSchedulers.from(handlerThread.getLooper()))
                .subscribe(this::onNotificationReceived, this::onNotificationSetupFailure);
    }

感谢您的支持!

1 个答案:

答案 0 :(得分:0)

当设备连接到新的外围设备时,它需要执行服务发现过程以获取属性(例如特性)句柄。

即使确保具有给定UUID的特征也存在,设备仍需要获取其句柄以执行低级BLE操作。取决于您外围设备的配置,Android OS可能会缓存发现的属性句柄,以在后续连接中重用。

在执行服务发现过程时,Android OS始终使用完整的发现-遍历所有服务/特征/描述符-如果要发现的属性更多,则花费的时间更长。通过减少外围设备上的属性数量,可以减少执行该过程所需的时间。

(另一方面,iOS仅允许发现特定/最小属性子集以加快处理过程

我希望这能回答您的问题。