RxAndroidBLE trying to read characteristic gives compilation error

163 Views Asked by At

Hello I am trying to learn RxAndroidBLE and read single characteristic with the following code:

                String macAddress = "84:CC:A8:2E:24:6A";
                RxBleDevice device = rxBleClient.getBleDevice(macAddress);
                Disposable disposable = device.establishConnection(false) // <-- autoConnect flag
                        .subscribe(
                                rxBleConnection -> {
                                // All GATT operations are done through the rxBleConnection.
//                                    Toast.makeText(MainActivity.this, "connected" , Toast.LENGTH_LONG).show();
                                },
                                throwable -> {
                                    // Handle an error here.
                                    Toast.makeText(MainActivity.this, "connect error" + throwable.getMessage(), Toast.LENGTH_LONG).show();
                                }
                        );
// When done... dispose and forget about connection teardown :)
                disposable.dispose();
                // Read characteristic
                device.establishConnection(false)
                        .flatMapSingle(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString(PIN_UUID))
 //                      .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                                characteristicValue -> {
                                    // Read characteristic value.
                                    rtext.setText(new String(characteristicValue));
                                },
                                throwable -> {
                                    Toast.makeText(MainActivity.this, "read error" + throwable.getMessage(), Toast.LENGTH_LONG).show();
                                    // Handle an error here.
                                }
                        ));

This gives error :

                    .flatMapSingle(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString(PIN_UUID))
                                  ^
(argument mismatch; bad return type in lambda expression
  Disposable cannot be converted to SingleSource<? extends R>)

where R,T are type-variables: R extends Object declared in method flatMapSingle(Function<? super T,? extends SingleSource<? extends R>>) T extends Object declared in class Observable

I have tried to use .flatMap and .flatMapSingle but both give the same error. I also wonder if I uncomment the line: .observeOn(AndroidSchedulers.mainThread())

AndroidSchedulers is not defined and the import line does not exist: import io.reactivex.android.schedulers.AndroidSchedulers;

1

There are 1 best solutions below

0
Dariusz Seweryn On

Apparently you have misplaced the brackets. In the line that starts with .flatMapSingle is closed at the end of the snippet.

                device.establishConnection(false)
// [Answer] The line below should be closed but instead the closing bracket is ...
                        .flatMapSingle(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString(PIN_UUID)) 
 //                      .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                                characteristicValue -> {
                                    // Read characteristic value.
                                    rtext.setText(new String(characteristicValue));
                                },
                                throwable -> {
                                    Toast.makeText(MainActivity.this, "read error" + throwable.getMessage(), Toast.LENGTH_LONG).show();
                                    // Handle an error here.
                                }
// [Answer] ... is closed below! Thus you try to return a Disposable where a Single is expected
                        )); 

Try using below:

                device.establishConnection(false)
                        .flatMapSingle(rxBleConnection -> rxBleConnection.readCharacteristic(UUID.fromString(PIN_UUID))) 
 //                      .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(
                                characteristicValue -> {
                                    // Read characteristic value.
                                    rtext.setText(new String(characteristicValue));
                                },
                                throwable -> {
                                    Toast.makeText(MainActivity.this, "read error" + throwable.getMessage(), Toast.LENGTH_LONG).show();
                                    // Handle an error here.
                                }
                        );