修复蓝牙连接锁未释放的问题
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);
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ import {
|
||||
|
||||
class BleReceive {
|
||||
constructor(_ref) {
|
||||
if(_ref){
|
||||
if(_ref && !this.ref){
|
||||
this.ref = _ref;
|
||||
}
|
||||
this.StorageKey = "linkedDevices";
|
||||
|
||||
@ -282,7 +282,7 @@ export default {
|
||||
value: "46",
|
||||
label: "手动报警",
|
||||
checked: false,
|
||||
type: ['210', '102', '6170','018A']
|
||||
type: ['210', '102', '6170', '018A']
|
||||
},
|
||||
{
|
||||
value: "47",
|
||||
@ -314,7 +314,7 @@ export default {
|
||||
value: "51",
|
||||
label: "警示灯",
|
||||
checked: false,
|
||||
type: ['100', '100J','018A']
|
||||
type: ['100', '100J', '018A']
|
||||
},
|
||||
|
||||
{
|
||||
@ -600,7 +600,7 @@ export default {
|
||||
});
|
||||
|
||||
},
|
||||
MapNavi(lon, lat, name, mapType) {//打开地图路线规划页面
|
||||
MapNavi(lon, lat, name, mapType) { //打开地图路线规划页面
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!lon || !lat) {
|
||||
reject("经纬度不能为空");
|
||||
@ -609,19 +609,45 @@ export default {
|
||||
|
||||
let url = "";
|
||||
if (!mapType || mapType == 'amap') {
|
||||
let amapScheme = uni.getSystemInfoSync().platform === 'ios' ? 'iosamap://' : 'androidamap://';
|
||||
url = `${amapScheme}route/plan?sourceApplication=myapp&dlat=${lat}&dlon=${lon}&dname=${name}&dev=0&t=0`;
|
||||
let amapScheme = uni.getSystemInfoSync().platform === 'ios' ? 'iosamap://' : 'androidamap://';
|
||||
url =
|
||||
`${amapScheme}route/plan?sourceApplication=myapp&dlat=${lat}&dlon=${lon}&dname=${name}&dev=0&t=0`;
|
||||
} else {
|
||||
url =`baidumap://map/direction?destination=latlng:${lat},${lon}|name:${name}&coord_type=gcj02&src=myapp`
|
||||
url =
|
||||
`baidumap://map/direction?destination=latlng:${lat},${lon}|name:${name}&coord_type=gcj02&src=myapp`
|
||||
}
|
||||
|
||||
plus.runtime.openURL(url, (ex) => {
|
||||
console.error("ex=",ex);
|
||||
reject("无法打开地图软件"+url);
|
||||
console.error("ex=", ex);
|
||||
reject("无法打开地图软件" + url);
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
},
|
||||
|
||||
saveDeviceLog(json) { //保存设备日志
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let url = '/app/xinghan/device/saveDeviceLog';
|
||||
|
||||
request({
|
||||
url: url,
|
||||
method: 'POST',
|
||||
data:json
|
||||
}).then(res => {
|
||||
if (res && res.code == 200) {
|
||||
resolve(res);
|
||||
return;
|
||||
}
|
||||
|
||||
reject(res);
|
||||
}).catch(ex => {
|
||||
reject(ex);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -15,6 +15,7 @@ import {
|
||||
showPop
|
||||
} from '@/utils/MsgPops.js';
|
||||
import Common from '@/utils/Common.js';
|
||||
import api from '@/api/670/HBY670.js'
|
||||
|
||||
|
||||
class SendBatchData {
|
||||
@ -48,6 +49,7 @@ class SendBatchData {
|
||||
console.log("开始发送分片数据");
|
||||
return new Promise((resolve, reject) => {
|
||||
if (f) {
|
||||
ble.data.voiceUploading=true;
|
||||
// 总数据包数
|
||||
var totalPackets = 1536; //36;
|
||||
|
||||
@ -62,6 +64,7 @@ class SendBatchData {
|
||||
const sendNextVideoPacket = () => {
|
||||
// console.log("准备发送一段数据");
|
||||
if (currentPacket > totalPackets) {
|
||||
ble.data.voiceUploading=false;
|
||||
if (!ReSendNo) {
|
||||
setTimeout(() => {
|
||||
|
||||
@ -149,7 +152,7 @@ class SendBatchData {
|
||||
currentPacket);
|
||||
setTimeout(sendNextVideoPacket, 800);
|
||||
} else {
|
||||
|
||||
ble.data.voiceUploading=false;
|
||||
hideLoading(these);
|
||||
|
||||
showPop({
|
||||
@ -191,7 +194,7 @@ class SendBatchData {
|
||||
|
||||
ble.sendString(f.deviceId, "video transmit start", f
|
||||
.writeServiceId, f.wirteCharactId).then(res => {
|
||||
|
||||
ble.data.voiceUploading=true;
|
||||
setTimeout(() => {
|
||||
console.log("握手成功了");
|
||||
resolve(true);
|
||||
@ -652,6 +655,14 @@ class SendBatchData {
|
||||
if (combinedData.length === curr - 1) {
|
||||
setTimeout(() => {
|
||||
holdHand('transmit complete', 200).then(res => {
|
||||
let json = {
|
||||
deviceId: these.device.id,
|
||||
name: these.formData.usrname,
|
||||
position: these.formData.job,
|
||||
unitName: these.formData.company,
|
||||
code: these.formData.usrid
|
||||
};
|
||||
api.sendUsr(json);
|
||||
MsgSuccess("人员信息发送成功", "确定", these);
|
||||
hideLoading(these);
|
||||
these.setBleFormData();
|
||||
@ -738,6 +749,11 @@ class SendBatchData {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
SendMsg(){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default SendBatchData;
|
||||
Reference in New Issue
Block a user