0

I am trying to read from a characteristic and then write to the same characteristic, but was unable to figure out how. As a first step, I was trying to do multiple writes following this example:

connectionDisposable =
                connectionObservable.flatMap(rxBleConnection -> {
                    return rxBleConnection.writeCharacteristic(SSID, bytes)
                            .flatMap(ssidBytes -> rxBleConnection.writeCharacteristic(SSID, bytes)
                                    .flatMap(ssid2Bytes -> rxBleConnection.writeCharacteristic(SSID, bytes)));
                })
                        .subscribe(ssid3Bytes -> {
                            //do something
                        }, this::onError, this::onComplete);

But I am getting the following error:

no instance(s) of type variable(s) R exist so that Single<R> conforms to ObservableSource<? extends R>

It would be great if someone can help me figure out how to do more than just one read or one write using RxAndroidBle.

tharun
  • 348
  • 6
  • 15

1 Answers1

0

The issue you observe is because the example uses RxJava 1, and the current version of RxAndroidBle is based on RxJava 2. During the transition some APIs changed from Observable<byte[]> to Single<byte[]>.

Flat mapping Observable to Single cannot be done with a .flatMap() operator but needs a .flatMapSingle().

Apart from that — if you are not interested in what data you had written then a simpler (in my opinion at least) alternative is available:

Completable writeMultipleCharacteristics(RxBleConnection rxBleConnection) {
    return Completable.concatArray(
        rxBleConnection.writeCharacteristic(SSID, bytes).ignoreElement(),
        rxBleConnection.writeCharacteristic(SSID, bytes).ignoreElement(),
        rxBleConnection.writeCharacteristic(SSID, bytes).ignoreElement()
    );
}

P.S. I have updated the example you mentioned with RxJava 2 code.

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21