尽我所能了解RxJava中的所有魔力和出色的rxandrodible库后,我陷入了困境!我需要修复的最后一件事是设置一个写/通知链接,在其中写一个值(单个值),然后订阅一个特定的特性。
我使用了以下代码-作为最佳实践(?)-但实际上并没有在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());}
);
没有给订阅TextView的预订所写的数据,我找不到任何出错的地方。
如果我只是设置通知,而不将其与写入结合使用,那么一切都会正常进行。
关于如何使其工作的任何建议?
所使用的代码示例仅给出一个响应。我一直在寻找正在进行的写入和订阅。我没有意识到take(1)调用实际上起到了作用,认为这是对复杂调用结构的清理。
对不起!这项功能符合我的预期:
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());}
);