Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
J
jensen
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Skye Yu
jensen
Commits
8983e0c5
Commit
8983e0c5
authored
Nov 25, 2025
by
martin hou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 截断不完整的蓝牙设备名称的UTF8编码内容
parent
a1ed6bce
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
4 deletions
+54
-4
jensen.js
src/utils/jensen.js
+54
-4
No files found.
src/utils/jensen.js
View file @
8983e0c5
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment