我正在尝试通过 rxJava 向设备发送命令列表。这是我的代码:
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));
}
});
}
}
它可以工作,然后我单击一次按钮。比如我的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);
}
LogCat 中的 myLogs:
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]
但是当我使用快速双击按钮时会出现问题。然后第一次单击 observable 仍然有效,我再次单击以再次调用 startWriteCommunication 方法。在此之后,我的日志看起来是这样的:
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]
主要问题是它们不按顺序排列,而且我的设备工作不正确。你能帮忙找出问题吗?