1

I'm new to Rx programming. I'm now trying to use RxAndroidBle to discover BLE devices' services and read some characteristics from the device.

I can use

device.establishConnection(false)
  .flatMap(rxBleConnection -> rxBleConnection.discoverServices());

to find device services

and use

device.establishConnection(false)
  .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUUID));

to read wanted characteristic

But I'd like to know if it is possible to combine these two tasks together? If possible, how can I do it?

Many thanks for your tips and help!

wdxpz
  • 15
  • 5

1 Answers1

1

I see that you're using a helper method readCharacteristic(UUID). You can skip the discovery at all and the library will do it for you under the hood seamlessly.

Sure, just use flatMap with zip or publish with merge. There are many ways but this one is a basic and silly one.

 bleDevice
     .establishConnection(false)
     .flatMap(connection ->
         Observable.zip(
             connection.readCharacteristic(UUID.randomUUID()).doOnNext(data -> doSomethingWithData(data)),
             connection.discoverServices().doOnNext(services -> doSomethingWithServices(data)),
             Pair::create
         )
      )
      .subscribe();

It'd be best if you could do some RxJava training.

pawel.urban
  • 1,031
  • 8
  • 10
  • Thanks for your reply! but I also want to discover services, not just read characteristics, is there any way to realize this? – wdxpz May 11 '17 at 15:17
  • Many thanks for you kindly help and suggestion! This helps to make it work! It's necessary for me do more training on RxJava – wdxpz May 12 '17 at 07:11
  • by using establishConnection and then to discover service, I found that sometimes, it never goes to discover service, and the subscriber's onError onCompleted will never happen, it seems that the connection was not established and the process will be stuck for a period. I'm wondering if there is a way to limit the time for establishing connection? Thanks a lot! – wdxpz May 12 '17 at 09:31
  • I don't find Pair in RxJAVA package. How to import ? – M D Sep 16 '19 at 06:07
  • Have a look in Android Support package. – pawel.urban Sep 16 '19 at 10:39