【发布时间】:2020-10-18 16:52:08
【问题描述】:
Arduino 通过蓝牙从传感器发送数据。我想将数据存储在一个数组中以对其进行操作。
这部分从特征中获取数据
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);
}
数据在 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));**
}
}
};
知道如何执行此功能吗?
也许我应该从修改broadcastUpdate中的格式开始,但我不太明白。
谢谢
【问题讨论】:
标签: java android android-studio arduino bluetooth-lowenergy