Polidea has released a new handy library called RxAndroidBle , which is very useful for solving many problems when using the vanilla Bluetooth APIs.
Before explaining more, the idea is to have a POJO model that has all the last values that the device sends (or I request) for me (in this case, it is a map object):
If I want to receive notifications of several characteristic notifications, I can do this:
final UUID serviceUuid =
final Map<UUID, byte[]> genericModel = new HashMap<>();
final Observable<RxBleConnection> connectionObservable =
connectionObservable
.flatMap(connection ->
connection.discoverServices()
.flatMap(services -> services.getService(serviceUuid).map(BluetoothGattService::getCharacteristics))
.flatMap(Observable::from)
.flatMap(characteristic -> connection
.setupNotification(characteristic)
.flatMap(observable -> observable),
Pair::new)
)
.subscribe(
pair -> genericModel.put(pair.first.getUuid(), pair.second),
throwable -> { }
);
And now, when I am connected to the device, notifications update the POJO object (Map in this particular example).
If I want to read the values, I can do the following:
connectionObservable.flatMap(connection ->
connection.discoverServices()
.flatMap(services -> services.getService(SERVICE_ID).map(BluetoothGattService::getCharacteristics))
.flatMap(Observable::from)
.filter(characteristic -> BleUtils.hasReadProperty(characteristic.getProperties()))
.flatMap(connection::readCharacteristic, Pair::new)
)
.subscribe(
pair -> genericModel.put(pair.first.getUuid(), pair.second),
throwable -> { }
);
My main question is:
: , , , . . ?
, , , , doOnComplete, , , .
, read, ( , , 7 15 , , , pojo ).
, , , , .
?
, t , .