I have a list of BLE devices, and am using RxJava to interact with them. I need to emit an item from the list, write a characteristic to it repeatedly until X happens, and then proceed to the next item in the list.
Current code:
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... ");
}
});
where .connectForPolicing()
looks like:
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;
}
}));
}
This code seems to immediately emit all the items in the list, and therefore will connect and buzz all items at the same time. How can I emit items one at a time so that I may interact with them?
The pseudocode would be something like:
for(Device device : devices) {
device.connect();
while(device.isConnected()) {
device.beep();
}
}