Search code examples
bluetooth-lowenergyrx-java2rxandroidblebluetooth-gatt

How to Retry RxAndroidBLE Discover Services in case of GATT error.


I am using RxAndroidBLE library for discovering services in my GATT server. it works fine most of the time but often i get GATT error 133 (0x85) and it fails. I will like to retry for discovery of the service couple of time for a time period, say for 5 seconds. here is the code i am trying

bleDevice = mBleClient.getBleDevice(macAddress);
    subscription =  bleDevice.establishConnection(false)
            .flatMap(RxBleConnection::discoverServices)
            .first() // Disconnect automatically after discovery
            .observeOn(AndroidSchedulers.mainThread())
            .doOnUnsubscribe(this::onUnsubscribe)
            .compose(this.bindToLifecycle())
            .retryWhen(errors -> errors.flatMap(error -> {
                        if (isGattError(error) {
                            return Observable.just(new Object());
                        } else {
                            return Observable.error(error);
                        }
                    }
            ))
            .timeout(5, TimeUnit.SECONDS)
            .subscribe(this::getScanResult, this::onConnectionFailure);

Its not working and looks like the retryWhen is not getting called. It may be more of rxJava issue but i will really appreciate any help on this.


Solution

  • As you wrote in the comments your this::onUnsubscribe is calling subscription.unsubscribe() so the .retryWhen() operator has no possibility of being called.

    You could move the .doOnUnsubscribe() below of .retryWhen() or the other way around to give get the intended behaviour.