如何将从Arduino接收的蓝牙输入数据存储在数组中?

huangapple go评论98阅读模式
英文:

How do i store bluetooth input data received from Arduino in a array?

问题

以下是翻译好的内容:

  1. Arduino通过蓝牙从传感器发送数据。我想将数据存储在一个数组中以便对其进行操作。
  2. 这部分从characteristic获取数据
  3. ```java
  4. private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
  5. final Intent intent = new Intent(action);
  6. Log.v("AndroidLE", "broadcastUpdate()");
  7. final byte[] data = characteristic.getValue();
  8. if (data != null && data.length > 0) {
  9. final StringBuilder stringBuilder = new StringBuilder(data.length);
  10. for(byte byteChar : data) {
  11. stringBuilder.append(String.format("%02X ", byteChar));
  12. }
  13. intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
  14. Log.v("AndroidLE", new String(data));
  15. Log.v("AndroidLE", stringBuilder.toString());
  16. }
  17. sendBroadcast(intent);
  18. }

数据在Logcat中以这些格式显示

  1. 2020-10-18 14:27:07.434 32292-32292 V/AndroidLE: 415
  2. 419
  3. 418
  4. 418
  5. 34 31 35 0D 0A 34 31 39 0D 0A 34 31 38 0D 0A 34 31 38 0D 0A
  6. 2020-10-18 14:27:07.446 32292-32339 V/AndroidLE: broadcastUpdate()
  7. 2020-10-18 14:27:07.448 32292-32339 V/AndroidLE: 417
  8. 417
  9. 2020-10-18 14:27:07.449 32292-32339 V/AndroidLE: 34 31 37 0D 0A 34 31 37
  1. private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. final String action = intent.getAction();
  5. if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
  6. updateConnectionState(true);
  7. } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
  8. updateConnectionState(false);
  9. clearUI();
  10. } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
  11. // 在用户界面上显示所有受支持的服务和characteristic。
  12. connect_caracterist_ard(mBluetoothLeService.getSupportedGattServices());
  13. } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
  14. storage_vect(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
  15. }
  16. }
  17. };

如何执行这个函数?有任何想法吗?

也许我应该从修改broadcastUpdate中的格式开始,但我对此不太了解。

谢谢!

  1. <details>
  2. <summary>英文:</summary>
  3. Arduino sends data from a sensor via bluetooth. I want to store the data in a array to operate with them.
  4. This part obtains the data from the characteristic

private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
Log.v("AndroidLE", "broadcastUpdate()");
final byte[] data = characteristic.getValue();
//Log.v("AndroidLE", "data.length: " + data.length);

  1. if (data != null &amp;&amp; data.length &gt; 0) {
  2. final StringBuilder stringBuilder = new StringBuilder(data.length);
  3. for(byte byteChar : data) {
  4. stringBuilder.append(String.format(&quot;%02X &quot;, byteChar));
  5. //Log.v(&quot;AndroidLE&quot;, String.format(&quot;%02X &quot;, byteChar));
  6. }
  7. intent.putExtra(EXTRA_DATA, new String(data) + &quot;\n&quot; + stringBuilder.toString());
  8. Log.v(&quot;AndroidLE&quot;, new String(data));
  9. Log.v(&quot;AndroidLE&quot;, stringBuilder.toString());
  10. //intent.putExtra(EXTRA_DATA, new String(data));
  11. //intent.putExtra(EXTRA_DATA, stringBuilder.toString());
  12. }
  13. sendBroadcast(intent);
  14. }
  1. the data has these formats in logcat

2020-10-18 14:27:07.434 32292-32292 V/AndroidLE: 415
419
418
418

  1. 34 31 35 0D 0A 34 31 39 0D 0A 34 31 38 0D 0A 34 31 38 0D 0A

2020-10-18 14:27:07.446 32292-32339 V/AndroidLE: broadcastUpdate()
2020-10-18 14:27:07.448 32292-32339 V/AndroidLE: 417
417
2020-10-18 14:27:07.449 32292-32339 V/AndroidLE: 34 31 37 0D 0A 34 31 37

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
updateConnectionState(true);
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
updateConnectionState(false);
clearUI();
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the user interface.
connect_caracterist_ard(mBluetoothLeService.getSupportedGattServices());
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
storage_vect(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
}
}
};

  1. Any idea how to perform this function?
  2. Maybe i should start by modifying the format in the broadcastUpdate, but I don&#39;t understand that well.
  3. Thanks
  4. </details>
  5. # 答案1
  6. **得分**: 0
  7. 你已经以Bytearray形式接收到数据,但你的代码会将每个字节转换为相应的十六进制值,并将结果存储在字符串中。
  8. 将你的`broadcastUpdate`修改为以下内容:
  9. ```java
  10. private void broadcastUpdate(final String action,
  11. final BluetoothGattCharacteristic characteristic) {
  12. final Intent intent = new Intent(action);
  13. final byte[] data = characteristic.getValue();
  14. if (data != null && data.length > 0) {
  15. intent.putExtra(EXTRA_DATA, data);
  16. }
  17. sendBroadcast(intent);
  18. }

这样做可以消除Stringbuilder,并且只将原始数据添加到Intent中。如果你从多个不同的特征中接收数据,你还应该添加intent.putExtra(EXTRA_CHAR, characteristic.getUuid().toString());。这可以让你以不同方式处理来自其他特征的数据。

你可以通过调用intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA)在你的onReceive函数中接收数据。

英文:

You already receive your data as Bytearry, but your code takes each byte, converts it to the corresponding Hex value and stores the result in a string.

Modify your broadcastUpdate to this:

  1. private void broadcastUpdate(final String action,
  2. final BluetoothGattCharacteristic characteristic) {
  3. final Intent intent = new Intent(action);
  4. final byte[] data = characteristic.getValue();
  5. if (data != null &amp;&amp; data.length &gt; 0) {
  6. intent.putExtra(EXTRA_DATA, data);
  7. }
  8. sendBroadcast(intent);
  9. }

This gets rid of the Stringbuilder and only adds the raw data to the Intent. If you receive data from multiple different characteristics you should also add intent.putExtra(EXTRA_CHAR, characteristic.getUuid().toString());. This enables you to handle data from other characteristics differently.

You can receive the data in your onReceive function by calling intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA)

huangapple
  • 本文由 发表于 2020年10月19日 00:52:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64415876.html
匿名

发表评论

匿名网友

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

确定