蓝牙增加断开重连、换设备后可以去再次绑定
This commit is contained in:
@ -8,6 +8,9 @@
|
||||
<view class="deviceName">
|
||||
蓝牙名:{{device.name}}
|
||||
</view>
|
||||
<view class="deviceName">
|
||||
状态:{{deviceStatu}}
|
||||
</view>
|
||||
<view class="deviceName">
|
||||
设备名:{{device.deviceName}}
|
||||
</view>
|
||||
@ -40,12 +43,14 @@
|
||||
var these = null;
|
||||
var eventChannel = null;
|
||||
var ble = null;
|
||||
var timeInteval = null;
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
Statu: {
|
||||
bound: null
|
||||
bound: null,
|
||||
timeInteval:null,
|
||||
isSearch:false
|
||||
},
|
||||
device: {
|
||||
"deviceId": "",
|
||||
@ -57,7 +62,8 @@
|
||||
|
||||
],
|
||||
"linkStatu": false,
|
||||
"macAddress": ""
|
||||
"macAddress": "",
|
||||
"communicationMode":""
|
||||
},
|
||||
serverDevice:null
|
||||
}
|
||||
@ -72,15 +78,36 @@
|
||||
} else {
|
||||
return "red";
|
||||
}
|
||||
},
|
||||
deviceStatu:function(){
|
||||
if(!this.device.name){
|
||||
return "";
|
||||
}
|
||||
if(!this.device.macAddress){
|
||||
return '等待设备上报Mac地址';
|
||||
}
|
||||
|
||||
if(!this.device.deviceName ){
|
||||
if(this.Statu.isSearch){
|
||||
return "无效设备";
|
||||
}else{
|
||||
if(this.Statu.timeInteval===null){
|
||||
return "等待查询设备"
|
||||
}else{
|
||||
return "正在查询设备";
|
||||
}
|
||||
}
|
||||
}else{
|
||||
return "找到有效设备"
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
},
|
||||
onBackPress() {
|
||||
console.log("返回时断开蓝牙连接,取消订阅");
|
||||
ble.disconnectDevice(these.device.deviceId);
|
||||
ble.removeReceiveCallback(pagePath);
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
console.log("返回时断开蓝牙连接,取消订阅");
|
||||
ble.disconnectDevice(these.device.deviceId);
|
||||
ble.removeReceiveCallback(pagePath);
|
||||
},
|
||||
onLoad(option) {
|
||||
@ -96,6 +123,7 @@
|
||||
if (f && f.macAddress) {
|
||||
these.device.macAddress = f.macAddress;
|
||||
console.log("222222");
|
||||
these.Statu.isSearch=false;
|
||||
these.initDevice();
|
||||
}
|
||||
}
|
||||
@ -112,9 +140,9 @@
|
||||
keys.forEach((v, index) => {
|
||||
these.device[v] = f[v];
|
||||
})
|
||||
console.log("LinkedList=", ble.data.LinkedList)
|
||||
console.log("f=", f);
|
||||
console.log("获取到设备", these.device);
|
||||
// console.log("LinkedList=", ble.data.LinkedList)
|
||||
// console.log("f=", f);
|
||||
// console.log("获取到设备", these.device);
|
||||
if (f.macAddress) {
|
||||
these.device.macAddress = f.macAddress;
|
||||
|
||||
@ -130,9 +158,10 @@
|
||||
methods: {
|
||||
|
||||
initDevice: function() {
|
||||
clearTimeout(timeInteval);
|
||||
clearTimeout(this.Statu.timeInteval);
|
||||
|
||||
timeInteval = setTimeout(() => {
|
||||
this.Statu.timeInteval = setTimeout(() => {
|
||||
this.Statu.isSearch=true;
|
||||
showLoading(these, {
|
||||
text: '正在获取设备信息'
|
||||
});
|
||||
@ -144,6 +173,7 @@
|
||||
deviceMac: these.device.macAddress
|
||||
}
|
||||
}).then(res => {
|
||||
|
||||
console.log("获取设备信息", res);
|
||||
if (res && res.code == 200) {
|
||||
let data = res.data;
|
||||
@ -180,6 +210,8 @@
|
||||
}).catch((ex) => {
|
||||
console.log("获取设备出现异常:", ex);
|
||||
}).finally(() => {
|
||||
this.Statu.timeInteval=null;
|
||||
this.Statu.isSearch=false;
|
||||
hideLoading(these);
|
||||
});
|
||||
|
||||
@ -202,11 +234,16 @@
|
||||
these.Statu.boundRemark = "设备上报Mac地址异常";
|
||||
return;
|
||||
}
|
||||
if(!this.device.deviceName){
|
||||
these.Statu.bound = false;
|
||||
these.Statu.boundRemark = "设备未入库";
|
||||
return;
|
||||
}
|
||||
these.Statu.bound = null;
|
||||
these.Statu.boundRemark = "";
|
||||
showLoading(these, {
|
||||
|
||||
text: "连接中..."
|
||||
text: "绑定中..."
|
||||
})
|
||||
let promise = request({
|
||||
url: '/app/device/bind',
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
<view class="imgContent center">
|
||||
<view class="img center">
|
||||
<image src="/static/images/bluetooth.png" class="titleIco" mode="aspectFit">
|
||||
<image src="/static/images/common/bluetooth.png" class="titleIco" mode="aspectFit">
|
||||
</image>
|
||||
</view>
|
||||
|
||||
@ -27,7 +27,7 @@
|
||||
|
||||
<view class="item " v-for="item, index in PairEquip" v-show="PairEquip.length>0">
|
||||
<view class="leftImg ">
|
||||
<image src="/static/images/BLEAdd/device.png" class="titleIco" mode="aspectFit">
|
||||
<image src="/static/images/common/bluetooth.png" class="titleIco" mode="heightFix">
|
||||
</image>
|
||||
</view>
|
||||
<view class="centertxt ">
|
||||
@ -49,7 +49,7 @@
|
||||
<view class="item" v-on:click="Link(item,index)" v-for="item, index in EquipMents"
|
||||
v-show="!item['linkStatu']">
|
||||
<view class="leftImg ">
|
||||
<image src="/static/images/BLEAdd/device.png" class="titleIco" mode="aspectFit">
|
||||
<image src="/static/images/common/bluetooth.png" class="titleIco" mode="heightFix">
|
||||
</image>
|
||||
</view>
|
||||
<view class="centertxt ">
|
||||
@ -81,8 +81,8 @@
|
||||
</view>
|
||||
</view>
|
||||
</BottomSlideMenuPlus>
|
||||
|
||||
<global-loading ref="loading" />
|
||||
|
||||
<global-loading ref="loading" />
|
||||
</view>
|
||||
</template>
|
||||
|
||||
@ -94,10 +94,10 @@
|
||||
hideLoading,
|
||||
updateLoading
|
||||
} from '@/utils/loading.js'
|
||||
const pagePath="pages/common/addBLE/addEquip";
|
||||
const pagePath = "pages/common/addBLE/addEquip";
|
||||
var ble = null;
|
||||
var these = null;
|
||||
var eventChannel=null;
|
||||
var eventChannel = null;
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
@ -130,9 +130,9 @@
|
||||
},
|
||||
PairEquip: [], //已配对设备
|
||||
EquipMents: [], //搜索出来的设备
|
||||
device:null,
|
||||
item:{
|
||||
deviceId:''
|
||||
device: null,
|
||||
item: {
|
||||
deviceId: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -141,28 +141,25 @@
|
||||
},
|
||||
onHide: function() {
|
||||
ble.StopSearch();
|
||||
|
||||
|
||||
},
|
||||
onBackPress: (e) => {
|
||||
|
||||
onUnload() {
|
||||
ble.StopSearch();
|
||||
ble.removeDeviceFound(pagePath);
|
||||
ble.removeReceiveCallback(pagePath);
|
||||
},
|
||||
onUnload(){
|
||||
ble.StopSearch();
|
||||
ble.removeDeviceFound(pagePath);
|
||||
ble.removeReceiveCallback(pagePath);
|
||||
},
|
||||
onLoad() {
|
||||
these = this;
|
||||
this.EquipMents = [];
|
||||
ble = bleTool.getBleTool();
|
||||
ble.addDeviceFound((arr) => {
|
||||
|
||||
|
||||
arr = arr.devices;
|
||||
for (var i = 0; i < arr.length; i++) {
|
||||
|
||||
arr[i].linkStatu = false;
|
||||
if(!arr[i].name){
|
||||
if (!arr[i].name) {
|
||||
continue;
|
||||
}
|
||||
let f = these.EquipMents.find(function(v) {
|
||||
@ -170,51 +167,56 @@
|
||||
});
|
||||
|
||||
if (!f) {
|
||||
|
||||
|
||||
these.EquipMents.push(arr[i]);
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
console.log("equip=", these.EquipMents)
|
||||
}, pagePath);
|
||||
// console.log("addEquip")
|
||||
// ble.addReceiveCallback((receivData,f,path,arr) => {
|
||||
// console.log("收到数据了:", receivData);//数据格式:{bytes:[109,97],str:"",hexs:"FA 01"}
|
||||
// if (this.item.deviceId == receivData.deviceId) {
|
||||
// console.log("11111:",receivData);
|
||||
|
||||
|
||||
// }
|
||||
|
||||
// });
|
||||
ble.addReceiveCallback((receivData, f, path, arr) => {
|
||||
if (f.macAddress) {
|
||||
showLoading(these, {
|
||||
text: '正在验证设备'
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
these.DeviceVerdict(f.deviceId);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
|
||||
}, pagePath);
|
||||
eventChannel = this.getOpenerEventChannel();
|
||||
|
||||
|
||||
eventChannel.on('detailData', function(rec) {
|
||||
console.log("接收到父页面的参数:",rec);
|
||||
these.device=rec.data;
|
||||
});
|
||||
console.log("接收到父页面的参数:", rec);
|
||||
these.device = rec.data;
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
onShow: function() {
|
||||
|
||||
|
||||
this.EquipMents=[];
|
||||
this.PairEquip=[];
|
||||
ble.StartSearch().catch((ex) => {
|
||||
|
||||
|
||||
this.EquipMents = [];
|
||||
this.PairEquip = [];
|
||||
ble && ble.StartSearch().catch((ex) => {
|
||||
if (ex.code == 10001) {
|
||||
these.showOpenSetting();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
},
|
||||
methods: {
|
||||
isItemLink: function(item, index) {
|
||||
let src = '/static/images/BLEAdd/noLink.png';
|
||||
|
||||
|
||||
if (this.PairEquip && this.PairEquip.length) {
|
||||
if (this.PairEquip.length > 0) {
|
||||
let f = this.PairEquip.find(function(v) {
|
||||
@ -229,7 +231,7 @@
|
||||
}
|
||||
return src;
|
||||
},
|
||||
|
||||
|
||||
|
||||
showOpenSetting: function() {
|
||||
this.Status.BottomMenu.show = true;
|
||||
@ -238,57 +240,121 @@
|
||||
this.Status.BottomMenu.show = false;
|
||||
ble.showBlueSetting(false);
|
||||
},
|
||||
Link: function(item, index) {
|
||||
this.item.deviceId=item.deviceId;
|
||||
showLoading(this,{
|
||||
text: "正在连接"
|
||||
});
|
||||
setTimeout(() => {
|
||||
let serviceid=null;
|
||||
if(item.advertisServiceUUIDs.length>0){
|
||||
serviceid=item.advertisServiceUUIDs[0];
|
||||
}
|
||||
ble.LinkBlue(item.deviceId,serviceid).then((res) => {
|
||||
let c = these.PairEquip.find(function(v) {
|
||||
return v.deviceId == item.deviceId;
|
||||
});
|
||||
if (!c) {
|
||||
|
||||
these.PairEquip.push(item);
|
||||
DeviceVerdict(deviceId) { //判断是否是目标设备
|
||||
if (these.device) { //从设备详情过来的,回设备详情去
|
||||
let f = ble.data.LinkedList.find(v => {
|
||||
if (v.deviceId == deviceId) {
|
||||
v.device = these.device;
|
||||
return true;
|
||||
}
|
||||
console.log("连接成功");
|
||||
if(these.device){//从设备详情过来的,回设备详情去
|
||||
ble.data.LinkedList.find(v=>{
|
||||
if(v.deviceId==item.deviceId){
|
||||
v.device=these.device;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
ble.updateCache();
|
||||
uni.navigateBack();
|
||||
return false;
|
||||
});
|
||||
if (f.macAddress) {
|
||||
|
||||
|
||||
if (f.macAddress != these.device.deviceMac) {
|
||||
ble.disconnectDevice(deviceId);
|
||||
updateLoading(these, {
|
||||
text: "设备Mac地址错误,请重选设备连接"
|
||||
})
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
hideLoading(these);
|
||||
|
||||
ble.updateCache();
|
||||
uni.navigateBack();
|
||||
return true;
|
||||
} else {
|
||||
updateLoading(these, {
|
||||
text: "等待设备上报Mac地址"
|
||||
})
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
Link: function(item) {
|
||||
this.item.deviceId = item.deviceId;
|
||||
showLoading(this, {
|
||||
text: "正在连接:第1次"
|
||||
});
|
||||
|
||||
let index = 1;
|
||||
let total = 5;
|
||||
|
||||
let linkCallback = (res) => {
|
||||
let c = these.PairEquip.find(function(v) {
|
||||
return v.deviceId == item.deviceId;
|
||||
});
|
||||
if (!c) {
|
||||
|
||||
these.PairEquip.push(item);
|
||||
}
|
||||
console.log("连接成功", these.device);
|
||||
if (!these.device) {
|
||||
console.log("跳转到绑定")
|
||||
hideLoading(these);
|
||||
uni.navigateTo({
|
||||
url:"/pages/common/addBLE/LinkBle",
|
||||
events:{
|
||||
|
||||
url: "/pages/common/addBLE/LinkBle",
|
||||
events: {
|
||||
|
||||
},
|
||||
success(res) {
|
||||
|
||||
res.eventChannel.emit('LinkItem', item);
|
||||
|
||||
res.eventChannel.emit('LinkItem', item);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
console.log("验证设备")
|
||||
these.DeviceVerdict(item.deviceId);
|
||||
}
|
||||
let execLink = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
|
||||
if (index > total) {
|
||||
reject({
|
||||
msg: "连接超时"
|
||||
});
|
||||
return;
|
||||
}
|
||||
ble.LinkBlue(item.deviceId).then((res) => {
|
||||
console.log("连接成功");
|
||||
|
||||
resolve(res);
|
||||
}).catch((ex) => {
|
||||
if (index == total) {
|
||||
console.log("连接了N次都没连上");
|
||||
reject(ex);
|
||||
return;
|
||||
}
|
||||
index++;
|
||||
updateLoading(this, {
|
||||
text: ex.msg + ",正在重试第" + index + "次"
|
||||
})
|
||||
execLink().then(resolve).catch(reject);
|
||||
|
||||
})
|
||||
|
||||
}).catch((ex) => {
|
||||
console.log("ex=",ex)
|
||||
uni.showModal({
|
||||
content:"连接失败:"+ex.msg
|
||||
});
|
||||
}).finally(()=>{
|
||||
hideLoading(this);
|
||||
});
|
||||
}, 0);
|
||||
|
||||
}
|
||||
|
||||
execLink().then((res) => {
|
||||
linkCallback(res);
|
||||
}).catch(ex => {
|
||||
console.log("ex=", ex)
|
||||
uni.showModal({
|
||||
content: "连接失败:" + ex.msg
|
||||
});
|
||||
hideLoading(these);
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -463,8 +529,8 @@
|
||||
}
|
||||
|
||||
.list .item .leftImg {
|
||||
width: 100rpx;
|
||||
height: 70rpx;
|
||||
width: 60rpx;
|
||||
height: 60rpx;
|
||||
}
|
||||
|
||||
.list .item .centertxt {
|
||||
@ -496,7 +562,7 @@
|
||||
font-family: "PingFang SC";
|
||||
font-size: 26rpx;
|
||||
font-weight: 400;
|
||||
line-height: 50rpx;
|
||||
line-height: 36rpx;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
@ -505,14 +571,14 @@
|
||||
font-family: "PingFang SC";
|
||||
font-size: 24rpx;
|
||||
font-weight: 400;
|
||||
line-height: 30rpx;
|
||||
line-height: 36rpx;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
|
||||
.list .item .rightIco .img {
|
||||
width: 50rpx;
|
||||
height: 50rpx;
|
||||
width: 45rpx;
|
||||
height: 45rpx;
|
||||
}
|
||||
|
||||
.openBlue {
|
||||
|
||||
Reference in New Issue
Block a user