我有一个在虚拟串行端口上一次只能接受 20 个字节的设备(使用 BLE)。如果我没记错的话, createNewLongWriteBuilder 似乎是完美的方法。
这是我的尝试:
String newNameMsg = "SOME STRING THAT IS LONGER THAN 20 CHARACTERS";
byte[] byteMsg = newNameMsg.getBytes(Charset.forName("UTF-8"));
byte[] endLine = hexStringToByteArray("0D"); // signifies end of line for my device
byte[] newName = new byte[byteMsg.length + endLine.length];
System.arraycopy(byteMsg, 0, newName, 0, byteMsg.length);
System.arraycopy(endLine, 0, newName, byteMsg.length, endLine.length);
connectionObservable
.flatMap(rxBleConnection -> rxBleConnection.createNewLongWriteBuilder()
.setCharacteristicUuid(characteristicUuid)
.setBytes(newName)
.setMaxBatchSize(20) // my device only accepts 20 characters at a time.
.setWriteOperationAckStrategy(new RxBleConnection.WriteOperationAckStrategy() {
@Override
public Observable<Boolean> call(Observable<Boolean> booleanObservable) {
return Observable.just(true); // this is supposed to tell the LongWriteBuilder that we should continue sending data, correct?
}
})
.build()
)
.subscribe(
byteArray -> {
// Written data.
Log.i("BLE Controller","Data has been written!");
},
throwable -> {
// Handle an error here.
}
);
实际结果:设备没有收到任何数据,但日志显示:
D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicLongWrite(107353908) D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicLongWrite(107353908) I/BLE 控制器:数据已写入!
D/RxBle#Radio:完成 RxBleRadioOperationCharacteristicLongWrite(107353908)
2017 年 3 月 30 日更新:
我最初的理解是不正确的,并且有很多问题。我发送的是订阅而不是 Observable。
s_noopy 指出:
WriteOperationAckStrategy 实际上是 ? Observable.repeatWhen() 运算符。过滤应该发生在 WOAS 内部,以便在准备好时触发重复。`
现在的情况:
我需要等待我的设备清除 TX 标志,然后才能发送下一批。为此,我需要实现setWriteOperationAckStrategy
,但我需要在发送下一批之前读取 TX 标志以查看它是否清晰。
我的尝试:
@Override
public Observable<Boolean> call(Observable<Boolean> objectObservable) {
return connectionObservable
.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(serialTX.getUuid()))
.observeOn(AndroidSchedulers.mainThread());
}
2017 年 7 月 30 日更新:
修改了 s_noopy 的代码,现在我有了:
final ByteArrayBatchObservable byteArrayBatchObservable = new ByteArrayBatchObservable(newName, 19);
Log.i("BLE Controller","sending updated name");
byteArrayBatchObservable.flatMap(bytesBatch -> // using batches of data to write...
connectionObservable.flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(vspT, bytesBatch)
.flatMap(writtenBytes -> { // ...and when each batch will be written...
final Func1<byte[], Boolean> filterFunction = txBytes -> checkIfZero(txBytes);
return rxBleConnection
.readCharacteristic(vspT.getUuid())
.repeat() // ...and repeat it...
.takeUntil(filterFunction)
.filter(filterFunction) // ...but don't emit anything until then...
.map(readBytes -> writtenBytes); // ...and emit the writtenBytesBatch...
}
), 1)
)
.subscribe(
byteArray -> {
// Written data.
Log.i("BLE Controller","BT has been renamed! :" + byteArray.toString());
},
throwable -> {
// Handle an error here.
Log.i("BLE Controller","BT rename ERROR");
}
);
public boolean checkIfZero(byte[] txBytes){
Log.i("BLE Controller", "checking if tx is cleared: " +txBytes.toString());
for (byte b : txBytes) {
if (b != 0) {
return false;
}
}
return true;
}
现在的情况:
我试图从使用 rxBleConnection 转换为使用 connectionObservable。看起来好像第一批写入成功,但即使订阅函数两次都返回成功写入的字节数组,蓝牙设备也只能看到第一批
日志
07-31 13:26:54.434 25060-25060/com.packet.sniffer I/BLE Controller: sending updated name
07-31 13:26:54.434 264-467/? I/ThermalEngine: Sensor:pa_therm1:33000 mC
07-31 13:26:54.440 25060-25060/com.packet.sniffer D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicWrite(99278425)
07-31 13:26:54.443 25060-25187/com.packet.sniffer D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicWrite(99278425)
07-31 13:26:54.458 25060-25060/com.packet.sniffer D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicWrite(56755989)
07-31 13:26:54.611 25060-25072/com.packet.sniffer D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=569a2000-b87f-490c-92cb-11ba5ea5167c status=0
07-31 13:26:54.614 25060-25243/com.packet.sniffer D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicRead(32706505)
07-31 13:26:54.614 25060-25187/com.packet.sniffer D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicWrite(99278425)
07-31 13:26:54.615 25060-25187/com.packet.sniffer D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicWrite(56755989)
07-31 13:26:54.714 25060-25071/com.packet.sniffer D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=569a2000-b87f-490c-92cb-11ba5ea5167c status=0
07-31 13:26:54.721 25060-25243/com.packet.sniffer D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicRead(39309874)
07-31 13:26:54.723 25060-25187/com.packet.sniffer D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicWrite(56755989)
07-31 13:26:54.724 25060-25187/com.packet.sniffer D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicRead(32706505)
07-31 13:26:54.809 1980-2244/com.android.bluetooth I/bt_btif_gatt: set_read_value unformat.len = 20
07-31 13:26:54.811 25060-25072/com.packet.sniffer D/RxBle#BluetoothGatt: onCharacteristicRead characteristic=569a2000-b87f-490c-92cb-11ba5ea5167c status=0
07-31 13:26:54.813 25060-25243/com.packet.sniffer I/BLE Controller: checking if tx is cleared: [B@c805018
07-31 13:26:54.813 25060-25243/com.packet.sniffer I/BLE Controller: BT has been renamed! :[B@5999a71
07-31 13:26:54.813 25060-25243/com.packet.sniffer I/BLE Controller: checking if tx is cleared: [B@c805018
07-31 13:26:54.816 25060-25187/com.packet.sniffer D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicRead(32706505)
07-31 13:26:54.817 25060-25187/com.packet.sniffer D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicRead(39309874)
07-31 13:26:54.908 1980-2244/com.android.bluetooth I/bt_btif_gatt: set_read_value unformat.len = 20
07-31 13:26:54.910 25060-25071/com.packet.sniffer D/RxBle#BluetoothGatt: onCharacteristicRead characteristic=569a2000-b87f-490c-92cb-11ba5ea5167c status=0
07-31 13:26:54.911 25060-25243/com.packet.sniffer I/BLE Controller: checking if tx is cleared: [B@a1f9bcf
07-31 13:26:54.911 25060-25243/com.packet.sniffer I/BLE Controller: BT has been renamed! :[B@208995c
07-31 13:26:54.911 25060-25243/com.packet.sniffer I/BLE Controller: checking if tx is cleared: [B@a1f9bcf
07-31 13:26:54.914 25060-25187/com.packet.sniffer D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicRead(39309874)
07-31 13:26:56.467 1980-2388/com.android.bluetooth D/HeadsetStateMachine: Disconnected process message: 10, size: 0
2017 年 7 月 31 日更新:
更新以使以前的代码同步
byteArrayBatchObservable.flatMap(bytesBatch -> // using batches of data to write...
byteArrayBatchObservable.flatMap(bytesBatch -> // using batches of data to write...
connectionObservable.flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(vspT, bytesBatch)
.flatMap(writtenBytes -> { // ...and when each batch will be written...
final Func1<byte[], Boolean> filterFunction = txBytes -> checkIfZero(txBytes);
return rxBleConnection
.readCharacteristic(vspT.getUuid())
.repeat() // ...and repeat it...
.takeUntil(filterFunction)
.filter(filterFunction) // ...but don't emit anything until then...
.map(readBytes -> writtenBytes); // ...and emit the writtenBytesBatch...
}
))
, 1)
不幸的是仍然无法正常工作,以下是相关日志:
4:55:35.108 25084-25084/com.packet.sniffer I/BLE Controller: sending updated name
07-31 14:55:35.110 25084-25084/com.packet.sniffer D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicWrite(135224277)
07-31 14:55:35.113 25084-25209/com.packet.sniffer D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicWrite(135224277)
07-31 14:55:35.113 25084-25084/com.packet.sniffer I/BluetoothLEController: verify connectivity
07-31 14:55:35.219 25084-25096/com.packet.sniffer D/RxBle#BluetoothGatt: onCharacteristicWrite characteristic=569a2000-b87f-490c-92cb-11ba5ea5167c status=0
07-31 14:55:35.225 25084-25243/com.packet.sniffer D/RxBle#Radio: QUEUED RxBleRadioOperationCharacteristicRead(155469961)
07-31 14:55:35.228 25084-25209/com.packet.sniffer D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicWrite(135224277)
07-31 14:55:35.229 25084-25209/com.packet.sniffer D/RxBle#Radio: STARTED RxBleRadioOperationCharacteristicRead(155469961)
07-31 14:55:35.316 25084-25097/com.packet.sniffer D/RxBle#BluetoothGatt: onCharacteristicRead characteristic=569a2000-b87f-490c-92cb-11ba5ea5167c status=0
07-31 14:55:35.317 25084-25243/com.packet.sniffer I/BLE Controller: checking if tx is cleared: [B@f0396a7
07-31 14:55:35.317 25084-25243/com.packet.sniffer I/BLE Controller: BT has been renamed! :[B@8983754
07-31 14:55:35.317 25084-25243/com.packet.sniffer I/BLE Controller: checking if tx is cleared: [B@f0396a7
07-31 14:55:35.320 25084-25209/com.packet.sniffer D/RxBle#Radio: FINISHED RxBleRadioOperationCharacteristicRead(155469961)