问题
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.
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
回答1:
I got it working now. The issue was I needed to be working with an instance of RxBleConnection
for later connectivity
回答2:
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);
you should put the other flatMap operations in first flatMap, beacause you can only get rxBleConnection in first flatMap
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);
来源:https://stackoverflow.com/questions/43667549/rxandroidble-multiple-characteristic-notifications-and-read-write