3

I am having troubles with reading a Ble device and using the RxAndroidBle library.

I keep getting this error:

BleGattException{status=22, bleGattOperation=BleGattOperation{description='CONNECTION_STATE'}}

Can anyone look at my code and see what I might be doing wrong:

subscription = rxBleDevice.establishConnection(context, true)
            .subscribe(rxBleConnection -> {
                rxBleConnection.readCharacteristic(UUID.fromString(UUID_LOG_COUNT)).doOnNext(Action1 -> Logger.d(Helper_Utils.reverseHex(HexString.bytesToHex(Action1))));
            }, throwable -> {
                Logger.d("Error", throwable.getMessage());
            });

If you need more info, I will try to provide it.

EDIT

I have used 2 different phones: OnePlus Two Android 6.0.1 Moto G Play Android 6.0.1

I have tried multiple times switching wifi and bluetooth on and off. I have never been able to get a reading with this example.

Glennmen
  • 33
  • 3
  • 12
  • Could you provide info about what phone / OS you use? You can also check if it will happen again after you will switch off and on again both WiFi and BT adapter. – Dariusz Seweryn Mar 29 '17 at 13:14
  • @s_noopy I have edited my answer. I have never gotten a reading, doesn't matter how many times I try. – Glennmen Mar 29 '17 at 14:53
  • To get an Observable working you need to subscribe to it. – Dariusz Seweryn Mar 29 '17 at 15:58
  • @s_noopy I am no expert in RxJava, but haven't I subscribed to it? Can you give an example please. – Glennmen Mar 29 '17 at 20:25
  • You have only subscribed to `RxBleDevice.establishConnection()`. After that you have created a read by `RxBleConnection.readCharacteristic()` but you have not subscribed to it. Could you clarify if `status == 22` is still the issue? – Dariusz Seweryn Mar 29 '17 at 20:50
  • Any news? Could you set `RxBleLog.setLogLevel(RxBleLog.VERBOSE)` and paste the logs from the app into the original post? – Dariusz Seweryn Mar 30 '17 at 11:40
  • @s_noopy thank you for pointing me in the right direction, if you post an answer here I will mark it as accepted. – Glennmen Mar 30 '17 at 15:32

2 Answers2

0

Thank you s_noopy for finding my problem.

This was the solution for my problem:

subscription = rxBleDevice.establishConnection(context, true)
        .subscribe(rxBleConnection -> {
           rxBleConnection.readCharacteristic(UUID.fromString(UUID_LOG_COUNT))
.subscribe(characteristicValue -> {
                            Logger.d(Helper_Utils.reverseHex(HexString.bytesToHex(characteristicValue)));
                        });
        }, throwable -> {
            Logger.d("Error", throwable.getMessage());
        });

I changed the .doOnNext with .subscribe

Glennmen
  • 33
  • 3
  • 12
0

status = 22 is a problem related to Android OS disconnecting your peripheral. There is not much you can do from the code to prevent it.

As for not reading the characteristic value — it is because you are not subscribing to it. The best approach in RxJava programming (or reactive programming in general) is to prepare a flow with only a single subscribe because then you minimise the amount of state.

You could do like this:

Subscription s = rxBleDevice.establishConnection(true) // establish the connection
  .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString(UUID_LOG_COUNT))) // when the connection is established start reading the characteristic
  .take(1) // after the first value unsubscribe from the upstream to close the connection
  .subscribe( // subscribe to read values
    characteristicValue -> Logger.d(Helper_Utils.reverseHex(HexString.bytesToHex(characteristicValue))), // do your thing with the read value here
    throwable -> Logger.d("Error", throwable.getMessage()) // log / show possible error here
  );

Remember that the result of .subscribe() is a Subscription which you can cancel by calling Subscription.unsubscribe() which will disconnect the peripheral.

My code is referencing the new API that was introduced by RxAndroidBle 1.2.0 which was released yesterday.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
  • How do you know what status 22 is? Where are the error codes? – User Apr 28 '19 at 19:33
  • 1
    It is all described in the current implementation of [`BleGattException`](https://github.com/Polidea/RxAndroidBle/blob/master/rxandroidble/src/main/java/com/polidea/rxandroidble2/exceptions/BleGattException.java) which points you to [this Android source code file](https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h). You want to search for `0x16` which is 22 decimal. – Dariusz Seweryn Apr 28 '19 at 19:42