1
0
forked from dyf/APP

蓝牙添加连接、断开、可用、不可用控制

This commit is contained in:
liub
2025-10-17 11:52:26 +08:00
parent 0ce4f4bef0
commit b114f6690d
13 changed files with 3024 additions and 407 deletions

View File

@ -32,7 +32,9 @@
</view>
<view class="centertxt ">
<view class="name" v-text="item.name"></view>
<view class="id" v-text="item.deviceId"></view>
<view class="id">
<text>信号:{{110+item.RSSI>100?100:110+item.RSSI}}%</text>
</view>
</view>
<view class="rightIco center">
<image src="/static/images/BLEAdd/linked.png" class="img" mode="aspectFit">
@ -63,7 +65,7 @@
</view>
<view class="id">
<text>信号强度:{{110+item.RSSI>100?100:110+item.RSSI}}%</text>
<text>信号:{{110+item.RSSI>100?100:110+item.RSSI}}%</text>
</view>
</view>
@ -116,6 +118,7 @@
data() {
return {
Status: {
isPageHidden: false,
intval: null,
time: null,
BottomMenu: {
@ -156,13 +159,14 @@
},
onHide: function() {
this.Status.isPageHidden = true;
ble.StopSearch();
},
onUnload() {
ble.StopSearch();
ble.removeDeviceFound(pagePath);
ble.removeReceiveCallback(pagePath);
ble.removeAllCallback(pagePath);
},
onLoad(option) {
let search = option.search;
@ -203,14 +207,86 @@
let StartSubsrib = () => {
these.EquipMents=[];
these.EquipMents = [];
ble = bleTool.getBleTool();
//蓝牙不可用的回调
ble.addStateBreakCallback(res=>{
if (these.Status.isPageHidden) {
return;
}
console.log("处理蓝牙不可用");
hideLoading(these);
these.PairEquip=[];
these.EquipMents=[];
uni.showToast({
icon:'fail',
title:'蓝牙已不可用'
});
these.showOpenSetting();
},pagePath);
//蓝牙再次可用的回调
ble.addStateRecoveryCallback(res=>{
if (these.Status.isPageHidden) {
return;
}
uni.showToast({
icon:'success',
title:'蓝牙恢复可用'
});
these.Status.BottomMenu.show = false;
these.EquipMents=[];
these.refreshBleList();
},pagePath);
//蓝牙断开连接的回调
ble.addDisposeCallback(res => {
if (these.Status.isPageHidden) {
return;
}
console.log("处理蓝牙断开连接");
hideLoading(these);
these.PairEquip.find(function(v, ind) {
these.PairEquip.splice(ind, 1);
return v.deviceId == res.deviceId;
});
}, pagePath);
//蓝牙连接已恢复的回调
ble.addRecoveryCallback(res => {
if (these.Status.isPageHidden) {
return;
}
// hideLoading(these);
these.EquipMents.find(function(v, ind) {
if (v.deviceId == res.deviceId) {
these.PairEquip.push(v);
return true;
}
return false;
});
if (these.device) {
clearInterval(this.Status.intval);
showLoading(these, {
text: '蓝牙连接已恢复,正在验证设备'
});
setTimeout(() => {
these.DeviceVerdict(res.deviceId);
}, 0);
} else {
hideLoading(these);
}
}, pagePath);
//搜索到新设备的回调
ble.addDeviceFound((arr) => {
if (these.Status.isPageHidden) {
return;
}
arr = arr.devices;
for (var i = 0; i < arr.length; i++) {
@ -246,8 +322,11 @@
}
console.log("EquipMents=", these.EquipMents)
}, pagePath);
//收到设备的消息回调
ble.addReceiveCallback((receivData, f, path, arr) => {
if (these.Status.isPageHidden) {
return;
}
if (f.macAddress && these.device) {
clearInterval(this.Status.intval);
this.Status.intval = null;
@ -265,6 +344,7 @@
}, pagePath);
}
if (search) {
@ -281,6 +361,7 @@
},
onShow: function() {
this.Status.isPageHidden = false;
this.refreshBleList();
},
methods: {
@ -288,26 +369,37 @@
if (!ble) {
return;
}
let promis = [];
for (let index = 0; index < this.PairEquip.length; index++) {
let item = this.PairEquip[index];
promis.push(ble.disconnectDevice(item.deviceId));
ble.StopSearch().then(res => {
// console.log("停止搜索成功");
this.EquipMents = [];
ble.StartSearch().then(result => {
// console.log("开始搜索成功");
}).catch(err => {
console.error("开始搜索失败,err=", err);
if(err.code===10001){
these.showOpenSetting();
}else{
uni.showModal({
title:'提示',
content:'出现异常:'+err.msg
});
}
}
Promise.allSettled(promis).finally(() => {
ble.StopSearch().then(res => {
// console.log("停止搜索成功");
these.EquipMents = [];
these.PairEquip = [];
ble.StartSearch().then(result => {
// console.log("开始搜索成功");
}).catch(err => {
console.error("开始搜索失败,err=", err);
if (err.code === 10001) {
these.showOpenSetting();
} else {
uni.showModal({
title: '提示',
content: '出现异常:' + err.msg
});
}
});
}).catch(ex => {
console.error("ex=", ex);
});
}).catch(ex => {
console.error("ex=", ex);
});
},
isItemLink: function(item, index) {
let src = '/static/images/BLEAdd/noLink.png';
@ -348,15 +440,16 @@
});
let removeLink = () => {
ble.subScribe(deviceId, false); //取消订阅消息
ble.DropDevice(deviceId); //从缓存中删除该设备
ble.disconnectDevice(deviceId); //断开与该设备的连接
ble.DropDevice(deviceId, null); //从缓存中删除该设备并断开连接
these.device.deviceMac = "";
let index = this.PairEquip.findIndex(function(v) {
let index = these.PairEquip.findIndex(function(v) {
return v.deviceId == deviceId;
});
if (index > -1) {
this.PairEquip.splice(index, 1);
}
}
console.log("f=", f);
@ -372,24 +465,30 @@
hideLoading(these);
}, 1000)
return;
}
} else {
}
hideLoading(these);
ble.updateCache();
uni.navigateBack();
return true;
} else {
this.Status.time = 30;
showLoading(these, {
text: "等待设备上报Mac地址," + these.Status.time + 's'
});
console.log("11111111", this.Status.time);
clearInterval(this.Status.intval);
this.Status.intval = null;
this.Status.intval = setInterval(() => {
this.Status.time = this.Status.time - 1;
if (this.Status.time < 0) {
console.log("停止倒计时", this.Status.time);
clearInterval(this.Status.intval)
this.Status.intval = null;
this.Status.time = null;
@ -408,15 +507,14 @@
setTimeout(() => {
hideLoading(these)
}, 1000);
}, 1500);
}
return;
}
updateLoading(these, {
updateLoading(these, {
text: "等待设备上报Mac地址," + these.Status.time + 's'
});
}, 1000);
return undefined;
}
@ -617,22 +715,24 @@
}
@keyframes expand {
0% {
width: 0;
height: 0;
opacity: 0.8;
}
80% {
width: 18.75rem;
height: 18.75rem;
opacity: 0.2;
}
100% {
width: 0.5rem;
height: 0.5rem;
opacity: 0.5;
}
0% {
width: 0;
height: 0;
opacity: 0.8;
}
80% {
width: 18.75rem;
height: 18.75rem;
opacity: 0.2;
}
100% {
width: 0.5rem;
height: 0.5rem;
opacity: 0.5;
}
}
.mainContent {