1
0
forked from dyf/APP

修复蓝牙连接锁未释放的问题

This commit is contained in:
liub
2026-04-30 11:23:28 +08:00
parent 677586e51c
commit 6d9df4c945
24 changed files with 1225 additions and 759 deletions

View File

@ -88,33 +88,33 @@
<view class="warnnig" :class="ShakeBit?'':'displayNone'">
<view>{{ShakeBit}}</view>
<view>{{AlarmTime}}</view>
<uni-icons type="closeempty" size="16" color="#FFFFFF" @click="CloseWarn()"></uni-icons>
<uni-icons type="closeempty" size="16" color="#FFFFFF" @click.stop="CloseWarn()"></uni-icons>
</view>
<view class="lampMode">
<view class="mode " :class="{active:formData.sta_SOSGrade=='2'}" v-on:click.stop="sosSetting(2)">
<view class="mode " :class="{active:formData.sta_SOSGrade==2}" v-on:click.stop="sosSetting(2)">
<view class="leftImg">
<image v-show="formData.sta_SOSGrade!='2'" class="img" src="/static/images/670/rb.png"
<image v-show="formData.sta_SOSGrade!=2" class="img" src="/static/images/670/rb.png"
mode="aspectFit"></image>
<image v-show="formData.sta_SOSGrade=='2'" class="img" src="/static/images/670/rbActive.png"
<image v-show="formData.sta_SOSGrade==2" class="img" src="/static/images/670/rbActive.png"
mode="aspectFit"></image>
</view>
<view class="rightTxt">
<text class="bigTxt">红蓝警示</text>
<text class="smallTxt">{{formData.sta_SOSGrade=='2'?'开启':'关闭'}}</text>
<text class="smallTxt">{{formData.sta_SOSGrade==2?'开启':'关闭'}}</text>
</view>
</view>
<view class="mode " :class="{active:formData.sta_SOSGrade=='1'}" v-on:click.stop="sosSetting(1)">
<view class="mode " :class="{active:formData.sta_SOSGrade==1}" v-on:click.stop="sosSetting(1)">
<view class="leftImg">
<image v-show="formData.sta_SOSGrade!='1'" class="img" src="/static/images/670/sg.png"
<image v-show="formData.sta_SOSGrade!=1" class="img" src="/static/images/670/sg.png"
mode="aspectFit"></image>
<image v-show="formData.sta_SOSGrade=='1'" class="img" src="/static/images/670/sgActive.png"
<image v-show="formData.sta_SOSGrade==1" class="img" src="/static/images/670/sgActive.png"
mode="aspectFit"></image>
</view>
<view class="rightTxt">
<text class="bigTxt">声光报警</text>
<text class="smallTxt">{{formData.sta_SOSGrade=='1'?'开启':'关闭'}}</text>
<text class="smallTxt">{{formData.sta_SOSGrade==1?'开启':'关闭'}}</text>
</view>
</view>
@ -170,28 +170,42 @@
<view class="item">
<text class="lbl">单位</text>
<input class="value" v-model.trim="formData.company" placeholder="请输入单位" maxlength="8"
<input class="value" v-model.trim="formData.company" placeholder="请输入单位" maxlength="6"
placeholder-class="usrplace" />
</view>
<view class="item">
<text class="lbl">姓名</text>
<input class="value" v-model.trim="formData.usrname" placeholder="请输入职位" maxlength="8"
<input class="value" v-model.trim="formData.usrname" placeholder="请输入职位" maxlength="6"
placeholder-class="usrplace" />
</view>
<view class="item">
<text class="lbl">部门</text>
<input class="value" v-model.trim="formData.job" placeholder="请输入姓名" maxlength="8"
<input class="value" v-model.trim="formData.job" placeholder="请输入姓名" maxlength="6"
placeholder-class="usrplace" />
</view>
<view class="item">
<text class="lbl">ID</text>
<input class="value" v-model.trim="formData.usrid" placeholder="ID" maxlength="16"
<input class="value" v-model.trim="formData.usrid" placeholder="ID" maxlength="6"
placeholder-class="usrplace" />
</view>
</view>
<view class="usrinfo">
<view>
<text class="usrtitle fleft">发送信息</text>
<view class="btnSend fright" v-on:click.stop="sendMsg(null)">发送</view>
<view class="clear"></view>
</view>
<view>
<view class="item">
<input maxlength="16" class="value" style="text-indent: 20rpx;" v-model="formData.sendMsg"
placeholder="请输入文字" placeholder-class="usrplace" />
</view>
</view>
</view>
<BottomSlideMenuPlus :config="Status.BottomMenu" @btnClick="btnClick" @itemClick="itemClick"
@close="closeActionSheet"></BottomSlideMenuPlus>
@ -229,6 +243,7 @@
import BleTool from '@/utils/BleHelper.js'
import BleReceive from '@/utils/BleReceive';
import SendBatchData from '@/utils/SendBatchData.js';
import MqttClient from '@/utils/mqtt.js';
var batchTool = null;
var eventChannel = null;
@ -237,7 +252,7 @@
var recei = null;
var mq = null;
var pagePath = "/pages/210/HBY210";
var sosTime = null;
export default {
components: {
TextToHexV1
@ -353,10 +368,11 @@
address: "", //地址
company: "湖北消防总队", //单位
usrname: "胡红军", //姓名
job: "中队长", //职位
company: "", //单位
usrname: "", //姓名
job: "", //职位
usrid: "", //id
sendMsg: '',
deviceId: "",
@ -371,7 +387,8 @@
sta_RadarType: "",
sta_ShakeBit: 0, //报警状态
sta_InfraRed: 0,
textLines: ['湖北星汉文化', '有限', '公司', '12345678']
textLines: ['湖北星汉文化', '有限', '公司', '12345678'],
sta_BreakNews:''
},
device: {
id: "",
@ -457,22 +474,22 @@
return '开机充电中'
}
},
RadarType(){
let f=this.Status.BottomMenu.menuItems.find(v=>{
return v.value==this.formData.sta_RadarType;
RadarType() {
let f = this.Status.BottomMenu.menuItems.find(v => {
return v.value == this.formData.sta_RadarType;
});
if(f){
if (f) {
return f.text;
}
return '关闭';
}
},
created() {
this.Status.navbar.height = uni.getSystemInfoSync().statusBarHeight + 20;
this.Status.navbar.height = uni.getSystemInfoSync().statusBarHeight + 44;
},
onUnload() {
console.log("页面卸载,释放资源");
@ -502,7 +519,7 @@
},
onLoad() {
this.initWatch();
these = this;
recei = BleReceive.getBleReceive();
ble = BleTool.getBleTool();
@ -547,16 +564,19 @@
});
if (!f) {
these.showBleUnConnect();
// these.getDetail();
these.getDetail();
console.log("初始 sta_SOSGrade 值:", these.formData.sta_SOSGrade);
return;
}
let form = f.formData;
if (form) {
let keys = Object.keys(form);
for (var i = keys.length; i >= 0; i--) {
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (key && form[key] != undefined) {
these.formData[key] = form[key];
}
these.formData[key] = form[key];
}
}
if (these.formData['imei'] && !these.device.deviceImei) {
@ -564,7 +584,7 @@
} else if (these.device.deviceImei) {
these.formData['imei'] = these.device.deviceImei;
}
// these.getDetail();
these.getDetail();
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
@ -573,11 +593,42 @@
these.formData.bleStatu = 'err';
MsgError("连接错误:" + ex.msg, "确定", these);
});
console.log("初始 sta_SOSGrade 值:", these.formData.sta_SOSGrade);
});
},
methods: {
initWatch(){
this.$watch('formData.sta_BreakNews',(newVal,oldVal)=>{
if(newVal && newVal=='I get it'){
MsgSuccess("用户已确认收到紧急通知", "确定", these);
setTimeout(() => {
these.formData.sta_BreakNews = "";
}, 10);
}
});
this.$watch("formData.sta_PowerPercent", (newVal, oldVal) => {
console.log("电量发生变化");
if (newVal <= 20 && (this.formData.sta_system === 2 || this.formData.sta_system === 0)) {
//电量在20%及以及下,且是未充电状态提醒
showPop({
message: "设备电量低于20%,请充电",
borderColor: "#e034344d",
buttonBgColor: "#E03434",
buttonText: '确定',
buttonTextColor: '#FFFFFFde',
showCancel: false,
showHeader: false,
visibleClose: true,
textColor: '#E03434',
iconUrl: '/static/images/common/path.png'
}, this);
}
});
},
showAction(type) {
if (type == 'BottomMenu') {
this.Status.BottomMenu.show = true;
@ -591,7 +642,7 @@
} else {
this.Status.PictureMenu.show = true;
this.Status.BottomMenu.show = false;
this.Status.PictureMenu.activeIndex=0;
this.Status.PictureMenu.activeIndex = 0;
}
},
@ -1153,13 +1204,13 @@
},
btnClick() {
let f = this.getDevice();
if (!f) {
this.showBleUnConnect();
return;
}
// if (!f) {
// this.showBleUnConnect();
// return;
// }
this.Status.BottomMenu.show = false;
let d = this.Status.BottomMenu.menuItems[this.Status.BottomMenu.activeIndex];
let bleSend = () => {
let json = {
@ -1167,20 +1218,25 @@
};
json = JSON.stringify(json);
if (ble) {
if (ble && f) {
ble.sendString(f.deviceId, json).then(res => {
console.log("ble send success")
this.formData.sta_RadarType = d.value;
}).catch(ex => {
this.mqSend(json);
this.mqSend(json).then(res => {
console.log("ble send success")
this.formData.sta_RadarType = d.value;
});
});
} else {
this.mqSend(json).then(res => {
console.log("ble send success")
this.formData.sta_RadarType = d.value;
});
}
}
bleSend();
},
itemClick(item, index) {
this.Status.BottomMenu.activeIndex = index;
@ -1210,201 +1266,20 @@
if (!batchTool) {
batchTool = new SendBatchData(these, f, ble);
}
batchTool.SendUsr(4);
batchTool.SendUsr(4);
return;
// let toByteValue = (value) => {
// if (typeof value === 'number') {
// return value;
// }
// if (typeof value === 'string') {
// if (value.startsWith('0x') || value.startsWith('0X')) {
// return parseInt(value, 16);
// }
// return parseInt(`0x${value}`, 16);
// }
// return 0;
// }
// let err = false;
// this.formData.textLines.find((txt) => {
// if (txt.length === 0) {
// console.log("txt=", txt);
// err = true;
// return true;
// }
// return false;
// })
// if (err) {
// MsgError('单位、部门、姓名、ID必须填写', '', these);
// return;
// }
// showLoading(these, {
// text: "请稍候..."
// });
// //握手
// let holdHand = () => {
// return new Promise((resolve, reject) => {
// updateLoading(these, {
// text: '设备准备中...'
// })
// let hexs = 'word transmit start';
// ble.sendString(f.deviceId, hexs, f.writeServiceId, f.wirteCharactId, 10).then(res => {
// setTimeout(() => {
// resolve(res)
// }, 2200);
// }).catch(ex => {
// reject(ex)
// });
// });
// }
// //画图
// let drawText = () => {
// return new Promise((resolve, reject) => {
// this.$refs.textToHex.drawAndGetPixels().then(allPixels => {
// if (!allPixels) {
// reject("文本初始化失败");
// return;
// }
// resolve(allPixels);
// }).catch(compEx => {
// reject(compEx);
// });
// });
// }
// //发送3个分包的数据
// let task = (allPixels) => {
// try {
// let combinedData = [];
// for (let i = 0; i < this.formData.textLines.length; i++) {
// let linePixels = (allPixels[i] || []).flat(Infinity).map(item =>
// typeof item === 'string' ? toByteValue(item) : item
// );
// for (var j = linePixels.length; j < 256; j++) {
// linePixels.push(0x00);
// }
// combinedData.push([0xFA, 0x06, i + 1, 1].concat(linePixels.slice(0, 128)));
// combinedData.push([0xFA, 0x06, i + 1, 2].concat(linePixels.slice(128, 256)));
// }
// combinedData.push([0x74, 0x72, 0x61, 0x6E, 0x73, 0x6D, 0x69, 0x74, 0x20, 0x63, 0x6F, 0x6D,
// 0x70, 0x6C, 0x65, 0x74, 0x65
// ]);
// let curr = 1;
// let len = combinedData.length;
// console.log(combinedData);
// showLoading(these, {
// text: '正在发送' + (curr - 1) + '/' + len
// });
// //分包发送
// let sendPacket = () => {
// if (combinedData.length === curr - 1) {
// these.setBleFormData();
// MsgSuccess("发送成功", '', these);
// let json = {
// deviceId: these.device.id,
// name: these.formData.textLines[1],
// position: these.formData.textLines[0],
// unitName: these.formData.textLines[2],
// code: ""
// };
// usrApi.sendUsr(json);
// hideLoading(these);
// return;
// }
// let array = combinedData[curr - 1];
// ble.sendHexs(f.deviceId, array, f.writeServiceId, f.wirteCharactId, 10).then(
// res => {
// updateLoading(these, {
// text: '正在发送' + curr + '/' + len
// });
// curr++;
// setTimeout(sendPacket, 300);
// }).catch(err => {
// if (err.code == '10007') {
// setTimeout(sendPacket, 300);
// } else {
// console.log("err:", err);
// hideLoading(these);
// MsgError("发送失败: " + (err.msg || err.message), '', these)
// }
// });
// }
// setTimeout(sendPacket, 0);
// // 5. 发送成功处理
// } catch (ex) {
// hideLoading(these);
// MsgError("发送失败: " + (ex.msg || ex.message), '', these);
// }
// }
// setTimeout(() => {
// Promise.allSettled([holdHand(), drawText()]).then(results => {
// console.log("results=", results)
// if (results[0].status == 'rejected') {
// updateLoading(these, {
// text: results[0].reason
// });
// setTimeout(() => {
// hideLoading(these)
// }, 500);
// return;
// }
// if (results[1].status == 'rejected') {
// updateLoading(these, {
// text: results[1].reason
// })
// setTimeout(() => {
// hideLoading(these)
// }, 500);
// return;
// }
// task(results[1].value);
// })
// }, 0);
},
toggleSetting(val, type) {
let f = this.getDevice();
if (!f) {
this.showBleUnConnect();
return;
}
// if (!f) {
// this.showBleUnConnect();
// return;
// }
let sta_type = "sta_" + type;
let newVal = null;
if (val == this.formData[sta_type]) {
@ -1419,13 +1294,17 @@
let json = {};
json['ins_' + type] = newVal
json = JSON.stringify(json);
if (ble) {
if (ble && f) {
ble.sendString(f.deviceId, json).then(res => {
console.log("send success")
this.formData[sta_type] = newVal;
}).catch(ex => {
this.mqSend(json);
});
} else {
this.mqSend(json).then(res => {
this.formData[sta_type] = newVal;
});
}
}
@ -1437,12 +1316,12 @@
CloseWarn() {
let f = this.getDevice();
// #ifdef APP
if (!f) {
this.showBleUnConnect();
return;
}
// #endif
// // #ifdef APP
// if (!f) {
// this.showBleUnConnect();
// return;
// }
// // #endif
// #ifdef WEB
f = {
@ -1454,8 +1333,8 @@
let json = {
ins_ShakeBit: 0
};
json = JSON.stringify(json);
if (ble) {
if (ble && f) {
ble.sendString(f.deviceId, json).then(res => {
this.formData.sta_ShakeBit = 0;
this.formData.sta_InfraRed = 0;
@ -1464,49 +1343,52 @@
this.formData.sta_ShakeBit = 0;
this.formData.sta_InfraRed = 0;
});
} else {
this.mqSend(json);
this.formData.sta_ShakeBit = 0;
this.formData.sta_InfraRed = 0;
}
}
MsgError('确定解除报警?', '', these, bleSend);
},
sosSetting(val) {
let f = this.getDevice();
if (!f) {
this.showBleUnConnect();
return;
}
console.log("old=", this.formData.sta_SOSGrade);
let f = these.getDevice();
let newVal = null;
if (val == this.formData.sta_SOSGrade) {
if (val == these.formData.sta_SOSGrade) {
newVal = 0;
} else {
newVal = val;
}
console.log("old1=", this.formData.sta_SOSGrade);
let msg = "";
if (newVal == 0 && this.formData.sta_SOSGrade == 1) {
if (newVal == 0 && these.formData.sta_SOSGrade == 1) {
msg = "确定解除声光报警?";
} else if (newVal == 1) {
msg = "确定开启声光报警?";
}
let bleSend = () => {
console.log("old2=", this.formData.sta_SOSGrade);
let json = {
ins_SOSGrade: newVal
};
json = JSON.stringify(json);
if (ble) {
ble.sendString(f.deviceId, json).then(res => {
this.formData.sta_SOSGrade = newVal;
}).catch(ex => {
this.mqSend(json);
this.formData.sta_SOSGrade = newVal;
console.log("json="+JSON.stringify(json));
these.formData.sta_SOSGrade = newVal;
console.log("new=", this.formData.sta_SOSGrade);
these.setBleFormData();
console.log("these.formData.sta_SOSGrade=" + these.formData.sta_SOSGrade);
if (ble && f) {
ble.sendString(f.deviceId, json).catch(ex => {
these.mqSend(json);
});
} else {
these.mqSend(json);
}
}
};
if (msg) {
MsgError(msg, '', these, () => {
@ -1515,7 +1397,25 @@
} else {
bleSend();
}
},
getDetail() {
var that = this;
usrApi.getDetail(this.device.id).then(res => {
console.log("获取人员信息=", res);
if (res && res.code == 200) {
res = res.data;
let personnelInfo = res.personnelInfo;
if (personnelInfo) {
that.formData.company = personnelInfo.unitName;
that.formData.usrname = personnelInfo.name;
that.formData.job = personnelInfo.position;
that.formData.usrid = personnelInfo.code
return;
}
}
});
},
getDevice: function() {
let f = ble.data.LinkedList.find((v) => {
@ -1556,22 +1456,178 @@
},
mqSend(json) {
if (mq) {
mq.publish("B/" + this.device.deviceImei, json).then(res => {
let keys = Object.keys(JSON.parse(json));
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let state = key.replace('ins_', 'sta_');
this.formData[state] = json[key];
}
}).catch(ex => {
sendMsg() {
debugger;
if (!this.permissions.includes('4') && this.Status.apiType !== 'listA') {
MsgError('无操作权限', '确定', these);
return;
}
this.formData.sendMsg.padStart(16, ' ');
this.formData.textLines = [this.formData.sendMsg.slice(0, 8), this.formData.sendMsg.slice(8, 16)];
showLoading(this, {
text: "发送中"
});
//握手
let holdHand = (hexs, time) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(true)
}, time);
});
}
//画图
let drawText = () => {
return new Promise((resolve, reject) => {
this.$refs.textToHex.drawAndGetPixels().then(allPixels => {
if (!allPixels) {
reject("文本初始化失败");
return;
}
console.log("画图成功");
resolve(allPixels);
}).catch(compEx => {
reject(compEx);
});
});
}
//发送3个分包的数据
let task = (allPixels) => {
try {
let combinedData = [];
for (let i = 0; i < 2; i++) {
let linePixels = (allPixels[i] || []).flat(Infinity).map(item =>
item.replace('0x', '')
);
for (var j = linePixels.length; j < 256; j++) {
linePixels.push("00");
}
combinedData.push(['0' + (i + 1), '01'].concat(linePixels.slice(0, 128)));
combinedData.push(['0' + (i + 1), '02'].concat(linePixels.slice(128, 256)));
}
let curr = 1;
let len = combinedData.length;
console.log("包数量", len)
//分包发送
let sendPacket = () => {
if (combinedData.length === curr - 1) {
Common.saveDeviceLog({
deviceId: this.device.id,
name: '发送紧急通知',
sendMsg: this.formData.sendMsg.trim()
}).catch(ex => {
});
holdHand('transmit complete', 200).then(res => {
MsgSuccess("消息发送成功", "确定", these);
});
hideLoading(these);
return;
}
let array = combinedData[curr - 1];
let json = {
ins_msg: array.join("")
}
this.mqSend(json).then(
res => {
curr++;
console.log("发送成功", curr);
setTimeout(sendPacket, 500);
}).catch(err => {
if (err.code == '10007') {
setTimeout(sendPacket, 500);
} else {
console.log("err:", err);
MsgError('发送失败' + (err.msg || err.code), '确定', these);
}
});
}
setTimeout(sendPacket, 0);
// 5. 发送成功处理
} catch (ex) {
uni.showModal({
title: '提示',
content: '发送失败' + (ex.msg || ex.code)
})
}
}
setTimeout(() => {
Promise.allSettled([holdHand('word transmit start', 2200), drawText()]).then(
results => {
console.log("PromiseAll=", results);
if (results[0].status == 'rejected') {
setTimeout(() => {
hideLoading(these)
}, 500);
return;
}
if (results[1].status == 'rejected') {
console.log("任务1失败了", results[1].reason);
return;
}
console.log("开始发送数据包");
task(results[1].value);
})
}, 0);
},
mqSend(json) {
return new Promise((resolve, reject) => {
if (mq) {
mq.publish("B/" + this.device.deviceImei, typeof(json)=='string'?json:JSON.stringify(json)).then(res => {
json=typeof(json)=='string'?JSON.parse(json):json
let keys = Object.keys(json);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let state = key.replace('ins_', 'sta_');
this.formData[state] = json[key];
}
resolve(res);
}).catch(ex => {
reject(ex);
});
} else {
reject("mq未初始化");
}
});
},
showBleUnConnect() {
MsgError("蓝牙未连接过该设备,请使用蓝牙重新添加该设备", '去连接', this, () => {
@ -1628,6 +1684,7 @@
this.receivData(data);
},
receivData(data) { //处理蓝牙/MQ的数据
console.log("data=", data);
let keys = Object.keys(data);
keys.forEach(key => {
this.formData[key] = data[key];
@ -1978,8 +2035,8 @@
}
.mode .leftImg .img {
width: 70rpx;
height: 70rpx;
width: 55rpx;
height: 55rpx;
}
.mode .rightTxt {