I am trying to send list of command to device via rxJava. Here is my code:
public void startWriteCommucation(final ArrayList<byte[]> b) {
if (isConnected()){
connectionObservable
.flatMap(new Func1<RxBleConnection, Observable<Observable<byte[]>>>() {
@Override
public Observable<Observable<byte[]>> call(final RxBleConnection rxBleConnection) {
final List<Observable<byte[]>> list = new ArrayList<>();
for (byte[] bytes: b){
Log.e("Observer", Arrays.toString(bytes));
list.add(rxBleConnection
.writeCharacteristic(BleDevice.characteristicWrite, bytes));
}
return Observable.from(list);
}
})
.concatMap(new Func1<Observable<byte[]>, Observable<byte[]>>() {
@Override
public Observable<byte[]> call(Observable<byte[]> observable) {
return observable;
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<byte[]>() {
@Override
public void call(byte[] bytes) {
view.setTextStatus("Write success");
Log.e("Subscriber", Arrays.toString(bytes));
}
});
}
}
It works, then i click button once. For example, my method to clikc:
public void onClick(){
ArrayList<byte[]> listCmd = new ArrayList<>();
listCmd.add(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
listCmd.add(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
startWriteCommucation(listCmd);
}
And myLogs in LogCat:
E/Observer: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
E/Observer: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
E/Subscriber: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
E/Subscriber: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
But problem occurs when i use fast double click to button. Then the first click with observable is still working, i click again to call startWriteCommunication method again. And after this my logs look so:
E/Observer: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
E/Observer: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
E/Observer: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
E/Observer: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
E/Subscriber: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
E/Subscriber: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
E/Subscriber: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
E/Subscriber: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Main problem that they not in order, and my device works not correct. Can you help to find a ploblem?