蓝牙添加连接、断开、可用、不可用控制
This commit is contained in:
@ -46,14 +46,14 @@
|
||||
var these = null;
|
||||
var eventChannel = null;
|
||||
var ble = null;
|
||||
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
Statu: {
|
||||
bound: null,
|
||||
timeInteval:null,
|
||||
isSearch:false
|
||||
timeInteval: null,
|
||||
isSearch: false
|
||||
},
|
||||
device: {
|
||||
"deviceId": "",
|
||||
@ -64,12 +64,12 @@
|
||||
"advertisServiceUUIDs": [
|
||||
|
||||
],
|
||||
"linkStatu": false,
|
||||
"linkStatu": true,
|
||||
"macAddress": "",
|
||||
"communicationMode":"",
|
||||
"imei":""
|
||||
"communicationMode": "",
|
||||
"imei": ""
|
||||
},
|
||||
serverDevice:null
|
||||
serverDevice: null
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
@ -83,51 +83,84 @@
|
||||
return "red";
|
||||
}
|
||||
},
|
||||
deviceStatu:function(){
|
||||
if(!this.device.name){
|
||||
deviceStatu: function() {
|
||||
if (!this.device.name) {
|
||||
return "";
|
||||
}
|
||||
if(!this.device.macAddress){
|
||||
if (!this.device.linkStatu) {
|
||||
return "蓝牙未连接";
|
||||
}
|
||||
if (!this.device.macAddress) {
|
||||
return '等待设备上报Mac地址';
|
||||
}
|
||||
|
||||
if(!this.device.deviceName ){
|
||||
if(this.Statu.isSearch){
|
||||
|
||||
if (!this.device.deviceName) {
|
||||
if (this.Statu.isSearch) {
|
||||
return "无效设备";
|
||||
}else{
|
||||
if(this.Statu.timeInteval===null){
|
||||
} else {
|
||||
if (this.Statu.timeInteval === null) {
|
||||
return "等待查询设备"
|
||||
}else{
|
||||
} else {
|
||||
return "正在查询设备";
|
||||
}
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
return "找到有效设备"
|
||||
}
|
||||
|
||||
|
||||
return "";
|
||||
}
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
console.log("返回时断开蓝牙连接,取消订阅");
|
||||
|
||||
ble.removeReceiveCallback(pagePath);
|
||||
console.log("返回取消订阅");
|
||||
|
||||
ble.removeAllCallback(pagePath);
|
||||
},
|
||||
onLoad(option) {
|
||||
these = this;
|
||||
ble = bleTool.getBleTool();
|
||||
console.log("LinkBle")
|
||||
|
||||
ble.addStateBreakCallback(res => {
|
||||
these.device.linkStatu = false;
|
||||
hideLoading(these);
|
||||
uni.showToast({
|
||||
icon:'fail',
|
||||
title:'蓝牙已不可用'
|
||||
})
|
||||
},pagePath);
|
||||
|
||||
ble.addStateRecoveryCallback(res => {
|
||||
if (these.device.deviceId) {
|
||||
showLoading(these, {
|
||||
text: '蓝牙已恢复正在连接设备'
|
||||
});
|
||||
|
||||
these.reLinkdevice();
|
||||
}
|
||||
},pagePath);
|
||||
ble.addDisposeCallback(res => {
|
||||
console.log("处理蓝牙断开连接");
|
||||
these.device.linkStatu = false;
|
||||
if (these.device.deviceId == res.deviceId) {
|
||||
|
||||
showLoading(these, {
|
||||
text: '蓝牙已断开,正在重连'
|
||||
});
|
||||
these.reLinkdevice();
|
||||
}
|
||||
},pagePath);
|
||||
|
||||
ble.addReceiveCallback((receive, f, path) => {
|
||||
console.log("收到设备消息,", receive);
|
||||
if (these.device.deviceId == receive.deviceId) {
|
||||
console.log("11111:",receive);
|
||||
these.device.imei=f.imei;
|
||||
console.log("11111:", receive);
|
||||
these.device.imei = f.imei;
|
||||
if (receive.bytes[0] == 0xFC || receive.str.indexOf('mac address:') == 0) {
|
||||
if (f && f.macAddress) {
|
||||
these.device.macAddress = f.macAddress;
|
||||
console.log("222222");
|
||||
these.Statu.isSearch=false;
|
||||
console.log("222222");
|
||||
these.Statu.isSearch = false;
|
||||
these.initDevice();
|
||||
}
|
||||
}
|
||||
@ -157,19 +190,41 @@
|
||||
}
|
||||
})
|
||||
|
||||
let inteval = setInterval(this.initDevice, 10000);
|
||||
let inteval = setInterval(this.initDevice, 10000);
|
||||
},
|
||||
methods: {
|
||||
|
||||
reLinkdevice() {
|
||||
ble.LinkBlue(these.device.deviceId).then(res => {
|
||||
updateLoading(these, {
|
||||
text: '连接成功'
|
||||
});
|
||||
these.device.linkStatu = true;
|
||||
setTimeout(() => {
|
||||
hideLoading(these);
|
||||
}, 2000);
|
||||
}).catch(ex => {
|
||||
updateLoading(these, {
|
||||
text: ex.msg
|
||||
});
|
||||
setTimeout(() => {
|
||||
hideLoading(these);
|
||||
}, 2000);
|
||||
});
|
||||
},
|
||||
initDevice: function() {
|
||||
if(!these.device.macAddress){
|
||||
if (!these.device.macAddress) {
|
||||
return;
|
||||
}
|
||||
|
||||
//无效的设备回调
|
||||
let deviceInvalid = () => {
|
||||
|
||||
these.$set(these.device, "deviceName", "");
|
||||
|
||||
}
|
||||
clearTimeout(this.Statu.timeInteval);
|
||||
|
||||
this.Statu.timeInteval = setTimeout(() => {
|
||||
this.Statu.isSearch=true;
|
||||
this.Statu.isSearch = true;
|
||||
showLoading(these, {
|
||||
text: '正在获取设备信息'
|
||||
});
|
||||
@ -181,45 +236,34 @@
|
||||
deviceMac: these.device.macAddress
|
||||
}
|
||||
}).then(res => {
|
||||
|
||||
|
||||
console.log("获取设备信息", res);
|
||||
if (res && res.code == 200) {
|
||||
console.log("res=", res);
|
||||
let data = res.data;
|
||||
this.serverDevice=data;
|
||||
this.serverDevice = data;
|
||||
if (data) {
|
||||
let keys = Object.keys(data);
|
||||
ble.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == these.device.deviceId) {
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
// if(!v.device){
|
||||
// v.device={};
|
||||
// }
|
||||
// v.device[key] = data[key];
|
||||
// console.log("key="+key);
|
||||
// console.log("value="+data[key]);
|
||||
|
||||
|
||||
let key = keys[i];
|
||||
these.$set(these.device, key, data[key]);
|
||||
|
||||
}
|
||||
|
||||
// ble.setBleData();
|
||||
}
|
||||
});
|
||||
|
||||
// console.log("device=", these.device);
|
||||
// console.log("LinkedList=", ble.data.LinkedList);
|
||||
|
||||
|
||||
} else {
|
||||
deviceInvalid();
|
||||
}
|
||||
} else {
|
||||
deviceInvalid();
|
||||
}
|
||||
|
||||
}).catch((ex) => {
|
||||
console.error("获取设备出现异常:", ex);
|
||||
}).finally(() => {
|
||||
this.Statu.timeInteval=null;
|
||||
this.Statu.isSearch=false;
|
||||
this.Statu.timeInteval = null;
|
||||
this.Statu.isSearch = false;
|
||||
hideLoading(these);
|
||||
});
|
||||
|
||||
@ -242,7 +286,7 @@
|
||||
these.Statu.boundRemark = "设备上报Mac地址异常";
|
||||
return;
|
||||
}
|
||||
if(!this.device.deviceName){
|
||||
if (!this.device.deviceName) {
|
||||
these.Statu.bound = false;
|
||||
these.Statu.boundRemark = "设备未入库";
|
||||
return;
|
||||
@ -267,26 +311,26 @@
|
||||
if (res.code == 200) {
|
||||
these.Statu.bound = true;
|
||||
these.Statu.boundRemark = "设备绑定成功!";
|
||||
let data=these.serverDevice;
|
||||
let data = these.serverDevice;
|
||||
let keys = Object.keys(data);
|
||||
ble.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == these.device.deviceId) {
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
if(!v.device){
|
||||
v.device={};
|
||||
if (!v.device) {
|
||||
v.device = {};
|
||||
}
|
||||
v.device[key] = data[key];
|
||||
v.device[key] = data[key];
|
||||
}
|
||||
|
||||
ble.updateCache();
|
||||
|
||||
ble.updateCache();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
ble.removeReceiveCallback(pagePath);
|
||||
clearTimeout(this.Statu.timeInteval);
|
||||
this.device.macAddress=null;
|
||||
this.Statu.timeInteval=null;
|
||||
this.device.macAddress = null;
|
||||
this.Statu.timeInteval = null;
|
||||
uni.$emit("refreshDeviceList");
|
||||
setTimeout(() => {
|
||||
uni.switchTab({
|
||||
@ -300,7 +344,7 @@
|
||||
}).catch((ex) => {
|
||||
these.Statu.bound = false;
|
||||
these.Statu.boundRemark = '出现了未知的异常,操作失败';
|
||||
console.log("ex=",ex);
|
||||
console.log("ex=", ex);
|
||||
}).finally(() => {
|
||||
hideLoading(this);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user