在特征上编写单个命令花了太长时间。我在下面的代码中使用了一个命令和一个循环。
getConnObservable()
.first()
.flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristics, command))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
bytes -> onWriteSuccess(),
this::onWriteFailure
);
在设备上写入差不多600毫秒。我需要逐个写100个命令。
任何人都可以解释一下进行批量操作的最佳方法
答案 0 :(得分:0)
通过BLE获得最高性能的最佳方法是使用相同的RxBleConnection
来执行所有写入 - 这意味着减轻RxJava的开销,即:
getConnObservable()
.first()
.flatMapCompletable(rxBleConnection -> Completable.merge(
rxBleConnection.writeCharacteristic(characteristics, command0)).toCompletable(),
rxBleConnection.writeCharacteristic(characteristics, command1)).toCompletable(),
(...)
rxBleConnection.writeCharacteristic(characteristics, command99)).toCompletable(),
))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
this::onWriteSuccess,
this::onWriteFailure
);
此外,可以通过订阅rxBleConnection.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH, delay, timeUnit)
来尝试协商最短的连接间隔(CI)
如果外围设备/特性支持此写入类型,则可以通过设置bluetoothGattCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_WITHOUT_RESPONSE)
来实现进一步的加速。*
*请注意,没有响应的写入的内部缓冲区是有限的,并且取决于API级别的行为有点不同。但是,对于约100次写作而言应该没关系。
关于this conversation:
RxAndroidBle
是一个蓝牙低功耗库,在性能方面与Blue2Serial
(which uses standard Bluetooth)进行比较并不是最好的选择。它们具有不同的用例 - 就像使用WiFi或以太网电缆来访问互联网一样。
最好的问候