0

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.

Avijeet
  • 365
  • 4
  • 14
  • Hello @Avijeet — are you sure that the connection and service discovery will finish successfully in those 5 seconds? – Dariusz Seweryn Aug 10 '17 at 16:44
  • what i have observed is that when it works it take only a second or two for service discovery. 5 seconds time interval can be changed , the main problem is how to retry . – Avijeet Aug 11 '17 at 02:41
  • 1
    What do you do in `.onUnsubscribe()`? – Dariusz Seweryn Aug 11 '17 at 05:37
  • onUnsubscribe i just if(subscription != null) { subscription.unsubscribe(); } – Avijeet Aug 11 '17 at 06:57
  • i also unsubscribe in connection failure as well – Avijeet Aug 11 '17 at 07:01
  • Oh I see what you are saying, since retryWhen will retry all the things above it so onUnsubscribe gets called as well and it will kill the connection.. if i don't do anything in onUnsubscribe then it should work .. right.. – Avijeet Aug 11 '17 at 07:32

1 Answers1

1

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.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
  • Thanks Dariusz, i have another small question for you, can we try the retryWhen approach for connectionObservable as well ? i face same issue(error 133) in preparing Connection Observable as well. – Avijeet Aug 11 '17 at 11:09
  • Yes, `RxBleDevice.establishConnection()` is ready to work with `.retryWhen()`. The only problem with `.retryWhen()` is when it is used on `Observable`s obtained from the `RxBleConnection` and the connection itself will be disconnected — those observables when retried will return the disconnection exception. To continue a new connection needs to be established. – Dariusz Seweryn Aug 11 '17 at 11:16
  • Do you have an example where we can handle exception and retry for connection. i have been observing that some time there is a garbage collection happens which cause BLE gatt to be disconnected, and then my code keeps waiting to connect and read from my ble device. – Avijeet Aug 11 '17 at 13:29
  • I do not get the garbage collector part but the retry when is used for instance in the [advanced sample activity](https://github.com/Polidea/RxAndroidBle/blob/master/sample/src/main/java/com/polidea/rxandroidble/sample/example4_characteristic/advanced/Presenter.java) which you may check out. – Dariusz Seweryn Aug 11 '17 at 15:23
  • Thanks Dariusz. I have tried various approach for RetryWhen in connection Observable. Once connection gets disconnected it does not work again. i will try some other approaches as well. if it does not work then may be i will post a separate question. But as always thanks for quick and timely help. – Avijeet Aug 11 '17 at 17:14
  • FYI I'm going on two week holiday so I might be unresponsive during that time. – Dariusz Seweryn Aug 11 '17 at 17:16
  • 1
    Since the RxAndroidBle2 release, here is the updated link for the [advanced sample activity](https://github.com/Polidea/RxAndroidBle/blob/master/sample/src/main/java/com/polidea/rxandroidble2/sample/example4_characteristic/advanced/Presenter.java). – Ari Lacenski Jan 09 '19 at 19:15