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

@ -46,9 +46,14 @@
</view>
<view class="tab">
<view class="center tabItem" @click="Status.tabIndex=0" :class="{'active':Status.tabIndex==0}">未入库(<text class="mathIcon">{{UnJoin.length}}</text>)</view>
<view class="center tabItem" @click="Status.tabIndex=1" :class="{'active':Status.tabIndex==1}">已入库(<text class="mathIcon">{{joined.length}}</text>)</view>
<view class="center tabItem" @click="Status.tabIndex=2" :class="{'active':Status.tabIndex==2}">全部(<text class="mathIcon">{{SearchEquips.length}}</text>)</view>
<view class="center tabItem" @click="Status.tabIndex=0" :class="{'active':Status.tabIndex==0}">未入库(<text
class="mathIcon">{{UnJoin.length}}</text>)</view>
<view class="center tabItem" @click="Status.tabIndex=1" :class="{'active':Status.tabIndex==1}">已入库(<text
class="mathIcon">{{joined.length}}</text>)</view>
<view class="center tabItem" @click="Status.tabIndex=2" :class="{'active':Status.tabIndex==2}">已筛选(<text
class="mathIcon">{{SearchEquips.length}}</text>)</view>
<view class="center tabItem" @click="Status.tabIndex=3" :class="{'active':Status.tabIndex==3}">全部(<text
class="mathIcon">{{EquipMents.length}}</text>)</view>
</view>
</view>
<view class="mainContent">
@ -56,8 +61,7 @@
<view class="list searchList">
<view class="item" v-on:click="Link(item,index)" v-for="item, index in list"
v-show="!item['linkStatu']"
:class="{'displayNone':item.name.toLowerCase().indexOf(search.toLowerCase())===-1}">
v-show="!item['linkStatu']">
<view class="leftImg ">
<image src="/static/images/common/bluetooth.png" class="titleIco" mode="heightFix">
</image>
@ -68,6 +72,7 @@
<text class="fright" :class="item.link?'green':'red'">{{item.link?'已连接':'未连接'}}</text>
<view class="clear"></view>
</view>
<view class="name lbl" v-if="item.advertisData">广播数据:{{item.advertisData}}</view>
<view class="name lbl">Mac:
<text class="green" v-if="item.macAddress">{{item.macAddress}}</text>
<view class="red" v-if="!item.macAddress && item.link">
@ -141,6 +146,8 @@
var time = null;
var time1 = null;
var mq = null;
var isValidTime = null;
export default {
data() {
return {
@ -158,7 +165,7 @@
showBack: true,
height: 90
},
tabIndex:0
tabIndex: 0
},
search: '',
groupid: '',
@ -169,20 +176,103 @@
devices: [],
devicesCnt: 0,
privateNetUrl: '',
scanKey: ''
scanKey: '',
typeKeys: [{
"type_name": "BJQ6170",
"ble": "BJQ6170"
},
{
"type_name": "HBY210",
"ble": "HBY210"
},
{
"type_name": "HBY670",
"ble": "HBY670"
},
{
"type_name": "BJQ6150",
"ble": "BJQ6150"
},
{
"type_name": "HBY650",
"ble": "HBY650"
},
{
"type_name": "BJQ7305",
"ble": "BJQ7305"
},
{
"type_name": "BJQ4877",
"ble": "BJQ4877"
},
{
"type_name": "HBY100",
"ble": "HBY100"
},
{
"type_name": "BJQ6070",
"ble": "BJQ6075"
},
{
"type_name": "HBY102",
"ble": "HBY102"
},
{
"type_name": "HBY018A",
"ble": "HBY018A"
},
{
"type_name": "BJQ6107",
"ble": "BJQ6107"
},
{
"type_name": "HBY100-J",
"ble": "HBY100J"
},
{
"type_name": "XH6155",
"ble": "BJQ6155"
},
{
"type_name": "BJQ6075J",
"ble": "BJQ6075"
},
{
"type_name": "HBY008A",
"ble": "HBY008A"
},
{
"type_name": "HBY100-Y",
"ble": "HBY100Y"
},
{
"type_name": "HBY102-J",
"ble": "HBY102J"
}
]
}
},
computed: {
list(){
if(this.Status.tabIndex==0){
cType(){
let f=this.typeKeys.find(v=>{
return v.type_name==this.type.typeName;
});
return f;
},
list() {
if (this.Status.tabIndex == 0) {
return this.UnJoin;
}
if(this.Status.tabIndex==1){
if (this.Status.tabIndex == 1) {
return this.joined;
}
return this.SearchEquips;
if (this.Status.tabIndex == 2) {
return this.SearchEquips;
}
return this.EquipMents;
},
SearchEquips: function() {
let f = this.EquipMents.filter(v => {
@ -190,19 +280,19 @@
});
return f;
},
joined(){
let f=this.EquipMents.filter(v => {
joined() {
let f = this.EquipMents.filter(v => {
return v.name.toLowerCase().indexOf(this.search.toLowerCase()) > -1 && v.isUpload;
});
return f;
});
return f;
},
UnJoin(){
let f=this.EquipMents.filter(v => {
UnJoin() {
let f = this.EquipMents.filter(v => {
return v.name.toLowerCase().indexOf(this.search.toLowerCase()) > -1 && !v.isUpload;
});
return f;
});
return f;
},
},
onHide: function() {
ble.StopSearch();
@ -226,13 +316,13 @@
},
onLoad() {
these = this;
mq = MQTT.getMqTool();
mq.init();
this.groupid = common.DateFormat(new Date(), 'yyyy-MM-dd HH:mm:ss');
ble = bleTool.getBleTool();
// serv = serTool.serverInit();
@ -431,9 +521,10 @@
// these.EquipMents=[];
ble.addDeviceFound((arr) => {
// console.log("发现新设备,",arr);
arr = arr.devices;
let promis = [];
let newDevice = false;
for (var i = 0; i < arr.length; i++) {
arr[i].linkStatu = false;
@ -444,7 +535,11 @@
arr[i].name = arr[i].name.replace(/\r\n/g, '').replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,'');
if(these.cType){
if(arr[i].name.indexOf(these.cType.ble)===-1){
continue;
}
}
let f = these.EquipMents.find(function(v, index) {
if (v.deviceId == arr[i].deviceId) {
@ -455,18 +550,25 @@
});
if (!f) {
newDevice = true;
arr[i].remark = '正在校验...';
arr[i].isValid = false;
if (arr[i].advertisData) {
arr[i].macAddress = arr[i].advertisData;
}
these.EquipMents.unshift(arr[i]);
these.getDevice(arr[i]);
// console.log("EquipMents=", these.EquipMents);
}
}
these.sendEquipToMq();
// console.log("equip=", these.EquipMents)
if (newDevice) {
clearTimeout(isValidTime);
isValidTime = setTimeout(these.getDevice, 2000); //延迟执行验证
}
}, pagePath);
// console.log("addEquip")
ble.addReceiveCallback((receivData, f, path, arr) => {
@ -511,17 +613,17 @@
ble.addStateBreakCallback(() => {
MsgError('蓝牙不可用','确定',these);
MsgError('蓝牙不可用', '确定', these);
these.EquipMents.filter((v, i) => {
these.$set(these.EquipMents[i], 'link', false);
});
}, pagePath);
ble.addStateRecoveryCallback(() => {
these.ReSearch();
MsgSuccess('蓝牙恢复可用','确定',these,()=>{
MsgSuccess('蓝牙恢复可用', '确定', these, () => {
MsgClear(these);
});
},
@ -555,7 +657,15 @@
eventChannel.on('addType', function(rec) {
console.log("接收到父页面的参数:", rec);
these.type = rec.data;
let ct=these.typeKeys.find(v=>{
if(v.type_name==these.type.typeName){
these.search=v.ble;
return true;
}
return false;
});
let callback = () => {
these.getTypeDeviceCnt().then(res => {
console.error("设备数量:", res);
@ -581,32 +691,32 @@
});
// #ifdef APP|APP-PLUS
setTimeout(()=>{
MsgInfo("如需要在PC上查看此数据,请复制链接后通过微信发送到PC,在PC端打开然后点右上角扫码授权","复制链接",these,true,()=>{
setTimeout(() => {
MsgInfo("如需要在PC上查看此数据,请复制链接后通过微信发送到PC,在PC端打开然后点右上角扫码授权", "复制链接", these, true, () => {
console.log("开始访问剪切板");
uni.setClipboardData({
data:'https://static-mp-5b7c35fc-f6fe-4100-a2e1-3669e4d4bfc9.next.bspapp.com/AppTools/views/index.html',
success(){
data: 'https://static-mp-5b7c35fc-f6fe-4100-a2e1-3669e4d4bfc9.next.bspapp.com/AppTools/views/index.html',
success() {
uni.showToast({
title:'已复制链接'
title: '已复制链接'
});
plus.runtime.openURL('weixin://', (err) => {
MsgError("打开微信失败,请手动打开微信,发送至'文件传输助手'");
MsgError("打开微信失败,请手动打开微信,发送至'文件传输助手'");
});
},
fail(ex) {
console.error("ex=",ex);
console.error("ex=", ex);
}
});
});
},500);
}, 500);
// #endif
},
onShow: function() {
@ -699,120 +809,199 @@
});
},
getDevice(item) {
//从后台通过类型和名称查询设备
return new Promise((resolve, reject) => {
let f = null;
let fIndex = -1;
these.EquipMents.find(function(v, index) {
if (v.deviceId == item.deviceId) {
f = v;
fIndex = index;
return true;
getDevice() {
let task = (item) => {
return new Promise((resolve, reject) => {
let f = null;
let fIndex = -1;
these.EquipMents.find(function(v, index) {
if (v.deviceId == item.deviceId) {
f = v;
fIndex = index;
return true;
}
return false;
});
if (fIndex == -1) {
reject("检验出现异常");
console.error("fIndex异常")
return;
}
return false;
});
if (this.devicesCnt > 0) {
if (this.devices && this.devices.length > 0) {
let eqp = this.devices.find(v => {
let flag = v.bluetooth_name.replace(/\r\n/g, '').replace(
/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '') == item.name;
if (this.devicesCnt > 0) {
console.log("从缓存验证")
if (this.devices && this.devices.length > 0) {
console.log("正在从缓存验证1111")
let eqp = this.devices.find(v => {
let flag = false;
if (!item.advertisData) {
flag = v.bluetooth_name.replace(/\r\n/g, '').replace(
/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '') == item.name;
} else {
flag = v.deviceMac == item.advertisData;
}
return flag;
return flag;
});
if (eqp) {
console.log("缓存验证成功");
these.$set(these.EquipMents[fIndex], "isUpload",
true);
these.$set(these.EquipMents[fIndex], "remark",
"校验完成,设备已入库");
these.$set(these.EquipMents[fIndex], "macAddress",
eqp.device_mac);
these.$set(these.EquipMents[fIndex], "device_name", eqp.device_name);
these.$set(these.EquipMents[fIndex], 'isValid', true);
});
if (eqp) {
console.log("从缓存中找到了设备", eqp);
resolve(eqp);
return;
}
// else{
// console.error("缓存验证失败");
// }
}
// console.error("缓存中找不到此设备22222222", this.devices);
// these.$set(these.EquipMents[fIndex], "remark", "校验完成,设备未入库");
// these.$set(these.EquipMents[fIndex], 'isValid', true);
// reject(null);
// these.sendEquipToMq();
// return;
}
let succCallback = (data) => {
if (data) {
these.$set(these.EquipMents[fIndex], "isUpload",
true);
these.$set(these.EquipMents[fIndex], "remark",
"校验完成,设备已入库");
these.$set(these.EquipMents[fIndex], "macAddress",
eqp.device_mac);
these.$set(these.EquipMents[fIndex], "device_name", eqp.device_name);
console.log("从缓存中找到了设备", eqp);
resolve(eqp);
return;
}
}
// console.error("缓存中找不到此设备22222222", this.devices);
these.$set(these.EquipMents[fIndex], "remark",
"校验完成,设备未入库");
reject(null);
these.sendEquipToMq();
return;
}
let succCallback = (data) => {
if (data) {
these.$set(these.EquipMents[fIndex], "isUpload",
true);
these.$set(these.EquipMents[fIndex], "remark",
"校验完成,设备已入库");
these.$set(these.EquipMents[fIndex], "macAddress",
data.device_mac);
these.$set(these.EquipMents[fIndex], "imei", data
.device_imei);
these.$set(these.EquipMents[fIndex], "device_name", data.device_name);
} else {
console.error("设备未入库111111", data)
these.$set(these.EquipMents[fIndex], "remark",
"校验完成,设备未入库");
}
these.sendEquipToMq();
}
let errCallback = () => {
these.$set(these.EquipMents[fIndex], "remark",
"校验出现异常");
these.sendEquipToMq();
}
let p2 = new Promise((resolve, reject) => {
let json = {
"tenant_id": this.type.tenantId,
"deviceType": this.type.deviceTypeId,
"deviceName": item.name
};
request({
url: '/app/xinghan/device/GetDeviceByName',
data: json,
method: 'POST'
}).then(res => {
if (res && res.code == 200) {
resolve(res.data);
return;
data.device_mac);
these.$set(these.EquipMents[fIndex], "imei", data
.device_imei);
these.$set(these.EquipMents[fIndex], "device_name", data.device_name);
} else {
console.error("设备未入库111111", data)
these.$set(these.EquipMents[fIndex], "remark",
"校验完成,设备未入库");
}
reject(res);
}).catch(ex => {
reject(ex);
these.sendEquipToMq();
}
let errCallback = (ex) => {
if (ex === null) {
these.$set(these.EquipMents[fIndex], "remark", "校验完成,设备未入库");
} else {
these.$set(these.EquipMents[fIndex], "remark", "校验出现异常");
}
these.sendEquipToMq();
}
let p2 = new Promise((succ, err) => {
if (item.advertisData) {
console.error("通过广播数据匹配mac地址");
request({
url: '/app/device/getDeviceInfoByDeviceMac',
method: 'GET',
data: {
deviceMac: item.advertisData
}
}).then(res => {
console.log("通过广播数据验证成功:", res)
if (res && res.code == 200) {
if (res.data) {
let data = {
device_mac: res.data.deviceMac,
device_imei: res.data.deviceImei,
device_name: res.data.deviceName,
id: res.data.id,
bluetooth_name: res.data.bluetoothName
};
succ(data);
} else {
err(null);
}
return;
}
err(res);
}).catch(ex => {
err(ex);
});
} else {
//从后台通过类型和名称查询设备
console.error("通过类型和名称查询设备");
let json = {
"tenant_id": this.type.tenantId,
"deviceType": this.type.deviceTypeId,
"deviceName": item.name
};
request({
url: '/app/xinghan/device/GetDeviceByName',
data: json,
method: 'POST'
}).then(res => {
console.log("通过名称校验结果", res);
if (res && res.code == 200) {
if (res.data) {
succ(res.data);
} else {
err(null);
}
return;
}
err(res);
}).catch(ex => {
err(ex);
});
}
});
})
Promise.any([p2]).then(res => {
succCallback(res);
}).catch(ex => {
errCallback(ex);
}).finally(() => {
p2.then(res => {
succCallback(res);
resolve(res);
}).catch(ex => {
errCallback(ex);
reject(ex);
}).finally(() => {
these.$set(these.EquipMents[fIndex], 'isValid', true);
});
});
});
}
for (let i = 0; i < this.EquipMents.length; i++) {
let element = this.EquipMents[i];
if (!element.isValid) {
task(element).catch(ex => {
if (ex === null && element.advertisData) {
console.log("广播自动入库");
this.uploadItem(element);
}
});
}
}
},
scan() {
@ -835,9 +1024,9 @@
let json = getUrlParams(res.result);
if (!json.key) {
MsgError('无效的二维码','',these);
MsgError('无效的二维码', '', these);
return;
}
this.scanKey = json.key;
@ -909,27 +1098,29 @@
},
ReSearch() {
if (!ble) {
MsgError('蓝牙模块未初始化','',this);
MsgError('蓝牙模块未初始化', '', this);
return;
}
showLoading(this,{text:'请稍候...'});
showLoading(this, {
text: '请稍候...'
});
ble.disconnectDevice().finally(dis => {
ble.StopSearch().finally(res => {
this.EquipMents = [];
this.PairEquip = [];
setTimeout(()=>{
setTimeout(() => {
ble.StartSearch().then(result => {
}).catch(err => {
console.error("err=", err);
MsgError("出现错误:" + err.msg, '', these);
}).finally(()=>{
hideLoading(these);
});
},600)
}).catch(err => {
console.error("err=", err);
MsgError("出现错误:" + err.msg, '', these);
}).finally(() => {
hideLoading(these);
});
}, 600)
}).catch(ex => {
console.error("ex=", ex);
MsgError("出现错误:" + ex.msg, '', these);
@ -959,8 +1150,9 @@
method: 'post',
data: json
}).then(res => {
console.log("res=", res);
if (res && res.code == 200) {
console.log("res=", res);
this.EquipMents.find((v, index) => {
if (v.deviceId == item.deviceId) {
this.$set(this.EquipMents[index], "isUpload", true);
@ -978,22 +1170,27 @@
return false;
});
} else {
console.error("出现错误,", item);
console.error("出现错误,", this.devices);
console.error("入库错误,", item);
console.error("入库结果,", res);
this.EquipMents.find((v, index) => {
if (v.deviceId == item.deviceId) {
this.$set(this.EquipMents[index], "isUpload", false);
this.$set(this.EquipMents[index], "remark", res.msg);
if (res.msg.indexOf('已存在') > -1) {
this.$set(this.EquipMents[index], "isUpload", true);
} else {
this.$set(this.EquipMents[index], "isUpload", false);
this.devices.find((d, i) => {
if (d.device_mac == item.macAddress) {
this.$set(this.EquipMents[index],
"device_name", d
.device_name);
return true;
}
});
}
this.devices.find((d, i) => {
if (d.device_mac == item.macAddress) {
this.$set(this.EquipMents[index], "device_name", d
.device_name);
return true;
}
});
return true;
}
@ -1010,10 +1207,14 @@
},
Link: function(item) {
if (item.advertisData) {
this.uploadItem(item);
return;
}
let exec = () => {
showLoading(this, {
text: "正在连接:第1次"
text: "正在连接..."
});
let index = 1;
@ -1053,7 +1254,7 @@
return false;
});
resolve(res);
}).catch((ex) => {
if (index == total) {
@ -1079,8 +1280,8 @@
these.sendEquipToMq();
}).catch(ex => {
console.log("ex=", ex)
MsgError("连接失败"+ex.code + ex.msg,'',these);
MsgError("连接失败" + ex.code + ex.msg, '', these);
hideLoading(these);
});
}
@ -1097,22 +1298,25 @@
</script>
<style>
.tabItem.active{
border-bottom:2rpx solid #BBE600;
color:#BBE600;
}
.mathIcon{
.tabItem.active {
border-bottom: 2rpx solid #BBE600;
color: #BBE600;
}
.tab{
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-content: center;
justify-content: space-evenly;
height: 60rpx;
font-size: 28rpx;
.mathIcon {
color: #BBE600;
}
.tab {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-content: center;
justify-content: space-evenly;
height: 60rpx;
font-size: 28rpx;
}
.noLink {
text-align: center;
width: 100%;
@ -1479,7 +1683,7 @@
.topStatric {
width: 100%;
box-sizing: border-box;
padding:0rpx 30rpx;
padding: 0rpx 30rpx;
position: fixed;
top: 0rpx;
z-index: 99;
@ -1523,6 +1727,7 @@
overflow-y: scroll;
margin-top: 150rpx;
}
/* #endif */