No notification data in Write/Notification set-up

2019-08-15 04:09发布

After doing my best to understand all the magic in RxJava and the excellent rxandrodible library I'm stuck! The last thing that I need to fix is to setup a write/Notification link in which I write a value - a single value - and after that subscribe to a specific characteristic.

I've used the following code - as considered best practice (?) - but it doesen't actually result in any data on the soundTV.

connectionSubscription = device.establishConnection(false)
                    .flatMap( // when the connection is available...
                            rxBleConnection -> rxBleConnection.setupNotification(RX_CHAR_UUID), // ... setup the notification...
                            (rxBleConnection, apScanDataNotificationObservable) -> Observable.combineLatest( // ... when the notification is setup...
                                    rxBleConnection.writeCharacteristic(TX_CHAR_UUID, new byte[]{SOUND}).toObservable(), // ... write the characteristic...
                                    apScanDataNotificationObservable.take(1),// ... and observe for the first notification on the AP_SCAN_DATA
                                    (writtenBytes, responseBytes) -> responseBytes // ... when both will appear return just the response bytes...
                            )
                    )
                    .flatMap(observable -> observable) // ... flatMap the result as it is Observable<byte[]>...// ... and finish after first response is received to cleanup notifications
                    .take(1)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            responseBytes -> {
                                soundTV.setText(new String(responseBytes));
                            },
                            throwable -> {
                                soundTV.setText(throwable.toString());}
                    );

There is no data written by the subscription to the give TextView, and I can't find anything that goes wrong.

If I just setup the notifications, without combining it with the write, everything works as it should.

Any suggestions on how to make it work?


The code example used gives only one response. What I was looking for was a write and a subscription that was ongoing. I didn't realize that the take(1) call actually made a difference, thought it was a clean-up in the complex call structure.

Sorry! This works as intended for me:

    connectionSubscription = device.establishConnection(false)
                    .flatMap( // when the connection is available...
                            rxBleConnection -> rxBleConnection.setupNotification(RX_CHAR_UUID), // ... setup the notification...
                            (rxBleConnection, apScanDataNotificationObservable) -> Observable.combineLatest( // ... when the notification is setup...
                                    rxBleConnection.writeCharacteristic(TX_CHAR_UUID, new byte[]{SOUND}).toObservable(), // ... write the characteristic...
                                    apScanDataNotificationObservable,// ... and observe for the first notification on the AP_SCAN_DATA
                                    (writtenBytes, responseBytes) -> responseBytes // ... when both will appear return just the response bytes...
                            )
                    )
                    .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 -> {
                                soundTV.setText(new String(responseBytes));
                            },
                            throwable -> {
                                soundTV.setText(throwable.toString());}
                    ); 

0条回答
登录 后发表回答