1

I am using RxAndroidBle library to connect to android devices, but I couldn't find any way to connect BLE device and send MTU request and get characteristic UUID of service by Service UUID in a single requet

Describe the issue

I need to connect BLE device & send MTU request and get characteristic UUID of service by Service UUID step by step

To Reproduce

Steps to reproduce the behavior:

  1. Find the BLE device by name or mac address
  2. Send MTU request 517
  3. Get service by Service UUID
  4. Get characteristic value by characteristic UUID

Additional context I can not combine all methods and unable to call one after one

private Observable<RxBleConnection> connectionObservable;
private PublishSubject<Boolean> disconnectTriggerSubject = PublishSubject.create();

private void connect() {
    Disposable disposable = connectionObservable
            .flatMapSingle(rxConn -> rxConn.requestMtu(RxBleConnection.GATT_MTU_MAXIMUM))
            .doOnComplete(() -> {
                Disposable notifDisposable = connectionObservable.doOnNext(rxBleConnection -> {
                    Single<Integer> mtu = rxBleConnection.requestMtu(RxBleConnection.GATT_MTU_MAXIMUM);
                    Log.w("TAG_TAG", "notif --> Mtu: " + rxBleConnection.getMtu());
                }).flatMapSingle(RxBleConnection::discoverServices)
                        .flatMapSingle(rxBleDeviceServices -> rxBleDeviceServices.getCharacteristic(CHARACTERISTIC_UUID))
                        .doOnComplete(() -> {
                            Disposable reedDisposable = connectionObservable.flatMapSingle(RxBleConnection::discoverServices)
                                    .flatMapSingle(rxBleDeviceServices -> rxBleDeviceServices.getCharacteristic(CHARACTERISTIC_UUID))
                                    .subscribe(
                                            characteristic -> {
                                                Log.w("TAG_TAG", "reed --> Value: " + Arrays.toString(characteristic.getValue()));
                                                if (characteristic.getValue() != null) {
                                                    Log.w("TAG_TAG", "reed --> Value: " + characteristic.getValue().length + " , HexString: " + toHexString(characteristic.getValue()));
                                                }
                                            },
                                            this::onConnectionFailure,
                                            this::onConnectionFinished
                                    );
                        })
                        .subscribe(
                                characteristic -> {
                                    Log.w("TAG_TAG", "notif --> Value: " + Arrays.toString(characteristic.getValue()));
                                    if (characteristic.getValue() != null) {
                                        Log.w("TAG_TAG", "notif --> Value: " + characteristic.getValue().length + " , HexString: " + toHexString(characteristic.getValue()));
                                    }
                                },
                                this::onConnectionFailure,
                                this::onConnectionFinished
                        );
            })
            .subscribe(
                    integer -> {
                        Log.w("TAG_TAG", "connect --> Integer: " + integer);
                    }
            );
}
Hossein Kurd
  • 3,184
  • 3
  • 41
  • 71
  • Your question is very unclear. It seems you are trying to execute multiple GATT operations in a sequence? Android has a restriction that you can only have one pending operation at a time. For example, if you call requestMtu you need to wait for onMtuChanged to arrive before you, for example, can read a characteristic value. – Emil Jun 01 '20 at 08:48
  • @Emil connect ussing RxAndroidBle library, post updated, thank you for your comment – Hossein Kurd Jun 01 '20 at 09:20
  • `I can not combine all methods and unable to call one after one` — why is that? Do you mean – you do not know how to achieve that? Could you post code that you already tried? – Dariusz Seweryn Jun 01 '20 at 09:58
  • @DariuszSeweryn Yes, I couldn't find any complete sample – Hossein Kurd Jun 01 '20 at 10:07
  • 1
    This is more of an `RxJava` problem than `RxAndroidBle` one (because you need to compose `Observable`, `Single` and `Completable` in a way you want). `RxAndroidBle` takes care of queueing commands on the Android BLE stack. – Dariusz Seweryn Jun 01 '20 at 11:21

0 Answers0