I'm very new when it comes to the android / BLE space. I'm attempting to write to a characteristic and listen on the notification using the RxAndroidBle library. I've looked at many implementations that just seem to be different enough that I can't get my scenario to work properly. This post: No notification data in Write/Notification set-up is almost what I need however, when I use the code posted, I'm unable to capture any data on my peripheral, which in turn doesn't return a response since I'm not sending back a response. When I switch to a long write builder I'm able to write to my peripheral and see the data flow in, but upon returning a response from my peripheral, nothing is captured in my notification subscription.
Here's my code:
private void writeNotify(RxBleDevice peripheral, byte[] payload) {
Disposable connectionSubscription = peripheral.establishConnection(false)
.flatMap( // when the connection is available...
rxBleConnection -> rxBleConnection.setupNotification(UUID.fromString(notifyCharacteristic)), // ... setup the notification...
(rxBleConnection, notifyObservable) -> Observable.combineLatest( // ... when the notification is setup...
rxBleConnection.createNewLongWriteBuilder().setBytes(payload).setCharacteristicUuid(UUID.fromString(writecharateristic)).setMaxBatchSize(20).build(), // ... write the characteristic...
//rxBleConnection.writeCharacteristic(UUID.fromString(writecharateristic), payload).toObservable(), // ... write the characteristic...
notifyObservable, // ... and observe for the first notification on the AP_SCAN_DATA
(writtenBytes, responseBytes) -> responseBytes
)
)
.flatMap(observable -> observable) // ... flatMap the result as it is Observable<byte[]>...// ... and finish after first response is received to cleanup notifications
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
responseBytes -> {
String data = new String(responseBytes);
},
throwable -> {
}
);
}
Separately, I've attempted to handle this as 2 operations where I set up my notification characteristic after performing a long write to my peripheral. Where I subscribe to the observeConnectionStateChanges method on my peripheral and wait for the disconnecting state to fire before setting up my notification characteristic. In this scenario I actually am able to receive partial responses so I know that the notification characteristic is working. I'm just not implementing this correctly. Any suggestions?