1
0
forked from dyf/APP

蓝牙小优化,App隐藏时断开设备连接

This commit is contained in:
liub
2026-03-16 15:20:18 +08:00
parent 7512b7211c
commit ede41f68fd
21 changed files with 417 additions and 227 deletions

View File

@ -95,7 +95,7 @@
<view style="padding-bottom: 20rpx;">
<view style="padding-bottom: 20rpx;">
<view class="proinfo lamp">
@ -355,6 +355,15 @@
},
onShow() {
this.Status.pageHide = false;
let f = this.getDevice();
if (f) {
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
computed: {
@ -363,14 +372,14 @@
methods: {
LighSetting(item, index) {
let val=item.key;
let val = item.key;
if (item.key === this.formData.sta_LedType) {
val='led_off';
val = 'led_off';
}
let f = this.getDevice();
// #ifdef APP-PLUS
if (!f) {
this.showBleUnConnect();
@ -382,7 +391,7 @@
deviceId: '12345'
}
// #endif
let json = {
ins_LedType: val
}
@ -390,10 +399,10 @@
ble.sendString(f.deviceId, json, f.writeServiceId, f.wirteCharactId, 30).then(res => {
this.formData.sta_LedType = val;
these.setBleFormData();
}).catch(ex => {
this.showMsg(ex.msg);
});
},
@ -501,13 +510,13 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if(res.device){
if (res.device) {
these.formData.bleStatu = 'connecting';
}else{
} else {
this.formData.bleStatu = false;
}
setTimeout(() => {

View File

@ -519,7 +519,7 @@
ble = BleTool.getBleTool();
this.$watch("formData.sta_battery", (newVal, oldVal) => {
if (newVal <= 20 && this.formData.sta_system==2) {
if (newVal <= 20 && this.formData.sta_system == 2) {
this.showMsg("设备电量低");
}
});
@ -595,6 +595,15 @@
},
onShow() {
this.Status.pageHide = false;
let f = this.getDevice();
if (f) {
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
computed: {
getbleStatu() {
@ -1197,9 +1206,9 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if (res.device) {
these.formData.bleStatu = 'connecting';

View File

@ -509,6 +509,16 @@
},
onShow() {
this.Status.pageHide = false;
let f = this.getDevice();
if (f) {
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
this.getLinkedCnt();
},
computed: {
@ -975,13 +985,13 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if(res.device){
if (res.device) {
these.formData.bleStatu = 'connecting';
}else{
} else {
this.formData.bleStatu = false;
}
setTimeout(() => {

View File

@ -574,6 +574,15 @@
},
onShow() {
this.Status.pageHide = false;
let f=this.getDevice();
if(f){
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
computed: {
getbleStatu() {
@ -1083,9 +1092,9 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if(res.device){
these.formData.bleStatu = 'connecting';

View File

@ -599,9 +599,9 @@ import request, { baseURL } from '@/utils/request.js';
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
//if (this.Status.pageHide) {
// return;
//}
if (res.deviceId == these.formData.deviceId) {
if(res.device){
these.formData.bleStatu = 'connecting';

View File

@ -679,7 +679,7 @@
}
let that = this;
uni.navigateTo({
url: "/pages/ImgCrop/ImgCrop",
url: "/pages/common/ImgCrop/ImgCrop",
events: {
ImgCutOverPath: function(data) {
that.selectedImage = data.picPath;

View File

@ -486,6 +486,15 @@
},
onShow() {
this.Status.pageHide = false;
let f=this.getDevice();
if(f){
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
onHide: function() {
this.Status.pageHide = true;
@ -1145,7 +1154,7 @@
sourceType: ['album'],
success: function(res) {
uni.navigateTo({
url: "/pages/ImgCrop/ImgCrop",
url: "/pages/common/ImgCrop/ImgCrop",
events: {
ImgCutOver: function(data) {
@ -2012,9 +2021,9 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if (res.device) {
these.formData.bleStatu = 'connecting';

View File

@ -433,6 +433,14 @@
},
onShow() {
this.Status.pageHide = false;
let f = this.getDevice();
if (f) {
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
computed: {
@ -689,9 +697,9 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
//if (this.Status.pageHide) {
// return;
//}
if (res.deviceId == these.formData.deviceId) {
this.formData.bleStatu = false;
setTimeout(() => {

View File

@ -426,6 +426,15 @@
},
onShow() {
this.Status.pageHide = false;
let f=this.getDevice();
if(f){
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
onBackPress(e) {
@ -581,9 +590,9 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if(res.device){
these.formData.bleStatu = 'connecting';
@ -909,7 +918,7 @@
sourceType: ['album'],
success: function(res) {
uni.navigateTo({
url: "/pages/ImgCrop/ImgCrop",
url: "/pages/common/ImgCrop/ImgCrop",
events: {
ImgCutOver: function(data) {

View File

@ -396,6 +396,15 @@
},
onShow() {
this.Status.pageHide = false;
let f=this.getDevice();
if(f){
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
onBackPress(e) {
@ -562,9 +571,9 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if(res.device){
these.formData.bleStatu = 'connecting';
@ -926,7 +935,7 @@
sourceType: ['album'],
success: function(res) {
uni.navigateTo({
url: "/pages/ImgCrop/ImgCrop",
url: "/pages/common/ImgCrop/ImgCrop",
events: {
ImgCutOver: function(data) {

View File

@ -331,7 +331,7 @@
var ble = null;
var rec = null;
var pagePath = "pages/6170/BJQ6170";
var pagePath = "/pages/6170/deviceControl/index";
export default {
components: {
@ -498,9 +498,12 @@
if (receive.deviceId !== this.formData.deviceId) {
return;
}
let data = recei.ReceiveData(receive, device, pagePath, recArr);
let data = rec.ReceiveData(receive, device, pagePath, recArr);
console.log("蓝牙收到消息", data)
this.receivData(data);
},
receivData(data) {
},
bleStateBreak() {
if (this.Status.pageHide) {
@ -542,9 +545,9 @@
});
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if (res.device) {
these.formData.bleStatu = 'connecting';
@ -856,20 +859,26 @@
})
uni.hideLoading();
}
}).catch(ex => {
console.error("ex=", ex);
});
}
if (ble && this.itemInfo.deviceMac) {
console.log("111111")
let f = this.getDevice();
if (f) {
console.log("222222");
let json = {
instruct: [1, 0, 0, 0, 0]
};
json.instruct[1] = parseInt(selectedItem.instructValue);
ble.sendString(f.deviceId, json).then(res => {
console.log("ble send success")
this.lightModeA = false;
}).catch(ex => {
console.error("ble send fail,mqsending....")
mqSend();
});
} else {
@ -979,7 +988,7 @@
}
let that = this;
uni.navigateTo({
url: "/pages/ImgCrop/ImgCrop",
url: "/pages/common/ImgCrop/ImgCrop",
events: {
ImgCutOver: function(data) {
that.selectedImage = data.picPath;
@ -990,6 +999,9 @@
ev.eventChannel.emit('checkImg', {
data: res.tempFiles[0].path,
});
},
fail(ex) {
console.error("跳转失败", ex);
}
});
}
@ -1797,7 +1809,7 @@
onLoad(options) {
these = this;
rec = BleReceive.getBleReceive();
const eventChannel = this.getOpenerEventChannel();
// 监听 'detailData' 事件,获取传过来的数据
uni.showLoading({
@ -1815,7 +1827,7 @@
// 根据 apiType 设置右图标的显示状态
this.isRightIconVisible = this.apiType === 'listA';
this.initBle();
// 初始化并连接MQTT
this.mqttClient = new MqttClient();
this.mqttClient.connect(() => {
@ -1896,6 +1908,9 @@
rec = BleReceive.getBleReceive();
ble = BleTool.getBleTool();
this.initBle();
console.log("ble=", ble);
let f = ble.data.LinkedList.find((v) => {
if (v.macAddress == this.itemInfo.deviceMac) {
console.log("找到设备了", v);
@ -1908,9 +1923,9 @@
if (f) {
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(
res => {
these.formData.bleStatu = true;
});
res => {
these.formData.bleStatu = true;
});
}
}
@ -1922,6 +1937,16 @@
})
},
onShow() {
let f = this.getDevice();
if (f) {
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
onUnload() {
// 页面卸载时断开MQTT连接
if (this.mqttClient) {

View File

@ -414,6 +414,14 @@ import request, { baseURL } from '@/utils/request.js';
},
onShow() {
this.Status.pageHide = false;
let f = this.getDevice();
if (f) {
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
computed: {
@ -613,9 +621,9 @@ import request, { baseURL } from '@/utils/request.js';
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
//if (this.Status.pageHide) {
// return;
//}
if (res.deviceId == these.formData.deviceId) {
if(res.device){
these.formData.bleStatu = 'connecting';

View File

@ -418,6 +418,15 @@ import request, { baseURL } from '@/utils/request.js';
},
onShow() {
this.Status.pageHide = false;
let f=this.getDevice();
if(f){
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
computed: {
getbleStatu(){
@ -510,9 +519,9 @@ import request, { baseURL } from '@/utils/request.js';
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if(res.device){
these.formData.bleStatu = 'connecting';
@ -1051,7 +1060,7 @@ import request, { baseURL } from '@/utils/request.js';
var gotoCutImg = (imgPath) => {
uni.navigateTo({
url: "/pages/ImgCrop/ImgCrop",
url: "/pages/common/ImgCrop/ImgCrop",
events: {
ImgCutOver: function(data) {
// console.log("我收到裁剪后的图片了,感谢老铁," + data)

View File

@ -16,7 +16,7 @@
<view class="eq" :style="{marginTop:Status.navbar.height+'px'}">
<view class="eq" >
<view class="leftImg" @click.stop="previewImg(device.devicePic?device.devicePic:formData.img)">
<image class="img" :src="device.devicePic?device.devicePic:formData.img" mode="aspectFit"></image>
</view>
@ -561,6 +561,15 @@
},
onShow() {
this.Status.pageHide = false;
let f=this.getDevice();
if(f){
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
onHide: function() {
this.Status.pageHide = true;
@ -634,9 +643,9 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if(res.device){
these.formData.bleStatu = 'connecting';
@ -1871,7 +1880,7 @@
var gotoCutImg = (imgPath) => {
uni.navigateTo({
url: "/pages/ImgCrop/ImgCrop",
url: "/pages/common/ImgCrop/ImgCrop",
events: {
ImgCutOver: function(data) {
// console.log("我收到裁剪后的图片了,感谢老铁," + data)

View File

@ -243,7 +243,7 @@
],
activeIndex: -1,
bgColor: '#2a2a2a',
itemBgColor: '#00000000',
itemBgColor: '#3a3a3a',
textColor: '#ffffffde',
textAlign: 'flex-start',
title: '主灯模式',
@ -376,6 +376,15 @@
},
onShow: function() {
this.Status.pageHide = false;
let f=this.getDevice();
if(f){
these.formData.bleStatu = 'connecting';
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
console.log("连接成功")
these.formData.bleStatu = true;
});
}
},
computed: {
getbleStatu() {
@ -399,22 +408,20 @@
return txt;
}
switch (this.formData.mode) {
case 0:
case 1:
txt = "强光模式";
break;
case 1:
case 2:
txt = "弱光模式";
break;
case 2:
case 3:
txt = "爆闪模式";
break;
case 3:
txt = "关闭";
break;
default:
txt = "关闭";
break;
@ -481,9 +488,9 @@
})
},
deviceRecovry(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
this.formData.bleStatu = true;
// 重新连接后状态以设备上报为准
@ -498,9 +505,9 @@
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
// if (this.Status.pageHide) {
// return;
// }
if (res.deviceId == these.formData.deviceId) {
if (res.device) {
these.formData.bleStatu = 'connecting';
@ -587,7 +594,7 @@
}
let json = recei.ReceiveData(receive, device, path, recArr);
console.log("收到消息:", receive.hexs);
console.log("收到消息:", receive);
if (!json) {
return;
}
@ -597,6 +604,8 @@
these.formData[key] = json[key];
}
});
these.formData.mode=parseInt(receive.hexs[2],16);
console.error("mode="+these.formData.mode);
if ('statu' in json) {
const chargingVal = json.statu;
const isCharging = chargingVal === 1 || chargingVal === '1' || chargingVal === true ||
@ -783,7 +792,7 @@
sourceType: ['album'],
success: function(res) {
uni.navigateTo({
url: "/pages/ImgCrop/ImgCrop",
url: "/pages/common/ImgCrop/ImgCrop",
events: {
ImgCutOver: function(data) {
//将8位的二进制数组转换成16进制数据
@ -1023,21 +1032,16 @@
case 0:
if (type == 'main') {
dataValue = 0x01;
} else if (type == 'fu') {
dataValue = 0x04;
}
break;
case 1:
dataValue = 0x02;
break;
// case 2:
// dataValue = 0x02;
// break;
case 2:
dataValue = 0x03;
break;
@ -1071,7 +1075,7 @@
});
ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId, 100).then(() => {
this.formData.mode = mode;
this.formData.mode = mode+1;
this.setBleFormData();
}).catch((ex) => {
these.showPop({
@ -1088,7 +1092,7 @@
},
handleItemClick(item, index) {
debugger;
this.Status.BottomMenu.activeIndex = index;

View File

@ -104,6 +104,7 @@
</view>
</BottomSlideMenuPlus>
<MsgBox ref="msgPop" />
<global-loading ref="loading" />
</view>
</template>
@ -115,7 +116,15 @@
showLoading,
hideLoading,
updateLoading
} from '@/utils/loading.js'
} from '@/utils/loading.js';
import {
MsgSuccess,
MsgError,
MsgClose,
MsgWarning,
showPop,
MsgClear
} from '@/utils/MsgPops.js';
const pagePath = "pages/common/addBLE/addEquip";
var ble = null;
var these = null;
@ -490,24 +499,17 @@
console.log('定位权限已授予');
resolve(true);
} else {
console.warn('定位权限被拒绝');
uni.showModal({
title: '权限提醒',
content: '为了正常扫描蓝牙设备,需要您开启定位权限',
showCancel: false,
success: () => {
uni.openSetting(); // 引导用户去设置页
}
});
// console.warn('定位权限被拒绝');
MsgClear(these);
showPop({headerTxt:'权限提醒',message:'扫描蓝牙设备,需要您开启定位权限',buttonText:'去开启',okCallback:uni.openSetting},these,true);
resolve(false);
}
},
(error) => {
console.error('请求定位权限失败:', error);
uni.showToast({
title: '权限请求异常',
icon: 'none'
});
MsgError('请求定位权限失败:'+error.code,'确定',these);
resolve(false);
}
);
@ -537,7 +539,7 @@
}
}
showLoading(these, {
text: '正在刷新'
text: '正在扫描蓝牙设备'
})
let time = null;
let startSearch = () => {
@ -558,10 +560,9 @@
if (err.code === 10001) {
these.showOpenSetting();
} else {
uni.showModal({
title: '提示',
content: '出现错误:' + err.msg
});
MsgClear(these);
MsgError('出现错误:' + err.msg,'确定',these);
}
}).finally(() => {
@ -571,7 +572,9 @@
}
ble.StopSearch().finally(startSearch);
ble.StopSearch().catch(err=>{
console.error("err=",err);
}).finally(startSearch);
@ -806,9 +809,9 @@
linkCallback(res);
}).catch(ex => {
console.error("ex=", ex)
uni.showModal({
content: "连接失败:" + ex.msg
});
MsgClear(these);
MsgError("连接失败:" + ex.msg,'确定',these);
hideLoading(these);
});