添加全局蓝牙监听

This commit is contained in:
liub
2025-08-18 16:32:25 +08:00
parent 96acaa5d94
commit 585bd76a0a
2 changed files with 130 additions and 80 deletions

View File

@ -5,6 +5,9 @@
<image src="/static/images/BLEAdd/addBleDevice.png" class="titleIco" mode="aspectFit">
</image>
</view>
<view class="deviceName">
蓝牙名:{{device.name}}
</view>
<view class="deviceName">
设备名:{{device.deviceName}}
</view>
@ -19,21 +22,24 @@
连接
</view>
<global-loading ref="loading" />
<global-loading ref="loading" />
</view>
</template>
<script>
import request from '@/utils/request.js';
import bleTool from '@/utils/BleHelper.js'
import {
showLoading,
hideLoading,
updateLoading
} from '@/utils/loading.js'
import bleTool from '@/utils/BleHelper.js';
import {
showLoading,
hideLoading,
updateLoading
} from '@/utils/loading.js';
const pagePath="pages/common/addBLE/LinkBle";
var these = null;
var eventChannel = null;
var ble=null;
var ble = null;
export default {
data() {
return {
@ -66,57 +72,113 @@
}
}
},
onBackPress() {
console.log("返回时断开蓝牙连接,取消订阅");
ble.disconnectDevice(these.device.deviceId);
ble.removeReceiveCallback(pagePath);
},
onUnload() {
ble.removeReceiveCallback(pagePath);
},
onLoad(option) {
these = this;
ble=bleTool.getBleTool();
ble = bleTool.getBleTool();
ble.addReceiveCallback((receive,f,path) => {
console.log("收到设备消息,", receive);
if (these.device.deviceId == receive.deviceId) {
console.log("11111");
if (receive.bytes[0] == 0xFC || receive.str.indexOf('mac address:') == 0) {
if (f && f.macAddress) {
these.device.macAddress = f.macAddress;
console.log("222222");
these.initDevice();
}
}
}
},pagePath);
eventChannel = this.getOpenerEventChannel();
eventChannel.on('LinkItem', function(data) {
let f=ble.data.LinkedList.find((v)=>{
return v.deviceId=data.deviceId;
let f = ble.data.LinkedList.find((v) => {
return v.deviceId = data.deviceId;
});
if(f){
these.device = f;
console.log("获取到设备",f);
these.initDevice();
}else{
if (f) {
these.device = Object.assign({}, these.device, f);
console.log("获取到设备", f);
} else {
console.log("未获取到设备");
}
})
},
methods: {
initDevice: function() {
showLoading(these, {
text: '正在获取设备信息'
});
request({
url: 'app/device/getDeviceInfoByDeviceMac',
url: '/app/device/getDeviceInfoByDeviceMac',
method: 'GET',
data: {
data: {
deviceMac: these.device.macAddress
}
}).then(res=>{
console.log("获取设备信息",res);
}).catch((ex)=>{
console.log("获取设备出现异常:",ex);
}).then(res => {
console.log("获取设备信息", res);
if (res && res.code == 200) {
let data = res.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];
v[key] = data[key];
console.log("key="+key);
console.log("value="+data[key]);
these.$set(these.device, key, data[key]);
}
ble.setBleData();
}
});
console.log("device=",these.device);
console.log("LinkedList=",ble.data.LinkedList);
}
}
}).catch((ex) => {
console.log("获取设备出现异常:", ex);
}).finally(() => {
hideLoading(these);
});
},
Link() {
// 调用绑定设备接口
let f=ble.data.LinkedList.find((v)=>{
return v.deviceId=these.device.deviceId;
let f = ble.data.LinkedList.find((v) => {
return v.deviceId = these.device.deviceId;
});
if(!f){
if (!f) {
these.Statu.bound = false;
these.Statu.boundRemark = "蓝牙连接不成功";
these.Statu.boundRemark = "蓝牙连接不成功";
return;
}
if(!f.macAddress){
if (!f.macAddress) {
these.Statu.bound = false;
these.Statu.boundRemark = "获取设备Mac地址异常";
these.Statu.boundRemark = "设备上报Mac地址异常";
return;
}
these.Statu.bound = null;
these.Statu.boundRemark = "";
showLoading(these,{
showLoading(these, {
text: "连接中..."
})
let promise = request({
@ -130,15 +192,17 @@
});
promise.then((res) => {
console.log("1111" + JSON.stringify(res));
if (res.code == 200) {
if (res.code == 200) {
these.Statu.bound = true;
these.Statu.boundRemark = "设备绑定成功!";
uni.$emit("refreshDeviceList");
setTimeout(()=>{
uni.switchTab({
url:"/pages/common/index/index"
});
},500);
setTimeout(() => {
uni.switchTab({
url: "/pages/common/index/index"
});
}, 500);
} else {
these.Statu.bound = false;
these.Statu.boundRemark = res.msg;

View File

@ -94,7 +94,7 @@
hideLoading,
updateLoading
} from '@/utils/loading.js'
const pagePath="pages/common/addBLE/addEquip";
var ble = null;
var these = null;
export default {
@ -136,17 +136,23 @@
},
onHide: function() {
ble.StopSearch();
ble.removeReceiveCallback(pagePath);
},
onBackPress: (e) => {
ble.StopSearch();
ble.removeDeviceFound();
ble.removeReceiveCallback();
ble.removeDeviceFound(pagePath);
ble.removeReceiveCallback(pagePath);
},
onUnload(){
ble.StopSearch();
ble.removeDeviceFound(pagePath);
ble.removeReceiveCallback(pagePath);
},
onLoad() {
these = this;
ble = bleTool.getBleTool();
ble.addDeviceFound((arr) => {
console.log("发现新设备"+JSON.stringify(arr));
arr = arr.devices;
for (var i = 0; i < arr.length; i++) {
@ -164,29 +170,30 @@
} else {
}
}
});
ble.addReceiveCallback((receivData) => {
console.log("收到数据了:", receivData);//数据格式:{bytes:[109,97],str:"",hexs:"FA 01"}
console.log("LinkedList=",ble.data.LinkedList);
let data=uni.getStorageSync(ble.StorageKey);
console.log("data=",data);
});
},pagePath);
},
onShow: function() {
console.log("222222");
this.EquipMents=[];
this.PairEquip=[];
ble.StartSearch().catch((ex) => {
if (ex.code == 10001) {
these.showOpenSetting();
}
});
ble.addReceiveCallback((receivData) => {
console.log("收到数据了:", receivData);//数据格式:{bytes:[109,97],str:"",hexs:"FA 01"}
console.log("LinkedList=",ble.data.LinkedList);
let data=uni.getStorageSync(ble.StorageKey);
console.log("data=",data);
},pagePath);
},
methods: {
@ -207,30 +214,7 @@
}
return src;
},
alert: function(title, content, callback) {
if (!title) {
title = '提示'
}
if (!content) {
content = title;
}
uni.showModal({
title: title,
content: content,
success: function(res) {
if (res.confirm) {
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
if (callback) {
callback(res);
}
}
});
},
showOpenSetting: function() {
this.Status.BottomMenu.show = true;
@ -257,13 +241,15 @@
these.PairEquip.push(item);
}
ble.removeReceiveCallback(pagePath);
uni.navigateTo({
url:"/pages/common/addBLE/LinkBle",
events:{
},
success(res) {
res.eventChannel.emit('LinkItem', item)
res.eventChannel.emit('LinkItem', item);
}
});