writeBLECharacteristicValue() {
function strToUTF8(str) {
let utf8 = [];
for (let i = 0; i < str.length; i++) {
let code = str.charCodeAt(i);
if (code <= 0x7F) {
utf8.push(code);
} else if (code <= 0x7FF) {
utf8.push(0xC0 | (code >> 6));
utf8.push(0x80 | (code & 0x3F));
} else if (code <= 0xFFFF) {
utf8.push(0xE0 | (code >> 12));
utf8.push(0x80 | ((code >> 6) & 0x3F));
utf8.push(0x80 | (code & 0x3F));
} else if (code <= 0x10FFFF) {
utf8.push(0xF0 | (code >> 18));
utf8.push(0x80 | ((code >> 12) & 0x3F));
utf8.push(0x80 | ((code >> 6) & 0x3F));
utf8.push(0x80 | (code & 0x3F));
}
}
return new Uint8Array(utf8);
}
var text = '您好 hello ياخشىمۇسىز';
const byteArray = strToUTF8(text);
const ESC = 0x1B;
const INIT_CMD = new Uint8Array([ESC, 0x40]);
const ALIGN_CENTER_CMD = new Uint8Array([ESC, 0x61, 0x01]);
const CUT_CMD = new Uint8Array([ESC, 0x6D]);
const fullCommand = new Uint8Array([
...INIT_CMD,
...ALIGN_CENTER_CMD,
...byteArray,
...CUT_CMD
]);
wx.writeBLECharacteristicValue({
writeType: 'write',
deviceId: this._deviceId,
serviceId: this._serviceId,
characteristicId: this._characteristicId,
value: fullCommand.buffer,
writeType: 'write',
success(res) {
console.log("命令发送成功", res);
},
fail(err) {
console.error("命令发送失败", err);
}
});
},
请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。