添加全局蓝牙监听
This commit is contained in:
@ -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;
|
||||
});
|
||||
|
@ -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,123 +376,44 @@
|
||||
|
||||
return className;
|
||||
},
|
||||
bleValueNotify: function(receive) { //订阅消息
|
||||
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;
|
||||
}
|
||||
bleValueNotify: function(receive, device, path) { //订阅消息
|
||||
|
||||
// 解析照明档位
|
||||
let lightingLevelByte = bytes[2];
|
||||
let lightingLevelText = lightingLevelByte === 0x6e ? '开启' : '关闭';
|
||||
let data = recei.ReceiveData(receive, device, pagePath);
|
||||
|
||||
|
||||
// 解析剩余照明时间(第三和第四字节,大端序)
|
||||
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) {
|
||||
these.showPop({
|
||||
message: "环境存在漏电电源",
|
||||
iconUrl: "/static/images/6155/DeviceDetail/warnning.png",
|
||||
borderColor: "#e034344d",
|
||||
buttonBgColor: "#E03434",
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('数据解析错误:', error);
|
||||
}
|
||||
if (data) {
|
||||
console.log("收到订阅消息", receive);
|
||||
let keys = Object.keys(data);
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
let key = keys[i];
|
||||
these.formData[key] = data[key];
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
if (these.formData.iswarn) {
|
||||
these.showPop({
|
||||
message: "环境存在漏电电源",
|
||||
iconUrl: "/static/images/6155/DeviceDetail/warnning.png",
|
||||
borderColor: "#e034344d",
|
||||
buttonBgColor: "#E03434",
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
|
Reference in New Issue
Block a user