用的蓝牙模块是通过字符串控制的,但是怎么传进去呢,api的value 是arraybuffer,我直接用字符串转arraybuffer 也不行
string2buffer(str) {
var buf = new ArrayBuffer(str.length / 2);
var bufView = new Uint8Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = parseInt(str.slice(i * 2, i * 2 + 2), 16);
}
return buf;
},
用的就是这个方法
求教
供参考
function hexStringToArrayBuffer(str) { if (!str) { return new ArrayBuffer(0); } var buffer = new ArrayBuffer(str.length/2.0); let dataView = new DataView(buffer) let ind = 0; for (let i = 0; i < str.length; i += 2) { let code = parseInt(str.substr(i, 2), 16) dataView.setUint8(ind, code) ind++ } return buffer; } function hexCharCodeToStr(hexCharCodeStr) { let str = hexCharCodeStr.toString(16); if (str.length === 1) { str = '0' + str; } return str; }