【问题标题】:How do i store bluetooth input data received from Arduino in a array?如何将从 Arduino 接收的蓝牙输入数据存储在数组中?
【发布时间】: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


    【解决方案1】:

    您已经以 Bytearry 形式接收数据,但您的代码获取每个字节,将其转换为相应的 Hex 值并将结果存储在字符串中。

    将您的broadcastUpdate 修改为:

    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函数中接收数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多