首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RxAndroidBLE :在没有服务发现的情况下启用特征通知

RxAndroidBLE :在没有服务发现的情况下启用特征通知
EN

Stack Overflow用户
提问于 2019-12-26 21:21:28
回答 1查看 201关注 0票数 1

我最近才开始使用RxAndroidBLE 2,我正在寻找一种可以在不调用discoverServices()的情况下启用特征通知的解决方案。在我的例子中,调用花费了很多时间(5-10秒)。确保该特征存在。我在网上找到了几个解决方案。但是,discoverServices()在每种情况下都是隐式调用的。到目前为止,我的实现看起来像...

代码语言:javascript
运行
复制
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);
    }

感谢您的支持!

EN

回答 1

Stack Overflow用户

发布于 2020-01-08 20:51:09

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

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

当执行服务发现过程时,Android操作系统总是使用完整的发现-它迭代所有的服务/特征/描述符-如果有更多的属性需要发现,这需要更长的时间。通过减少外围设备上的属性数量,可以减少执行该过程所需的时间。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59489096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档