0
votes

I am trying to read and write in/from characteristic of particular service of BLE device.

I am referring demo code from RxAndroidBle here.

In this demo as soon as discover services when you press particular characteristic it go in next screen and make connection and then doing read/write operation.

My question: is there any way to read/write any characteristics after service discovered? Why you need to make connection for with any characteristics before read/write operation?

Below code is used to connect single characteristic but i want to connect 3-4 characteristics at the same time. i tried .flatmap() but giving me errors.

  final Disposable connectionDisposable = connectionObservable
                            .flatMapSingle(RxBleConnection::discoverServices)
                            .flatMapSingle(rxBleDeviceServices -> rxBleDeviceServices.getCharacteristic(Consts.ALERT_LEVEL_UUID))
                            .observeOn(AndroidSchedulers.mainThread())
                            .doOnSubscribe(disposable -> text_connection.setText("Status: Connecting"))
                            .subscribe(
                                    characteristic -> {
                                        updateUI(characteristic);
                                        Log.i(getClass().getSimpleName(), "Hey, connection has been established!");
                                    },
                                    this::onConnectionFailure,
                                    this::onConnectionReceived
                            );

Like in below image as soon as you press R or W for any characteristic it will give you result.

enter image description here

2

2 Answers

1
votes

If I understand your question you might want to look into sharing the BLE connection using Jake Wharton's ReplayingShare.

Here is some discussion of it that might help: https://github.com/Polidea/RxAndroidBle/issues/406

Reactive purists seem to believe that you should be able to do anything you want with a single subscribe() statement, but I'm not smart enough to figure that out.

0
votes

I got work around.

For connection

 Disposable disposable = connectionObservable
                            .flatMap(connection -> connection.discoverServices()
                                            .map(RxBleDeviceServices::getBluetoothGattServices)
                                            .flatMapObservable(Observable::fromIterable) // map to individual services
                                            .map(BluetoothGattService::getCharacteristics) // for each service take all characteristics)
                                            .flatMap(Observable::fromIterable) // map to individual characteristic)
                                    //.filter(characteristic -> (BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) != 0) // consider only characteristics that have indicate or notify property

                            )
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(
                                    characteristic -> {
                                        onConnectionReceived();
                               },
                                    this::onConnectionFailure,
                                    this::onConnectionReceived
                            );

                    compositeDisposable.add(disposable);

If connection successful then you can read, write and notify any characteristics in this single connection

For read, write and notifications you can check RxAndroidBle