rx-java2 - 在 RxAndroidBle 流中抛出 UndeliverableException
问题描述
我有一个行为不端的 BLE 设备(温度传感器),status 8 (GATT_INSUF_AUTHORIZATION or GATT_CONN_TIMEOUT)
每次我尝试连接到设备时都会抛出异常。我不担心这个异常,因为设备有故障。
但是,我不断收到通知,在使用 RxAndroidBle(1.9.1) 时,rxjava2 没有正确处理错误;看这里;
这是我的代码。
rxBleClient
.getBleDevice(macAddress)
.establishConnection(false)
.flatMapSingle { it.readRssi() }
.subscribe({ "test1:Success" }, { "test1:error" })
和错误
I/RxBle#GattCallback: MAC='E9:CF:8A:D0:01:19' onConnectionStateChange(), status=8, value=0
D/RxBle#ClientOperationQueue: FINISHED ConnectOperation(147547253) in 10257 ms
D/RxBle#ConnectionOperationQueue: Connection operations queue to be terminated (MAC='E9:CF:8A:D0:01:19')
com.polidea.rxandroidble2.exceptions.BleDisconnectedException: Disconnected from MAC='E9:CF:8A:D0:01:19' with status 8 (GATT_INSUF_AUTHORIZATION or GATT_CONN_TIMEOUT)
at com.polidea.rxandroidble2.internal.connection.RxBleGattCallback$2.onConnectionStateChange(RxBleGattCallback.java:77)
at android.bluetooth.BluetoothGatt$1$4.run(BluetoothGatt.java:249)
at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:725)
at android.bluetooth.BluetoothGatt.-wrap0(Unknown Source:0)
at android.bluetooth.BluetoothGatt$1.onClientConnectionState(BluetoothGatt.java:244)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:70)
at android.os.Binder.execTransact(Binder.java:697)
D/BleDeviceManagerNew$observeRssiTest: test1:error
E/plication$setupApp: Terminal Exception From RXJAVA was Not handled correctly
io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | com.polidea.rxandroidble2.exceptions.BleDisconnectedException: Disconnected from MAC='E9:CF:8A:D0:01:19' with status 8 (GATT_INSUF_AUTHORIZATION or GATT_CONN_TIMEOUT)
at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
at io.reactivex.internal.operators.observable.ObservableUnsubscribeOn$UnsubscribeObserver.onError(ObservableUnsubscribeOn.java:67)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onError(ObservableSubscribeOn.java:63)
我不确定我还应该做什么——我已经实现了一个“包罗万象”的解决方案,但不喜欢这种方法;
RxJavaPlugins.setErrorHandler { e -> Timber.e(e, "Terminal Exception From RXJAVA was Not handled correctly") }
但并不认为这是一个好的解决方案,因为我应该能够处理蒸汽上的异常。关于我哪里出错的任何建议?
解决方案
你的代码很好。该库有一个缺陷,无法实现您想要的行为。有关该主题的更多信息,请参见该图书馆的 wiki 页面。
虽然可以设计一个不会抛出的 API,但UndeliverableException
它需要有一个单独的错误Observable
或Completable
用于BluetoothAdapter
关闭和一个单独的用于RxBleConnection
断开连接。用户将负责将它们适当地混合到他们的链中。
当前的 API 不允许这样做。
推荐阅读
- python - 如何更改pip的安装路径
- google-apps-script - 如何通过应用程序脚本在谷歌表格中使用带有逗号分隔值的单元格创建动态下拉列表
- holoviews - 维度 value_format 回调无法正常工作
- node.js - api返回的数据不显示
- python - 非单调数组中的高效 Numpy 搜索
- plotly - 如何禁用和/或更改 Plot.ly 甘特日期快速选择器的日期范围?
- excel - 从 Excel 复制单元格范围
- javascript - 使用 Javascript 验证 PIN - SyntaxError: Unexpected token )
- javascript - 在纵向模式下打印时是否存在像素限制?
- angular - 如何在 navigationEnd 事件中获取当前路线配置?