【问题标题】:HEX data from BLE characteristic to Byte Array, how to convert to String从 BLE 特征到字节数组的十六进制数据,如何转换为字符串
【发布时间】:2019-04-26 23:02:51
【问题描述】:

我的 BLE android 应用目前可以连接到我的 BLE 硬件并连接到 GATT 服务器。我还可以启用通知并读取特征。然而,所宣传的特征是 HEX 格式。 在我的服务上,我尝试接收字符串或字节数组格式的数据,尝试了几个转换过程,但我仍然得到无意义的数据(即 ??x 等)

关于如何接收/转换十六进制数据的任何想法? 服务:

private void broadcastUpdate(final String action,
        final BluetoothGattCharacteristic characteristic) {
    final Intent intent = new Intent(action);

    if (UUID_BLE_SHIELD_RX.equals(characteristic.getUuid())) {

        //final byte[] rx = characteristic.getValue();

        final String rx=characteristic.getStringValue(0);
        //final     char rx=raw(char.value)


        intent.putExtra(EXTRA_DATA, rx);
    }

主要活动:

   private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (Service.ACTION_GATT_DISCONNECTED.equals(action)) {
        } else if (Service.ACTION_GATT_SERVICES_DISCOVERED
                .equals(action)) {
                getGattService(mBluetoothLeService.getSupportedGattService());
        } else if (Service.ACTION_DATA_AVAILABLE.equals(action)) {
           displayData(intent.getExtras().getString(Service.EXTRA_DATA));
        }

在 NRF 连接中看到的来自 BLE 模块的数据:(0x)04-01-19-00-BE

【问题讨论】:

    标签: android hex bluetooth-lowenergy bluetooth-gatt


    【解决方案1】:

    对于特征属性,常用:

    String yourHexData = theValueOfYourHexData;
    yourHexData.getBytes();
    

    这是我用来将十六进制字符串正确转换为byte [] 的方法:

    public static byte[] hexToByteData(String hex)
    {
        byte[] convertedByteArray = new byte[hex.length()/2];
        int count  = 0;
    
        for( int i = 0; i < hex.length() -1; i += 2 )
        {
            String output;
            output = hex.substring(i, (i + 2));
            int decimal = (int)(Integer.parseInt(output, 16));
            convertedByteArray[count] =  (byte)(decimal & 0xFF);
            count ++;
        }
        return convertedByteArray;
    }
    

    我不知道你的意图,但字节数组也是“人类无法阅读的”。为此,有必要使用适当的十六进制到字符串的转换。但是,如果您需要十六进制到字节数组的转换,请尝试我的方法。

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      在你的 broadcastUpdate 函数中更改如下

      if (UUID_BLE_SHIELD_RX.equals(characteristic.getUuid())) {
          final byte[] rx = characteristic.getValue();
          intent.putExtra(EXTRA_DATA, rx);
      }
      

      在 MainActivity 中

      } else if (Service.ACTION_DATA_AVAILABLE.equals(action)) {
             displayData(intent.getByteArrayExtra(Service.EXTRA_DATA));
      }
      

      在 displayData 中调用函数将 byteArray 转换为 hexString

      public String bytesToHex(byte[] bytes) {
          try {
              String hex="";
              for (byte b : bytes) {
                  String st = String.format("%02X", b);
                  hex+=st;
              }
              return hex;
          } catch (Exception ex) {
              // Handler for exception
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-01-14
        • 1970-01-01
        • 2015-09-21
        • 1970-01-01
        • 2021-10-31
        • 2019-02-12
        相关资源
        最近更新 更多