gpt4 book ai didi

Android从蓝牙数据中获取byteValue

转载 作者:行者123 更新时间:2023-11-29 15:15:06 25 4
gpt4 key购买 nike

读取蓝牙传感器特性时,值或测量值存储在特性值中。我怎样才能得到这样的字节值:

00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00

来自传感器值?

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

@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 ********/


}

我基本上一直在使用不同类型的输出,来试验所有的可能性。使用 byte[] 特征值,我形成了不同类型的输出。忽略 UUID,这只是我们证明这些值来自正确的 channel 。所以我只是分割 UUID 以获得前 6 个字符,这证明了它来自的 channel 。

在上述情况下,Log.i 以 ADC3 字节数组值开头,此行记录一次测量值:

B@42bc0738

然后是StringBuilder之后的姊妹日志:

7a:17:d0:7f:ff:ff:21:b4:e1:80:00:00:a4:a6:f4:77:73:5e

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

最佳答案

转自评论:

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

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

// 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));
}

关于Android从蓝牙数据中获取byteValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com