英文:
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 && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data) {
stringBuilder.append(String.format("%02X ", byteChar));
//Log.v("AndroidLE", 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());
//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'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 && data.length > 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论