1
0
forked from dyf/APP

添加全局蓝牙监听

This commit is contained in:
liub
2025-08-18 16:33:06 +08:00
parent 585bd76a0a
commit 793fe7132b
2 changed files with 35 additions and 109 deletions

View File

@ -311,6 +311,7 @@
methods: {
getDevice: function() {
console.log("LinkedList=", ble.data.LinkedList);
console.log("formData=", these.formData);
let f = ble.data.LinkedList.find((v) => {
return v.deviceId == these.formData.deviceId;
});

View File

@ -167,13 +167,16 @@
<script>
import gbk from '@/utils/gbk.js'
import BleTool from '@/utils/BleHelper.js'
import BleReceive from '@/utils/BleReceive';
import {
showLoading,
hideLoading,
updateLoading
} from '@/utils/loading.js'
const pagePath = "pages/650/HBY650";
var ble = null;
var these = null;
var recei = null;
export default {
data() {
return {
@ -265,10 +268,11 @@
onUnload() {
console.log("页面卸载,释放资源");
ble.removeReceiveCallback(this);
ble.removeReceiveCallback(pagePath);
},
onLoad: function() {
these = this;
recei = BleReceive.getBleReceive();
ble = BleTool.getBleTool();
ble.addReceiveCallback(these.bleValueNotify);
let eventChannel = this.getOpenerEventChannel();
@ -309,7 +313,7 @@
these.formData.deviceName = device.deviceName;
these.formData.img = device.devicePic;
these.formData.id = device.id;
these.formData.deviceId=f.deviceId;
these.formData.deviceId = f.deviceId;
ble.LinkBlue(f.deviceId, f.writeServiceId, f.wirteCharactId, f.notifyCharactId);
these.setBleFormData();
@ -322,7 +326,7 @@
},
onBackPress(e) {
ble.removeReceiveCallback();
ble.removeReceiveCallback(pagePath);
},
computed: {
RSSIRemark: function() {
@ -372,82 +376,21 @@
return className;
},
bleValueNotify: function(receive) { //订阅消息
bleValueNotify: function(receive, device, path) { //订阅消息
let data = recei.ReceiveData(receive, device, pagePath);
if (data) {
console.log("收到订阅消息", receive);
var parseData = () => {
let bytes = receive.bytes;
if (bytes[0] == 0x55) {
try {
let staticLevelByte = bytes[1];
let staticLevelText = '未知';
let modeCurr = "";
switch (staticLevelByte) {
case 0x65:
staticLevelText = '高档';
modeCurr = "hight";
break;
case 0x66:
staticLevelText = '中档';
modeCurr = "center";
break;
case 0x67:
staticLevelText = '低档';
modeCurr = "low";
break;
case 0x68:
staticLevelText = '关闭';
modeCurr = "close";
break;
let keys = Object.keys(data);
for (var i = 0; i < keys.length; i++) {
let key = keys[i];
these.formData[key] = data[key];
}
// 解析照明档位
let lightingLevelByte = bytes[2];
let lightingLevelText = lightingLevelByte === 0x6e ? '开启' : '关闭';
// 解析剩余照明时间(第三和第四字节,大端序)
let lightingTime = (bytes[3] << 8) | bytes[4];
let hours = Math.floor(lightingTime / 60);
let remainingMinutes = lightingTime % 60;
let xuhang = '0分';
// 处理不同情况的显示
if (hours === 0) {
xuhang = `${remainingMinutes}`;
} else if (remainingMinutes === 0) {
xuhang = `${hours}小时`;
} else {
xuhang = `${hours}小时${remainingMinutes}`;
}
// 解析剩余电量
let batteryLevelByte = bytes[5];
// 电量百分比范围检查
let batteryLevel = Math.max(0, Math.min(100, batteryLevelByte));
let iswarn = false;
let warn = bytes[6];
if (warn == 0x00) {
warn = '无预警';
} else if (warn == 0x01) {
warn = '弱预警';
} else if (warn == 0x02) {
iswarn = true;
warn = '中预警';
} else if (warn == 0x03) {
iswarn = true;
warn = '强预警';
} else if (warn == 0x04) {
iswarn = true;
warn = '非常强预警';
}
these.formData.battary = batteryLevel;
these.formData.xuhang = xuhang;
these.formData.cMode = lightingLevelByte === 0x6e;
these.formData.modeCurr = modeCurr;
these.formData.warnLevel = warn;
these.formData.iswarn = iswarn;
these.setBleFormData();
if (iswarn) {
if (these.formData.iswarn) {
these.showPop({
message: "环境存在漏电电源",
iconUrl: "/static/images/6155/DeviceDetail/warnning.png",
@ -455,40 +398,22 @@
buttonBgColor: "#E03434",
});
}
} catch (error) {
console.error('数据解析错误:', error);
}
}
if (receive.str) {
try {
let str = receive.str;
if (str.indexOf('mac address:') == 0) {
these.formData.macAddress = str.split(':')[1];
console.log("收到mac地址:", )
} else {
console.log("收到无法解析的字符串:", str)
}
} catch (ex) {
console.log("将数据转文本失败", ex);
}
}
these.setBleFormData();
}
// 转换为字节数组
const bytes = receive.bytes;
parseData(bytes);
// // 转换为字节数组
// const bytes = receive.bytes;
// parseData(bytes);
},
getDevice: function() {
console.log("LinkedList=",ble.data.LinkedList);
console.log("formData=",these.formData);
console.log("LinkedList=", ble.data.LinkedList);
console.log("formData=", these.formData);
let f = ble.data.LinkedList.find((v) => {
return v.deviceId == these.formData.deviceId;
});