我最近才开始使用RxAndroidBLE 2,我正在寻找一种可以在不调用discoverServices()的情况下启用特征通知的解决方案。在我的例子中,调用花费了很多时间(5-10秒)。确保该特征存在。我在网上找到了几个解决方案。但是,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);
}
感谢您的支持!
发布于 2020-01-08 12:51:09
当设备连接到新的外围设备时,它需要执行服务发现过程以获得属性(例如,特征)句柄。
即使确保存在具有给定UUID的特征,设备也需要获取其句柄来执行低级BLE操作。根据您外围设备的配置,Android操作系统可能会缓存已发现的属性句柄,以便在后续连接中重用。
当执行服务发现过程时,Android操作系统总是使用完整的发现-它迭代所有的服务/特征/描述符-如果有更多的属性需要发现,这需要更长的时间。通过减少外围设备上的属性数量,可以减少执行该过程所需的时间。
(另一方面,iOS只允许发现特定/最小的属性子集,以加快过程)
我希望这能回答你的问题。
https://stackoverflow.com/questions/59489096
复制相似问题