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

View File

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