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

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

@ -33,10 +33,15 @@
<text class="lbl">设备名称</text>
<text class="value valueFont">{{device.deviceName}}</text>
</view>
<view class="item">
<text class="lbl">蓝牙状态</text>
<text class="value" :class="formData.bleStatu?'green':'red'">{{formData.bleStatu?'已连接':'未连接'}}</text>
</view>
<view class="item">
<text class="lbl">设备状态</text>
<text class="value valueFont">{{formData.sta_system}}</text>
</view>
<view class="item">
<text class="lbl">定位信息</text>
<view class="multiValue" @click.stop="gotoMap()">
@ -293,6 +298,7 @@
data() {
return {
Status: {
pageHide: false,
apiType: "listA",
navbar: {
icons: [{
@ -369,7 +375,7 @@
showClose: false
},
usrToggle: true,
bleLinkCnt:0
bleLinkCnt: 0
},
formData: {
battary: "", //电量
@ -391,7 +397,8 @@
staticWarn: false, //静止报警
fourGStrenth: 0, //4G信号强度
deviceId: "",
sta_system: "" //设备状态0关机1仅充电2开机未充电,3开机且充电
sta_system: "", //设备状态0关机1仅充电2开机未充电,3开机且充电
bleStatu: false
},
device: {
id: "",
@ -446,7 +453,7 @@
onUnload() {
console.log("页面卸载,释放资源");
let statusTopic = `A/${this.formData.imei?this.formData.imei:this.device.deviceImei}`;
ble.removeReceiveCallback(pagePath);
ble.removeAllCallback(pagePath);
if (mqttClient) {
mqttClient.unsubscribe(statusTopic);
mqttClient.disconnect();
@ -460,8 +467,14 @@
these = this;
recei = BleReceive.getBleReceive();
ble = BleTool.getBleTool();
console.log("670")
ble.addReceiveCallback(these.bleValueNotify);
ble.addReceiveCallback(these.bleValueNotify, pagePath);
ble.addStateBreakCallback(these.bleStateBreak, pagePath);
ble.addStateRecoveryCallback(these.bleStateRecovry, pagePath);
ble.addDisposeCallback(these.deviceDispose, pagePath);
ble.addRecoveryCallback(these.deviceRecovry, pagePath);
let eventChannel = this.getOpenerEventChannel();
eventChannel.on('detailData', function(data) {
@ -510,12 +523,17 @@
these.formData['imei'] = these.device.deviceImei;
}
these.getDetail();
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId);
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId).then(res => {
these.formData.bleStatu = true;
});
});
},
onShow() {
this.Status.pageHide=false;
},
onHide: function() {
this.Status.pageHide=true;
},
onBackPress(e) {
ble.removeReceiveCallback(pagePath);
@ -524,22 +542,92 @@
},
methods: {
previewImg(img){
if(!img){
deviceRecovry(res) {
if (this.Status.pageHide) {
return;
}
if (res.deviceId == these.formData.deviceId) {
this.formData.bleStatu = true;
setTimeout(() => {
hideLoading(these, 1000);
});
uni.showToast({
icon: 'success',
title: '蓝牙连接成功'
});
}
},
deviceDispose(res) {
if (this.Status.pageHide) {
return;
}
if (res.deviceId == these.formData.deviceId) {
this.formData.bleStatu = false;
setTimeout(() => {
hideLoading(these, 1000);
});
uni.showToast({
icon: 'fail',
title: '蓝牙连接已断开'
});
}
},
bleStateBreak() {
if (this.Status.pageHide) {
return;
}
//蓝牙适配器不可用
this.formData.bleStatu = false;
uni.showToast({
icon: 'fail',
title: '蓝牙已不可用'
})
hideLoading(this);
},
bleStateRecovry() {
console.log("蓝牙可用");
if (this.Status.pageHide) {
return;
}
console.log("蓝牙可用");
//蓝牙适配器再次可用,尝试恢复连接
showLoading(this, {
text: "蓝牙恢复可用,正在连接设备"
});
ble.LinkBlue(these.formData.deviceId).then(() => {
these.formData.bleStatu = true;
updateLoading(these, {
text: '连接成功'
});
}).catch(ex => {
updateLoading(these, {
text: ex.msg
})
}).finally(() => {
setTimeout(() => {
hideLoading(these);
}, 1000);
});
},
previewImg(img) {
if (!img) {
return;
}
uni.previewImage({
urls:[img]
urls: [img]
})
},
showBleUnConnect() {
this.Status.bleLinkCnt++;
if(this.Status.bleLinkCnt>3){
if (this.Status.bleLinkCnt > 3) {
return;
}
this.showPop({
message: "蓝牙未连接过该设备,请使用蓝牙重新添加该设备",
message: "蓝牙未连接过该设备,请使用蓝牙重新添加该设备,取消"+(4-this.Status.bleLinkCnt)+"次后不再提醒",
iconUrl: "/static/images/6155/DeviceDetail/uploadErr.png",
borderColor: "#e034344d",
buttonBgColor: "#E03434",
@ -571,7 +659,7 @@
});
},
gotoMap() {
let lnglat=lnglatConvert.wgs84_to_gcj02(this.formData.Lon,this.formData.Lat);
let lnglat = lnglatConvert.wgs84_to_gcj02(this.formData.Lon, this.formData.Lat);
this.detailData.longitude = lnglat[0];
this.detailData.latitude = lnglat[1];
uni.navigateTo({
@ -1175,6 +1263,7 @@
}
if (this.formData.SOS == type) {
console.log("111111");
type = "close";
}
@ -1210,26 +1299,28 @@
message.deviceImei = these.device.deviceImei;
let requestSend = (callback) => {
debugger;
message.isBluetooth = false;
api.sendSos(message).then((res) => {
console.log("res=>", res);
if (res && res.code == 200) {
console.log("接口发送SOS指令成功");
if (callback) {
callback();
}
return Promise.resolve({
code: 200,
msg: '操作成功'
});
if(callback){
callback();
}
} else {
return this.sendMQ(json);
}
}).then((res) => {
console.log("res=", res);
if(callback){
if (callback) {
callback();
}
}).catch((ex) => {
@ -1262,15 +1353,15 @@
dataView.setUint8(3, 0x00); // 数据长度
dataView.setUint8(4, 0x01); // 数据长度
dataView.setUint8(5, dic.ble[type]); // 数据
console.log("type=", type);
ble.sendData(f.deviceId, buffer, f.writeServiceId, f.wirteCharactId, 10).then(
() => {
console.log("蓝牙发送成功了");
hideLoading(these);
these.setBleFormData();
message.isBluetooth = true;
api.sendSos(message);
if(callback){
// api.sendSos(message);
if (callback) {
callback();
}
}).catch((ex) => {
@ -1286,41 +1377,45 @@
}
let OpenSOS = () => {
let execSos=()=>{
let execSos = () => {
these.formData.qzwarn = true; //标记为强制报警了
these.Status.staticWarn.time = 180;
this.formData.SOS = type;
let loopFunc = () => {
if (these.Status.staticWarn.inteval === null) {
return;
}
if (these.Status.staticWarn.time === 0) {
clearInterval(these.Status.staticWarn.inteval);
these.Status.staticWarn.inteval = null;
these.formData.qzwarn = false;
if(this.Status.Pop.okCallback){
these.CloseWarn(false);
these.formData.SOS = 'close';
these.CloseWarn(false);
}
return;
}
these.Status.staticWarn.time = these.Status.staticWarn
.time - 1;
if (these.Status.Pop.clickEvt == 'time' && this.Status.Pop.showPop) {
console.log("111111");
this.showQzWarn(this.Status.Pop.okCallback);
}
}
these.Status.staticWarn.inteval = setInterval(() => {
loopFunc();
}, 1000);
}
bleSendCmd(execSos);
}
@ -1329,14 +1424,16 @@
if (type == 'rb') { //红蓝报警
console.log("11111");
let sendRb = () => {
this.formData.SOS = type;
bleSendCmd();
these.formData.qzwarn = false;
this.Status.staticWarn.time = 0;
return;
}
if (these.formData.qzwarn && these.Status.staticWarn.inteval) {
if (these.formData.qzwarn && these.Status.staticWarn.inteval && these.Status.staticWarn.time) {
console.log("222222");
this.Status.Pop.okCallback=null;
these.showQzWarn(sendRb);
} else {
sendRb();
@ -2121,8 +2218,8 @@
}
let requestSend = () => {
let json = {
deviceId: this.device.id,
deviceImei: this.device.deviceImei ? this.device.deviceImei : this.formData.imei,
@ -2283,7 +2380,7 @@
setTimeout(sendNextPacket, 200);
}).catch(err => {
if (err.code == '10007') {
setTimeout(sendNextPacket,200);
setTimeout(sendNextPacket, 200);
} else {
requestSend();
}