首页 > 解决方案 > 在 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") }

但并不认为这是一个好的解决方案,因为我应该能够处理蒸汽上的异常。关于我哪里出错的任何建议?

标签: rx-java2rxandroidble

解决方案


你的代码很好。该库有一个缺陷,无法实现您想要的行为。有关该主题的更多信息,请参见该图书馆的 wiki 页面

虽然可以设计一个不会抛出的 API,但UndeliverableException它需要有一个单独的错误ObservableCompletable用于BluetoothAdapter关闭和一个单独的用于RxBleConnection断开连接。用户将负责将它们适当地混合到他们的链中。

当前的 API 不允许这样做。


推荐阅读