RxAndroidBle多个特征通知和读/写 [英] RxAndroidBle Multiple Characteristic Notifications and Read/Write
问题描述
我在设置多个特征的通知时遇到问题. 我已经阅读了文档,许多示例仅涉及非常详细的情况.
I'm having issues setting up notifications on multiple characteristics. I've reviewed the documentation and many of the examples only cover very granular situations.
我的用例如下: 1.扫描设备 2.用户选择要连接的设备(连接持续到关闭应用程序为止) 3.订阅具有许多特征的通知 4.一次读/写单个特征,在某些情况下一次读/写许多特征
My use case is as follows: 1. scan for devices 2. user selects device to connect to (with the connection persisting until the app is closed) 3. subscribe to notifications for many characteristics 4. read/write to either single characteristics at a time, and in some cases read/write to many characteristics at a time
推荐答案
这是我多次写入的解决方案
this my solution for multiple write
mConnObservable.flatMapSingle(rxBleConnection -> {
return rxBleConnection.writeCharacteristic(SSID, mSsidView.getText().toString().getBytes())
.flatMap(ssidBytes -> rxBleConnection.writeCharacteristic(SSID2, mPassPhrase.getText().toString().getBytes())
.flatMap(ssid2Bytes -> rxBleConnection.writeCharacteristic(SSID3, mSecurityModeSpinner.getSelectedItem().toString().getBytes())));
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(ssid3Bytes -> {
//do something
}, this::onError, this::onComplete);
您应该将其他flatMap操作放在第一个flatMap中,因为您只能在第一个flatMap中获得rxBleConnection
you should put the other flatMap operations in first flatMap, beacause you can only get rxBleConnection in first flatMap
RxAndroidBle RxJava 1版本的原始解决方案:
Original solution for RxAndroidBle RxJava 1 version:
mConnObservable.flatMap(rxBleConnection -> {
return rxBleConnection.writeCharacteristic(SSID, mSsidView.getText().toString().getBytes())
.flatMap(ssidBytes -> rxBleConnection.writeCharacteristic(SSID2, mPassPhrase.getText().toString().getBytes())
.flatMap(ssid2Bytes -> rxBleConnection.writeCharacteristic(SSID3, mSecurityModeSpinner.getSelectedItem().toString().getBytes())));
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(ssid3Bytes -> {
//do something
}, this::onError, this::onComplete);
这篇关于RxAndroidBle多个特征通知和读/写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!