Android从蓝牙数据中获取byteValue

1 android bytearray bluetooth-lowenergy

当读取蓝牙传感器特性时,值或测量值存储在特征值中.我怎样才能获得字节值:

00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
Run Code Online (Sandbox Code Playgroud)

从传感器值?

/ ******** 编辑 *********** /

@Vikram推荐以下内容:

        private void updateAdc3Values(BluetoothGattCharacteristic characteristic) {

        // Convert values if needed
        //double adc3 = SensorTagData.extractAdc3(characteristic);


/******** Used to get raw HEX value ********/   

        byte[] ADCValue3 = characteristic.getValue();
        String adc3Hex = ADCValue3.toString()
                .replace("[", "")   //remove the right bracket
                .replace("]", ""); //remove the left bracket

//      Log.e("ADC3", "ADC CH3 characteristicvalue from TEST is " + adc3Hex);
//      Log.i("ADC3", "ADC Last 6CH3 characteristicvalue from TEST is " + adc3Hex.substring(adc3Hex.length() - 6));  //Prints last 6 of this string

        // Get UUID
        String ch3 = (String.valueOf(characteristic.getUuid()));
        String ch3UUID = ch3.substring(0, Math.min(ch3.length(), 8));
//      Log.d("ADC3", "ADC FIRST 6CH3 characteristicvalue from TEST is " + ch3.substring(0, Math.min(ch3.length(), 8)));  //Print first 6 of this string


        String adc3hex6 = adc3Hex.substring(adc3Hex.length() - 6);

        StringBuilder sb = new StringBuilder(); 
        for (byte b : ADCValue3) { 
            if (sb.length() > 0) { 
                sb.append(':'); 
                } 
        sb.append(String.format("%02x", b)); }


        Log.w("ADC3", "StringBuilder " + sb);

/********  Used to get raw HEX value   ********/        


    }
Run Code Online (Sandbox Code Playgroud)

我基本上一直在使用不同类型的输出,以试验所有可能性.使用byte []特征值,我正在形成不同类型的输出.无视UUID,我们只是证明价值来自正确的渠道.所以我只是切断了UUID以获得前6个字符,它证明了它来自的频道.

在上面的例子中,Log.i以ADC3字节数组值开头,该行在一次测量中记录的值为:

B@42bc0738
Run Code Online (Sandbox Code Playgroud)

然后在StringBuilder之后的姐妹日志记录:

7a:17:d0:7f:ff:ff:21:b4:e1:80:00:00:a4:a6:f4:77:73:5e
Run Code Online (Sandbox Code Playgroud)

这似乎是我正在寻找的正确的格式和价值.现在是时候比较价值了.

Vik*_*ram 5

从评论中转移:

OP正在获取byte[]from BluetoothGattCharacteristic#getValue()方法.

要以所需格式格式化代码,请使用以下代码段:

// Use StringBuilder so that we can go through the byte[] 
// and append formatted text
StringBuilder sb = new StringBuilder(); 

for (byte b : byteArray) { 
    if (sb.length() > 0) { 
        sb.append(':'); 
    } 

    sb.append(String.format("%02x", b)); 
}
Run Code Online (Sandbox Code Playgroud)