Commit 8983e0c5 authored by martin hou's avatar martin hou

fix: 截断不完整的蓝牙设备名称的UTF8编码内容

parent a1ed6bce
......@@ -603,6 +603,56 @@ function Jensen(log, conn) {
this.registerHandler = function (cmdid, handler) {
handlers[cmdid] = handler;
};
this.truncateIncompleteUtf8 = function(uint8arr) {
const bytes = uint8arr;
let end = bytes.length;
// 从后面向前,看多少个 continuation bytes (0b10xxxxxx)
let continuationBytes = 0;
// 向前扫描最多 3 个字节(UTF-8 最长是 4 字节字符)
for (let i = bytes.length - 1; i >= Math.max(0, bytes.length - 4); i--) {
const byte = bytes[i];
if ((byte & 0b1100_0000) === 0b1000_0000) {
// 是 continuation byte
continuationBytes++;
continue;
} else {
// 遇到一个不是 continuation byte 的字节
const lead = byte;
let expectedLength = 1;
if ((lead & 0b1111_1000) === 0b1111_0000) {
expectedLength = 4;
} else if ((lead & 0b1111_0000) === 0b1110_0000) {
expectedLength = 3;
} else if ((lead & 0b1110_0000) === 0b1100_0000) {
expectedLength = 2;
} else if ((lead & 0b1000_0000) === 0) {
expectedLength = 1;
} else {
// 无效 lead byte,可以认为不完整
expectedLength = continuationBytes + 1;
}
// 如果从这个 lead 到末尾的字节数 < 期待长度,说明不完整
const actualLength = (bytes.length - 1 - i) + 1; // 包括 lead 自己
if (actualLength < expectedLength) {
// 截断到 lead 字节之前
end = i;
}
break;
}
}
if (end < bytes.length) {
// 返回截断后的子数组
return bytes.subarray(0, end);
} else {
// 整体是完整的
return bytes;
}
}
}
function Command(id) {
......@@ -1518,7 +1568,7 @@ Jensen.registerHandler(BLUETOOTH_SCAN, (msg) => {
mac.push(m.length == 1 ? '0' + m : m);
}
devices.push({
name: decoder.decode(sname),
name: decoder.decode(this.truncateIncompleteUtf8(sname)),
mac: mac.join('-')
});
}
......@@ -1543,7 +1593,7 @@ Jensen.registerHandler(BT_GET_PAIRED_DEV_LIST, (msg) => {
}
let seq = msg.body[k++] & 0xff;
devices.push({
name: decoder.decode(sname),
name: decoder.decode(this.truncateIncompleteUtf8(sname)),
mac: mac.join('-'),
sequence: seq
});
......@@ -1572,7 +1622,7 @@ Jensen.registerHandler(BLUETOOTH_STATUS, (msg) => {
return {
status: 'connected',
mac: mac.join('-'),
name: decoder.decode(sname),
name: decoder.decode(this.truncateIncompleteUtf8(sname)),
a2dp: (msg.body[i++] & 0xff) == 1,
hfp: (msg.body[i++] & 0xff) == 1,
avrcp: (msg.body[i++] & 0xff) == 1,
......@@ -1637,7 +1687,7 @@ Jensen.registerHandler(BT_DEV_LIST, (msg) => {
let rssi = msg.body[k++] & 0xff;
let cod = ((msg.body[k++] & 0xff) << 16) | ((msg.body[k++] & 0xff) << 8) | (msg.body[k++] & 0xff);
devices.push({
name: decoder.decode(sname),
name: decoder.decode(this.truncateIncompleteUtf8(sname)),
mac: mac.join('-'),
rssi: rssi,
cod: cod
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment