在 RxAndroidBle 流中抛出 UndeliverableException

UndeliverableException thrown within a RxAndroidBle stream

我有一个行为异常的 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.onConnectionStateChange(RxBleGattCallback.java:77)
        at android.bluetooth.BluetoothGatt.run(BluetoothGatt.java:249)
        at android.bluetooth.BluetoothGatt.runOrQueueCallback(BluetoothGatt.java:725)
        at android.bluetooth.BluetoothGatt.-wrap0(Unknown Source:0)
        at android.bluetooth.BluetoothGatt.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)

我不确定我还应该做什么 - 我已经实施了 'catch all' 解决方案,但不喜欢这种方法;

RxJavaPlugins.setErrorHandler { e -> Timber.e(e, "Terminal Exception From RXJAVA was Not handled correctly") }

但我不认为这是一个很好的解决方案,我应该能够处理 Steam 上的异常。对我哪里出错有什么建议吗?

你的代码没问题。该库有一个缺陷,不允许实现您想要的行为。有关该主题的更多信息,请参见 this library's wiki page

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

当前API不允许。