添加全局蓝牙消息监听
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
import receivTool from "@/utils/BleReceive.js"
|
||||
var recei = null;
|
||||
class BleHelper {
|
||||
constructor() {
|
||||
this.StorageKey = "linkedDevices";
|
||||
recei = receivTool.getBleReceive();
|
||||
|
||||
this.init();
|
||||
}
|
||||
@ -37,7 +40,9 @@ class BleHelper {
|
||||
onDeviceFound: [], //发现新设备的事件
|
||||
receivDataCallback: [] //接收到数据的事件
|
||||
}
|
||||
|
||||
this.addReceiveCallback((a,b,c)=>{
|
||||
recei.ReceiveData(a,b,c);
|
||||
}, "BleReceiveData");
|
||||
this.dic = {
|
||||
errRemarks: [{
|
||||
key: '10000',
|
||||
@ -128,8 +133,8 @@ class BleHelper {
|
||||
}
|
||||
|
||||
//设置发现新设备的回调
|
||||
addDeviceFound(callback,currKey) {
|
||||
let key =currKey?currKey:this.getCurrentPagePath();
|
||||
addDeviceFound(callback, currKey) {
|
||||
let key = currKey ? currKey : this.getCurrentPagePath();
|
||||
if (!key) {
|
||||
key = new Date().getTime();
|
||||
}
|
||||
@ -151,7 +156,7 @@ class BleHelper {
|
||||
|
||||
//移除发现新设备的回调
|
||||
removeDeviceFound(currKey) {
|
||||
let key =currKey?currKey: this.getCurrentPagePath();
|
||||
let key = currKey ? currKey : this.getCurrentPagePath();
|
||||
|
||||
if (key) {
|
||||
console.log("key=" + key);
|
||||
@ -170,12 +175,13 @@ class BleHelper {
|
||||
}
|
||||
|
||||
//添加接收到数据的回调
|
||||
addReceiveCallback(callback,currKey) {
|
||||
let key =currKey?currKey: this.getCurrentPagePath();
|
||||
addReceiveCallback(callback, currKey) {
|
||||
let key = currKey ? currKey : this.getCurrentPagePath();
|
||||
if (!key) {
|
||||
key = new Date().getTime();
|
||||
}
|
||||
if (key) {
|
||||
console.log("订阅消息回调,key=" + key);
|
||||
let f = this.cfg.receivDataCallback.findIndex((v) => {
|
||||
return v.key == key;
|
||||
});
|
||||
@ -193,10 +199,10 @@ class BleHelper {
|
||||
|
||||
|
||||
//设置接收到数据的回调
|
||||
removeReceiveCallback(ev,currKey) {
|
||||
let key =currKey?currKey: this.getCurrentPagePath();
|
||||
removeReceiveCallback(currKey) {
|
||||
let key = currKey ? currKey : this.getCurrentPagePath();
|
||||
if (key) {
|
||||
console.log("key=" + key);
|
||||
console.log("移除消息回调:" + key);
|
||||
let f = this.cfg.receivDataCallback.findIndex((v) => {
|
||||
return v.key == key;
|
||||
});
|
||||
@ -426,8 +432,10 @@ class BleHelper {
|
||||
});
|
||||
|
||||
uni.onBLECharacteristicValueChange((receive) => {
|
||||
|
||||
|
||||
//订阅消息
|
||||
let f=this.data.LinkedList.find((v) => {
|
||||
return v.deviceId == receive.deviceId;
|
||||
})
|
||||
let dataView = new DataView(receive.value);
|
||||
|
||||
// 转换为字节数组
|
||||
@ -453,7 +461,7 @@ class BleHelper {
|
||||
.deviceId) {
|
||||
v.macAddress = str.replace(
|
||||
header, "");
|
||||
console.log("收到mac地址:", str)
|
||||
// console.log("收到mac地址:", str)
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this.data
|
||||
@ -461,13 +469,15 @@ class BleHelper {
|
||||
}
|
||||
|
||||
if (bytes[0] == 0xFC) { //6155以0xFC开头代表mac地址
|
||||
if (arr.length >= 7) {
|
||||
let mac = arr.slice(1, 7).join(":");
|
||||
// console.log("收到mac地址:", bytes)
|
||||
if (bytes.length >= 7) {
|
||||
let mac = hexs.slice(1, 7).join(":")
|
||||
.toUpperCase();
|
||||
this.data.LinkedList.find((v) => {
|
||||
if (v.deviceId == receive
|
||||
.deviceId) {
|
||||
v.macAddress = mac;
|
||||
console.log("收到mac地址:", str)
|
||||
// console.log("收到mac地址:", str)
|
||||
}
|
||||
});
|
||||
uni.setStorageSync(this.StorageKey, this
|
||||
@ -478,19 +488,31 @@ class BleHelper {
|
||||
////console.log("将数据转文本失败", ex);
|
||||
}
|
||||
let recData = {
|
||||
deviceId: receive.deviceId,
|
||||
serviceId: receive.serviceId,
|
||||
characteristicId: receive.characteristicId,
|
||||
bytes: bytes,
|
||||
str: str,
|
||||
hexs: hexs
|
||||
};
|
||||
console.log("监听到特征值:" + JSON.stringify(recData));
|
||||
if (this.cfg.receivDataCallback) {
|
||||
|
||||
if (this.cfg.receivDataCallback.length > 0) {
|
||||
|
||||
let path = this.getCurrentPagePath();
|
||||
|
||||
this.cfg.receivDataCallback.forEach((rec) => {
|
||||
rec.callback(recData);
|
||||
|
||||
if (rec.callback) {
|
||||
|
||||
}
|
||||
rec.callback(recData, f, path);
|
||||
|
||||
})
|
||||
}
|
||||
} else {
|
||||
console.log("无任何人订阅receivDataCallback,不处理数据");
|
||||
console.log("无人订阅receivDataCallback,不处理数据");
|
||||
}
|
||||
});
|
||||
|
||||
@ -986,9 +1008,16 @@ class BleHelper {
|
||||
}).then((res) => {
|
||||
////console.log("11111111");
|
||||
if (res) { //新连接
|
||||
if (fIndex == -1) {
|
||||
console.log("开始获取服务", targetServiceId)
|
||||
return this.getService(deviceId, targetServiceId, writeCharId,
|
||||
notifyCharId); //获取服务
|
||||
} else {
|
||||
if (!f.notifyState) {
|
||||
this.subScribe(deviceId, true);
|
||||
}
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
} else { //已连接过,直接订阅消息
|
||||
if (fIndex > -1 && f && !f.notifyState) {
|
||||
|
||||
@ -1196,25 +1225,31 @@ class BleHelper {
|
||||
|
||||
//将点阵数据转换成RGB565
|
||||
convertToRGB565(pixels, type) {
|
||||
if (!type) {
|
||||
type = 'rgb';
|
||||
}
|
||||
const result = new Uint16Array(pixels.length / 4);
|
||||
let index = 0;
|
||||
for (let i = 0; i < pixels.length; i += 4) {
|
||||
let r = pixels[i];
|
||||
let g = pixels[i + 1];
|
||||
let b = pixels[i + 2];
|
||||
let a = pixels[i + 3];
|
||||
|
||||
let rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
|
||||
let bgr565 = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
|
||||
if (type == 'bgr') {
|
||||
result[index++] = bgr565;
|
||||
result[index++] = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
|
||||
} else {
|
||||
result[index++] = rgb565;
|
||||
result[index++] = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
setBleData() {
|
||||
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
|
||||
}
|
||||
}
|
||||
|
||||
let instance = null;
|
||||
|
@ -1,7 +1,211 @@
|
||||
|
||||
class BleReceive {
|
||||
constructor() {
|
||||
this.StorageKey = "linkedDevices";
|
||||
|
||||
}
|
||||
|
||||
getCurrentPagePath() {
|
||||
|
||||
const pages = getCurrentPages();
|
||||
|
||||
if (pages.length === 0) {
|
||||
console.log("pages.length=0");
|
||||
return "";
|
||||
}
|
||||
|
||||
const currentPage = pages[pages.length - 1];
|
||||
console.log("currentPage=", currentPage.route);
|
||||
return currentPage.route;
|
||||
}
|
||||
|
||||
|
||||
setBleFormData(data,f) {
|
||||
if(data){
|
||||
let linkedList=uni.getStorageSync(this.StorageKey);
|
||||
linkedList.find((v)=>{
|
||||
if(f.deviceId==v.deviceId){
|
||||
let keys=Object.keys(data);
|
||||
keys.forEach((key)=>{
|
||||
if(!v.formData){
|
||||
v.formData={};
|
||||
}
|
||||
if(!f.formData){
|
||||
f.formData={};
|
||||
}
|
||||
v.formData[key]=data[key];
|
||||
f.formData[key]=data[key];
|
||||
});
|
||||
|
||||
uni.setStorageSync(this.StorageKey,linkedList);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
ReceiveData(receive,f,path) {
|
||||
if(f && f.macAddress && f.id){
|
||||
if(f.detailPageUrl=='/pages/6155/deviceDetail'){
|
||||
console.log("该设备是6155");
|
||||
return this.Receive_6155(receive,f,path);
|
||||
}
|
||||
|
||||
if(f.detailPageUrl=='/pages/650/HBY650'){
|
||||
console.log("该设备是650");
|
||||
return this.Receive_650(receive,f,path);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
console.log("已收到该消息,但无法处理",f);
|
||||
|
||||
}
|
||||
|
||||
Receive_650(receive,f,path) {
|
||||
console.log("通用程序正在处理650的数据");
|
||||
|
||||
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 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 = '非常强预警';
|
||||
}
|
||||
|
||||
let formData={};
|
||||
formData.battary = batteryLevel;
|
||||
formData.xuhang = xuhang;
|
||||
formData.cMode = lightingLevelByte === 0x6e;
|
||||
formData.modeCurr = modeCurr;
|
||||
formData.warnLevel = warn;
|
||||
formData.iswarn = iswarn;
|
||||
this.setBleFormData(formData,f);
|
||||
|
||||
let route=this.getCurrentPagePath();
|
||||
console.log("f=",f);
|
||||
console.log("route="+route);
|
||||
if (iswarn && f.detailPageUrl.indexOf(route)==-1 ) {
|
||||
uni.showModal({
|
||||
content:"环境存在漏电电源",
|
||||
title:"警告",
|
||||
success(res){
|
||||
if(res.confirm){
|
||||
|
||||
if(f){
|
||||
uni.navigateTo({
|
||||
url: f.detailPageUrl,
|
||||
events: {
|
||||
ack: function(data) {}
|
||||
},
|
||||
success: (res) => {
|
||||
res.eventChannel.emit('detailData', {
|
||||
data: f,
|
||||
deviceType: '',
|
||||
apiType: 'listA'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}else{
|
||||
console.log("当前全局不处理此消息");
|
||||
}
|
||||
return formData;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (receive.str) {
|
||||
try {
|
||||
|
||||
let str = receive.str;
|
||||
|
||||
if (str.indexOf('mac address:') == 0) {
|
||||
let formData={};
|
||||
formData.macAddress = str.split(':')[1];
|
||||
this.setBleFormData(formData,f);
|
||||
return formData;
|
||||
}
|
||||
return null;
|
||||
} catch (ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let data=parseData();
|
||||
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
Receive_6155() {
|
||||
console.log("通用程序正在处理6155的数据");
|
||||
}
|
||||
|
||||
|
||||
@ -9,13 +213,13 @@ class BleReceive {
|
||||
|
||||
|
||||
|
||||
let instance = null;
|
||||
let receiveInstance = null;
|
||||
export default {
|
||||
getBleReceive: function(found, receive) {
|
||||
if (!instance) {
|
||||
instance = new BleReceive();
|
||||
if (!receiveInstance) {
|
||||
receiveInstance = new BleReceive();
|
||||
|
||||
}
|
||||
return instance;
|
||||
return receiveInstance;
|
||||
}
|
||||
}
|
@ -26,7 +26,6 @@ export const hideLoading = (ev) => {
|
||||
if(!ev){
|
||||
return;
|
||||
}
|
||||
console.log("hide.....")
|
||||
ev.$refs.loading.hide();
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user