如何在安卓中从BLE读取数据的完整长度,当我的数据包含0x0a和0x0d?

huangapple 未分类评论42阅读模式
英文:

How to read full length of data from BLE in android when my data contain 0x0a and 0x0d?

问题

我能正确连接到设备,并且能够正确地接收特征,没有任何错误。但是当接收到的数据中包含 0x0a 和 0x0d 时会出现问题。

例如,设备将数据发送为 0b710a6000179c21346fb23feec651bc 给移动应用。应用将会按如下方式接收消息。应用将数据拆分成了两部分数据。

> D/BluetoothLeService: 接收数据 = 0B71
>
> D/BluetoothLeService: 接收数据 = 6000179C21346FB23FEEC651BC

这是函数 broadcastUpdate 的日志消息:

  1. private void broadcastUpdate(final String action,
  2. final BluetoothGattCharacteristic characteristic) {
  3. final Intent intent = new Intent(action);
  4. if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
  5. ...
  6. } else {
  7. // 对于所有其他配置文件,将数据按十六进制格式写入。
  8. final byte[] data = characteristic.getValue();
  9. if (data != null && data.length > 0) {
  10. String hexData=DeviceControlActivity.bytesToHex(data);
  11. Log.d(TAG, "接收数据 = "+hexData);
  12. intent.putExtra(EXTRA_DATA, data);
  13. }
  14. }
  15. sendBroadcast(intent);
  16. }

我尝试使用 nRF Connect 应用来测试 BLE 的写入/读取操作,但是它也得到了与上述相同的输出。后来我发现 0x0A 是换行字符 \n,而 0x0D 是回车字符 \r

是否有办法在数据包含 0x0A0x0D 时接收到完整的数据长度?这样应用就可以接收到完整的数据 0B710A6000179C21346FB23FEEC651BC。请给予建议。谢谢。

英文:

I am able to connect to the device correctly and it can receive the characteristic correctly without any errors. But it gets something wrong when the received data contain 0x0a and 0x0d.

For example, the device sends the data as 0b710a6000179c21346fb23feec651bc to the mobile app. The app will receive the message as below. The app split the data into 2 data.

> D/BluetoothLeService: Receive Data = 0B71
>
> D/BluetoothLeService: Receive Data = 6000179C21346FB23FEEC651BC

This is the log message from the function broadcastUpdate

  1. private void broadcastUpdate(final String action,
  2. final BluetoothGattCharacteristic characteristic) {
  3. final Intent intent = new Intent(action);
  4. if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
  5. ...
  6. } else {
  7. // For all other profiles, writes the data formatted in HEX.
  8. final byte[] data = characteristic.getValue();
  9. if (data != null && data.length > 0) {
  10. String hexData=DeviceControlActivity.bytesToHex(data);
  11. Log.d(TAG, "Receive Data = "+hexData);
  12. intent.putExtra(EXTRA_DATA, data);
  13. }
  14. }
  15. sendBroadcast(intent);
  16. }

I tried to use nRF Connect application to test the BLE write/read but it also gets the same output as above. Then I found that 0x0A is the newline character \n and 0x0D is the return characters \r.

Is there any way to receive the full length of data when it contains 0x0A and 0x0D? So the app can receive full data as 0B710A6000179C21346FB23FEEC651BC.
Please advice. Thank you

答案1

得分: 0

蓝牙协议栈不会对数据进行任何分析。

必须是在远程设备上运行的应用在发送之前将数据分成两部分。

英文:

BLE stacks do not analyze the data at all.

It must be the application running on the remote device that splits the data into two parts before sending.

huangapple
  • 本文由 发表于 2020年4月4日 19:11:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/61027187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定