【发布时间】:2016-11-17 23:41:52
【问题描述】:
我有一个 BLE 设备列表,并且正在使用 RxJava 与它们进行交互。我需要从列表中发出一个项目,重复写入一个特征直到 X 发生,然后继续到列表中的下一个项目。
当前代码:
Observable.from(mDevices)
.flatMap(new Func1<Device, Observable<?>>() {
@Override
public Observable<?> call(Device device) {
Log.d(TAG, "connecting for policing");
return device.connectForPolicing();
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Object>() {
@Override
public void call(Object o) {
Log.d(TAG, "subscribing... ");
}
});
.connectForPolicing() 的样子:
public Observable<byte[]> connectForPolice() {
....
return device.establishConnection(mContext, false)
.flatMap(new Func1<RxBleConnection, Observable<byte[]>>() {
@Override
public Observable<byte[]> call(RxBleConnection rxBleConnection) {
byte[] value = new byte[1];
value[0] = (byte) (3 & 0xFF);
//Buzz the device
return rxBleConnection.writeCharacteristic(Constants.BUZZER_SELECT, value);
}
})
.repeat(3)//ignore
.takeUntil(device.observeConnectionStateChanges().filter(new Func1<RxBleConnection.RxBleConnectionState, Boolean>() {
@Override
public Boolean call(RxBleConnection.RxBleConnectionState rxBleConnectionState) {
return rxBleConnectionState == RxBleConnection.RxBleConnectionState.DISCONNECTING;
}
}));
}
此代码似乎会立即发出列表中的所有项目,因此会同时连接和蜂鸣所有项目。如何一次发出一个项目以便与它们交互?
伪代码类似于:
for(Device device : devices) {
device.connect();
while(device.isConnected()) {
device.beep();
}
}
【问题讨论】:
标签: android rx-java rx-android android-ble rxandroidble