No notification data in Write/Notification set-up

最后都变了- 提交于 2019-12-11 06:32:41

问题


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());}
                    ); 

来源:https://stackoverflow.com/questions/52916457/no-notification-data-in-write-notification-set-up

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!