gpt4 book ai didi

android - BxAndroidBle 无法读取设备,status=22

转载 作者:行者123 更新时间:2023-12-05 00:06:54 25 4
gpt4 key购买 nike

我在读取 Ble 设备和使用 RxAndroidBle 库时遇到问题。

我一直收到这个错误:

BleGattException{status=22, bleGattOperation=BleGattOperation{description='CONNECTION_STATE'}}

谁能看看我的代码,看看我可能做错了什么:

subscription = rxBleDevice.establishConnection(context, true)
.subscribe(rxBleConnection -> {
rxBleConnection.readCharacteristic(UUID.fromString(UUID_LOG_COUNT)).doOnNext(Action1 -> Logger.d(Helper_Utils.reverseHex(HexString.bytesToHex(Action1))));
}, throwable -> {
Logger.d("Error", throwable.getMessage());
});

如果您需要更多信息,我会尽力提供。

编辑

我用过 2 款不同的手机:一加二安卓 6.0.1Moto G Play Android 6.0.1

我试过多次打开和关闭 wifi 和蓝牙。我从来没有读过这个例子。

最佳答案

status = 22 是与 Android 操作系统断开外围设备连接相关的问题。您无法通过代码来阻止它发生。

至于读不到特征值——是因为你没有订阅它。 RxJava 编程(或一般的响应式编程)中的最佳方法是准备一个只有一个订阅的流,因为这样可以最大限度地减少状态量。

你可以这样做:

Subscription s = rxBleDevice.establishConnection(true) // establish the connection
.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString(UUID_LOG_COUNT))) // when the connection is established start reading the characteristic
.take(1) // after the first value unsubscribe from the upstream to close the connection
.subscribe( // subscribe to read values
characteristicValue -> Logger.d(Helper_Utils.reverseHex(HexString.bytesToHex(characteristicValue))), // do your thing with the read value here
throwable -> Logger.d("Error", throwable.getMessage()) // log / show possible error here
);

请记住,.subscribe() 的结果是一个 Subscription,您可以通过调用 Subscription.unsubscribe() 取消它,这将断开连接外围设备。

我的代码引用了昨天发布的 RxAndroidBle 1.2.0 引入的新 API。

关于android - BxAndroidBle 无法读取设备,status=22,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43093579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com