读取蓝牙传感器特性时,该值或测量值将存储在特性值中。我如何获得这样的字节值:
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,这只是证明这些值来自正确的通道。因此,我只是剪切了UUID以获取前6个字符,这证明了它来自的频道。
在上述情况下,以ADC3字节数组值开头的Log.i,此行记录了一个测量值:
B@42bc0738
然后在StringBuilder记录之后出现姐妹记录:
7a:17:d0:7f:ff:ff:21:b4:e1:80:00:00:a4:a6:f4:77:73:5e
这似乎是我想要的正确格式和值。现在是时候比较这些值了。
最佳答案
从评论转移:
OP正在从byte[]
方法获取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));
}