c***@intasect.com.cn
c***@intasect.com.cn
  • 发布:2024-06-26 11:35
  • 更新:2024-12-05 23:40
  • 阅读:218

在 uni.onBLECharacteristicValueChange方法里面获取到的ArrayBuffer解析出来与蓝牙卡尺的表显有偏差

分类:uni-app

采用的解析逻辑是,请各位大佬指教

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("");
}

2024-06-26 11:35 负责人:无 分享
已邀请:
1***@qq.com

1***@qq.com - 小海

function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
ab2str(res.value)

要回复问题请先登录注册