采用的解析逻辑是,请各位大佬指教
uni.onBLECharacteristicValueChange((res) => {
var result2 = "";
var hex = ab2hex(res.value);
console.log("ab2hex转换后的值", hex);
console.log("16进制转换后的值", parseInt(hex, 16));
if (hex.length >= 10) {
let valueStr2 = hex.substring(hex.length - 2, hex.length - 6);
let typeStr2 = hex.substring(hex.length, hex.length - 2);
let actValue2 = parseInt(valueStr2, 16);
console.log(actValue2);
//1234
var actStr = String(actValue2);
if (actStr.length == 1) {
actStr = "00" + actStr;
} else if (actStr.length == 2) {
actStr = "0" + actStr;
}
let res1 = actStr.slice(0, -2);
let res2 = actStr.substring(actStr.length, actStr.length - 2);
let res3 = res1 + "." + res2;
if (typeStr2 === "01") {
result2 = "-" + res3;
} else {
result2 = res3;
}
resultValue = result2;
}
})
// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
console.log(buffer, "buffer");
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ("00" + bit.toString(16)).slice(-2);
}
);
return hexArr.join("");
}