修复蓝牙连接锁未释放的问题
This commit is contained in:
@ -768,7 +768,7 @@ class BleHelper {
|
||||
} catch (convertException) {
|
||||
if (str && (str.trim().startsWith('{') || str.trim().startsWith('['))) {
|
||||
console.error("JSON解析失败(可能是格式错误的数据)", convertException);
|
||||
|
||||
|
||||
}
|
||||
console.error("错误的数据", str);
|
||||
}
|
||||
@ -916,12 +916,12 @@ class BleHelper {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// console.log("蓝牙连接状态变化了", res);
|
||||
console.log("蓝牙连接状态变化了", res);
|
||||
|
||||
clearTimeout(stateTimeout);
|
||||
stateTimeout = setTimeout(() => {
|
||||
if (!res.connected) {
|
||||
// console.error("蓝牙已断开", res);
|
||||
console.error("蓝牙已断开", res);
|
||||
let f = this.data.LinkedList.find((
|
||||
v) => {
|
||||
if (v.deviceId == res
|
||||
@ -940,15 +940,18 @@ class BleHelper {
|
||||
if (!fdis) {
|
||||
if (this.data.connectingDevices[res.deviceId]) {
|
||||
console.log(`设备 ${res.deviceId} 已经在连接中,跳过自动重试`);
|
||||
return;
|
||||
} else {
|
||||
res.device = f.device;
|
||||
console.log("蓝牙状态可用,尝试1次恢复连接,", f.deviceId);
|
||||
this.LinkBlue(res.deviceId, f
|
||||
.writeServiceId, f
|
||||
.wirteCharactId, f
|
||||
.notifyCharactId, 1).catch(ex => {
|
||||
console.error(ex.msg);
|
||||
});
|
||||
}
|
||||
|
||||
res.device = f.device;
|
||||
console.log("蓝牙状态可用,尝试5次恢复连接,", f.deviceId);
|
||||
this.LinkBlue(res.deviceId, f
|
||||
.writeServiceId, f
|
||||
.wirteCharactId, f
|
||||
.notifyCharactId, 5);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1010,6 +1013,19 @@ class BleHelper {
|
||||
});
|
||||
if (f) {
|
||||
|
||||
if (item.advertisData) {
|
||||
let bytes = new Uint8Array(item.advertisData);
|
||||
let hex = Array.from(bytes).map(b => b.toString(16).toUpperCase().padStart(2, '0'))
|
||||
.join(':');
|
||||
let reg = /^([0-9a-f]{2}[:-]){5}[0-9a-f]{2}$/i;
|
||||
if (reg.test(hex)) {
|
||||
item.advertisData = hex;
|
||||
} else {
|
||||
item.advertisData = '';
|
||||
}
|
||||
}
|
||||
item.name = item.name?.replace(/\r\n/g, '').replace(
|
||||
/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
|
||||
// console.log("发现目标设备:", item);
|
||||
arr.push(item);
|
||||
}
|
||||
@ -1577,17 +1593,42 @@ class BleHelper {
|
||||
|
||||
//连接某个设备
|
||||
LinkBlue(deviceId, targetServiceId, writeCharId, notifyCharId, maxRetries) {
|
||||
|
||||
//连接成功的回调
|
||||
let LinkedCallback=() => {
|
||||
if (this.cfg.recoveryCallback.length > 0) {
|
||||
this.cfg.recoveryCallback.forEach((
|
||||
c) => {
|
||||
try {
|
||||
c.callback({
|
||||
deviceId: deviceId,
|
||||
connected: true
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"执行蓝牙恢复连接的回调出现异常,",
|
||||
error)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (this.data.platform == 'web') {
|
||||
LinkedCallback();
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
if (this.data.connectingDevices[deviceId]) {
|
||||
console.error("连接任务进行中,本次连接被阻断")
|
||||
return Promise.resolve(false);
|
||||
return Promise.reject({
|
||||
code: -1,
|
||||
msg: '正在连接中,请稍候...'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
this.data.connectingDevices[deviceId] = true;
|
||||
|
||||
if (maxRetries === undefined) {
|
||||
if (!maxRetries) {
|
||||
maxRetries = 0; // 最大重试次数
|
||||
}
|
||||
if (!writeCharId) {
|
||||
@ -1608,33 +1649,37 @@ class BleHelper {
|
||||
return false;
|
||||
});
|
||||
////console.log("findex=" + fIndex);
|
||||
|
||||
|
||||
var these = this;
|
||||
|
||||
|
||||
//连接设备
|
||||
var linkDevice = () => {
|
||||
// 添加重试次数限制
|
||||
|
||||
|
||||
let retryCount = 0;
|
||||
|
||||
|
||||
const connect = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
|
||||
if (fIndex > -1 && f?.Linked) {
|
||||
// console.log("当前已连接,跳过其他步骤");
|
||||
console.log("当前已连接,释放连接锁");
|
||||
delete this.data.connectingDevices[deviceId];
|
||||
resolve(false);
|
||||
LinkedCallback();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!this.data.available) {
|
||||
console.error("蓝牙不可用,释放连接锁");
|
||||
reject(this.getError({
|
||||
code: 10001
|
||||
}));
|
||||
delete this.data.connectingDevices[deviceId];
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
console.log("正在连接" + deviceId);
|
||||
uni.createBLEConnection({
|
||||
deviceId: deviceId,
|
||||
@ -1643,8 +1688,8 @@ class BleHelper {
|
||||
//释放连接锁
|
||||
|
||||
delete this.data.connectingDevices[deviceId];
|
||||
console.log("新连接成功", this.data.LinkedList);
|
||||
|
||||
console.error("新连接成功释放连接锁", deviceId);
|
||||
LinkedCallback();
|
||||
// 处理 MTU 设置
|
||||
if (plus.os.name === 'Android') {
|
||||
this.setMtu(deviceId).catch(ex => {
|
||||
@ -1666,7 +1711,7 @@ class BleHelper {
|
||||
if (fIndex > -1) {
|
||||
this.data.LinkedList[fIndex].Linked = true;
|
||||
this.data.LinkedList[fIndex].linkId =
|
||||
linkId;
|
||||
linkId;
|
||||
} else {
|
||||
this.data.LinkedList.push(cr);
|
||||
}
|
||||
@ -1683,22 +1728,7 @@ class BleHelper {
|
||||
// console.log("LinkedList=", this.data
|
||||
// .LinkedList);
|
||||
|
||||
//执行连接成功的回调
|
||||
if (this.cfg.recoveryCallback.length > 0) {
|
||||
this.cfg.recoveryCallback.forEach((
|
||||
c) => {
|
||||
try {
|
||||
c.callback({
|
||||
deviceId: deviceId,
|
||||
connected: true
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"执行蓝牙恢复连接的回调出现异常,",
|
||||
error)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
resolve(true);
|
||||
@ -1722,6 +1752,7 @@ class BleHelper {
|
||||
}, 1000); // 延迟1秒后重试
|
||||
} else {
|
||||
//释放连接锁
|
||||
console.error("连接失败,释放连接锁", deviceId)
|
||||
delete this.data.connectingDevices[deviceId];
|
||||
reject(ex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user