I apologise if this is a duplicate question, I could not find a helpful answer anywhere. I am also a beginner in reactive programming so I am sorry in advance for silly questions.
I am using rxandroidble2 in a Kotlin Android app. I can successfully connect to a BLE device if I only want to read from one characteristic. However, I cannot get it to work for connecting to two separate characteristics.
Characteristic A sends packets every two seconds. Characteristic B sends packets multiple times per second. I can connect to both of them if I do it separately, i.e. run the app with the connection made for only CharA or CharB.
The solutions I have tried are all from stackoverflow, in particular I have tried:
- RxAndroidBle Multiple Characteristic Notifications and Read/Write
- Read multiple characteristics from an Android device using library RxAndroidBle
- https://github.com/Polidea/RxAndroidBle/issues/61
- RxAndroidBle crash on subscribe to multiple characteristics
My code is this:
respeckLiveSubscription = connectionObservable?.flatMap{
Observable.combineLatest(
it.setupNotification(UUID.fromString(Constants.CHARACTERISTIC_A_UUID)),
it.setupNotification(UUID.fromString(Constants.CHARACTERISTIC_B_UUID)),
io.reactivex.functions.BiFunction<Observable<ByteArray>, Observable<ByteArray>, Pair<Observable<ByteArray>, Observable<ByteArray>>> {
t1, t2 -> Pair.create(t1, t2)
}
)
}?.subscribe({
processRESpeckPacket(it.first.blockingFirst(), respeckVersion, this)
Log.i("ble", "processed first packet")
processRESpeckPacket(it.second.blockingFirst(), 7, this)
Log.i("ble", "processed second packet")
},{
Log.i("ble", "error when connecting = " + it.stackTrace)
})
Log.i("ble", "does this mean connection is over")
I have a few issues:
- The subscribe method receives an object of type Pair<Observable, Observable>. When I try to get the objects in the pair, they are returned as observables and the only way I managed to get the observable values was by calling blockingFirst(). Obviously that is not a good solution as my code doesn't run.
- I cannot get the BiFunction to return a Pair of ByteArrays instead of Observable.
- If I comment out the processRESpeckPacket lines and just leave the logs, they only run once, which makes me think that the value change notifications are not working.
- I also tried most of the solutions above in Java and I am getting the same behaviour. Did something change in rxandroidble2 that is not explained in the stackoverflow answers that I linked to?
- Is the fact that my characteristics are sent at different frequencies causing all the problems?
Please help me out I have been stuck with this forever! How can I get it to work in Kotlin?
Thanks a lot.