我只是发现rxandroidble并且可以在连接后可靠地将单个命令发送到BLE设备
但是,我一直在努力寻找编写命令链的最佳方法,例如,如果我有一系列需要发送的3条命令
当然可以通过嵌套发送来完成,但是我确定有更好的方法!
单个命令的发送代码为
rxBleMainConection.writeCharacteristic(COMS_WRITE_CHAR_UUID,bytes).toObservable()
.subscribe(
characteristicValue -> {
// Written characteristic value.
Log.d(TAG,"Written command: " + Arrays.toString(characteristicValue));
},
throwable -> {
// Handle an error here.
Log.d(TAG,"Error writing command");
throwable.printStackTrace();
}
);
发送一系列说5条命令的最佳方法是什么?
答案 0 :(得分:1)
您可以像这样将所有要写入的内容连接起来:
Single.concat(Arrays.asList(
rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytes0),
rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytes1),
rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytes2),
rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytes3),
// ...
rxBleMainConnection.writeCharacteristic(COMS_WRITE_CHAR_UUID, bytesn)
))
.subscribe(
characteristicValue -> {
// Written characteristic value.
Log.d(TAG, "Written command: " + Arrays.toString(characteristicValue));
},
throwable -> {
// Handle an error here.
Log.d(TAG, "Error writing command");
throwable.printStackTrace();
},
() -> {
Log.d(TAG, "All writes completed");
}
);
我鼓励您查看本网站上已经要求的其他questions regarding "multiple writes" with RxAndroidBle
。有些帖子可能会给您提示/想法。
作为旁注:最好创建仅使用单个.subscribe()
的代码,因为这样您便拥有最少的需要自己管理的状态。