蓝牙添加连接、断开、可用、不可用控制
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);
|
||||
});
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -458,7 +458,7 @@
|
||||
}
|
||||
console.log("111111",this.deviceId);
|
||||
console.log("ble==null,",ble)
|
||||
ble && ble.DropDevice(this.deviceId.id);
|
||||
ble && ble.DropDevice(null,this.deviceId.id);
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg,
|
||||
|
||||
@ -84,6 +84,7 @@
|
||||
}
|
||||
},
|
||||
onLoad() {
|
||||
this.phone= uni.getStorageSync('phone');
|
||||
if (uni.getStorageSync("token") && uni.getStorageSync("clientID")) { //免登陆
|
||||
let time = uni.getStorageSync("tokenTime");
|
||||
if (!time) {
|
||||
@ -101,7 +102,7 @@
|
||||
uni.removeStorageSync("token")
|
||||
uni.removeStorageSync("clientID")
|
||||
uni.removeStorageSync("tokenTime")
|
||||
this.phone= uni.getStorageInfoSync('phone');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@
|
||||
<text class="title">隐私协议</text>
|
||||
<uni-icons type="right" size="25" color="rgba(255, 255, 255, 0.4)" class="uniIcon"></uni-icons>
|
||||
</view>
|
||||
<view class="menu-item" @click="gotoAppStore()">
|
||||
<view class="menu-item" @click="gotoAppStore()" v-if="appVersion?true:false">
|
||||
<image src="/static/images/common/ver.png" class="icon"></image>
|
||||
<text class="title">版本更新 ({{appVersion}})</text>
|
||||
<uni-icons type="right" size="25" color="rgba(255, 255, 255, 0.4)" class="uniIcon"></uni-icons>
|
||||
|
||||
Reference in New Issue
Block a user