1
0
forked from dyf/APP

添加全局蓝牙监听

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>
@ -25,12 +28,15 @@
<script>
import request from '@/utils/request.js';
import bleTool from '@/utils/BleHelper.js'
import bleTool from '@/utils/BleHelper.js';
import {
showLoading,
hideLoading,
updateLoading
} from '@/utils/loading.js'
} from '@/utils/loading.js';
const pagePath="pages/common/addBLE/LinkBle";
var these = null;
var eventChannel = null;
var ble = null;
@ -66,35 +72,91 @@
}
}
},
onBackPress() {
console.log("返回时断开蓝牙连接,取消订阅");
ble.disconnectDevice(these.device.deviceId);
ble.removeReceiveCallback(pagePath);
},
onUnload() {
ble.removeReceiveCallback(pagePath);
},
onLoad(option) {
these = this;
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;
});
if (f) {
these.device = f;
these.device = Object.assign({}, these.device, f);
console.log("获取到设备", f);
these.initDevice();
} else {
console.log("未获取到设备");
}
})
},
methods: {
initDevice: function() {
showLoading(these, {
text: '正在获取设备信息'
});
request({
url: 'app/device/getDeviceInfoByDeviceMac',
url: '/app/device/getDeviceInfoByDeviceMac',
method: 'GET',
data: {
deviceMac: these.device.macAddress
}
}).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() {
@ -110,7 +172,7 @@
}
if (!f.macAddress) {
these.Statu.bound = false;
these.Statu.boundRemark = "获取设备Mac地址异常";
these.Statu.boundRemark = "设备上报Mac地址异常";
return;
}
these.Statu.bound = null;
@ -133,6 +195,8 @@
if (res.code == 200) {
these.Statu.bound = true;
these.Statu.boundRemark = "设备绑定成功!";
uni.$emit("refreshDeviceList");
setTimeout(() => {
uni.switchTab({

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,30 +170,31 @@
} else {
}
}
},pagePath);
},
onShow: function() {
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);
},
onShow: function() {
console.log("222222");
ble.StartSearch().catch((ex) => {
if (ex.code == 10001) {
these.showOpenSetting();
}
});
},
methods: {
isItemLink: function(item, index) {
@ -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);
}
});