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

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

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

问题

以下是翻译好的内容:

Arduino通过蓝牙从传感器发送数据。我想将数据存储在一个数组中以便对其进行操作。

这部分从characteristic获取数据
```java
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);
    Log.v("AndroidLE", "broadcastUpdate()");
    final byte[] data = characteristic.getValue();

    if (data != null && data.length > 0) {
        final StringBuilder stringBuilder = new StringBuilder(data.length);
        for(byte byteChar : data) {
            stringBuilder.append(String.format("%02X ", byteChar));
        }

        intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
        Log.v("AndroidLE", new String(data));
        Log.v("AndroidLE", stringBuilder.toString());
    }

    sendBroadcast(intent);
}

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

2020-10-18 14:27:07.434 32292-32292 V/AndroidLE: 415
    419
    418
    418
    
    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)) {
            // 在用户界面上显示所有受支持的服务和characteristic。
            connect_caracterist_ard(mBluetoothLeService.getSupportedGattServices());
        } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
            storage_vect(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
        }
    }
};

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

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

谢谢!


<details>
<summary>英文:</summary>

Arduino sends data from a sensor via bluetooth. I want to store the data in a array to operate with them.

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);

    if (data != null &amp;&amp; data.length &gt; 0) {
        final StringBuilder stringBuilder = new StringBuilder(data.length);
          for(byte byteChar : data) {
              stringBuilder.append(String.format(&quot;%02X &quot;, byteChar));
            //Log.v(&quot;AndroidLE&quot;, String.format(&quot;%02X &quot;, byteChar));
        }

        intent.putExtra(EXTRA_DATA, new String(data) + &quot;\n&quot; + stringBuilder.toString());
        Log.v(&quot;AndroidLE&quot;, new String(data));
        Log.v(&quot;AndroidLE&quot;, stringBuilder.toString());
        //intent.putExtra(EXTRA_DATA, new String(data));
        //intent.putExtra(EXTRA_DATA, stringBuilder.toString());
    }

    sendBroadcast(intent);
}
the data has these formats in logcat

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

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));
}
}
};


Any idea how to perform this function?

Maybe i should start by modifying the format in the broadcastUpdate, but I don&#39;t understand that well.

Thanks 


</details>


# 答案1
**得分**: 0

你已经以Bytearray形式接收到数据,但你的代码会将每个字节转换为相应的十六进制值,并将结果存储在字符串中。

将你的`broadcastUpdate`修改为以下内容:

```java
private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);
    final byte[] data = characteristic.getValue();
    if (data != null && data.length > 0) {
        intent.putExtra(EXTRA_DATA, data);
    }
    sendBroadcast(intent);
}

这样做可以消除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:

private void broadcastUpdate(final String action,
                             final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);
    final byte[] data = characteristic.getValue();
    if (data != null &amp;&amp; data.length &gt; 0) {
        intent.putExtra(EXTRA_DATA, data);
    }
    sendBroadcast(intent);
}

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:

确定