遇到BleAlreadyConnectedException时在onErrorResumeNext重新开始读取数据

restart reading data in onErrorResumeNext when BleAlreadyConnectedException is encountered

我正在使用 RXAndroidBle 库连接 BLE 设备并从中读取数据。我已将 establishConnection 功能设置为 true 即自动连接为真。当 BleAlreadyConnectedException 发生时,我想捕获该异常并重新启动读取数据流,因为每次处理和连接到 BLE 设备都会产生问题。所以最好保持连接并重新读取数据。

onErrorResumeNexti中重新调用writeStatus、readModelInfo、getReadings等函数。现在确定我将如何实现它。

   device.establishConnection(true)
        .flatMap(rxBleConnection -> {
            rxBleConnection.discoverServices();
            mRxBleConnection = rxBleConnection;
            return Observable.just(rxBleConnection);
        })
        .flatMap(rxBleConnection -> rxBleConnection.setupNotification(TSDictionary.BATTERY_LEVEL,NotificationSetupMode.QUICK_SETUP).flatMap(it->it))
        .flatMap(bytes -> writeStatus())
        .flatMap(bytes->readModelInfo(bytes))
        .flatMap(bytes -> getReadings(bytes))
        .doOnNext(data->initializeErrorHistory(data))
        .flatMap(data->getSequenceSize())
        .flatMap(length ->getOperationInfo(length))
        .doOnNext(data->initializeOperationInfo(data))
        .onErrorResumeNext(new Function<Throwable, ObservableSource<? extends ArrayList<Map<Integer, TSDictionaryMetaData>>>>() {
            @Override
            public ObservableSource<? extends ArrayList<Map<Integer, TSDictionaryMetaData>>> apply(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {

                if(throwable instanceof  BleAlreadyConnectedException){

                    // i want to RECALL/restart the  function call 
                    // writeStatus ,readModelInfo,getReadings, initializeErrorHistory
                    // getSequenceSize , getOperationInfo, initializeOperationInfo
                }
                return null;
            }
        })
        .subscribe(data -> {

        }, e -> {
            e.printStackTrace();
        });

onErrorResumeNext放在靠近连接代码的地方。

device.establishConnection(true)
.doOnNext(rxBleConnection -> {
    rxBleConnection.discoverServices();
    mRxBleConnection = rxBleConnection;
})
.onErrorResumeNext(throwable -> {
     if (throwable instanceof BleAlreadyConnectedException) {
        return Observable.just(mRxBleConnection);
     }
     return Observable.error(throwable);
})
.flatMap(rxBleConnection -> 
     rxBleConnection.setupNotification(TSDictionary.BATTERY_LEVEL,
         NotificationSetupMode.QUICK_SETUP)
    .flatMap(it->it)
)
.flatMap(bytes -> writeStatus())
.flatMap(bytes->readModelInfo(bytes))
.flatMap(bytes -> getReadings(bytes))
.doOnNext(data->initializeErrorHistory(data))
.flatMap(data->getSequenceSize())
.flatMap(length ->getOperationInfo(length))
.doOnNext(data->initializeOperationInfo(data))
.subscribe(data -> {

}, e -> {
    e.printStackTrace();
});