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;
Apparently you have misplaced the brackets. In the line that starts with
.flatMapSingleis closed at the end of the snippet.Try using below: