蓝牙增加断开重连、换设备后可以去再次绑定
This commit is contained in:
@ -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