1
0
forked from dyf/APP

完成7305,添加在详情页去绑定蓝牙设备

This commit is contained in:
liub
2025-09-17 15:21:20 +08:00
parent ed8f02fb39
commit da9a9f4874
19 changed files with 5311 additions and 1523 deletions

View File

@ -40,9 +40,9 @@ class BleHelper {
onDeviceFound: [], //发现新设备的事件
receivDataCallback: [] //接收到数据的事件
}
this.addReceiveCallback((a, b, c) => {
recei.ReceiveData(a, b, c);
}, "BleReceiveData");
// this.addReceiveCallback((a, b, c) => {
// recei.ReceiveData(a, b, c);
// }, "BleReceiveData");
this.dic = {
errRemarks: [{
key: '10000',
@ -104,6 +104,36 @@ class BleHelper {
}
}
DropDevice(bleId, deviceId) {
let flag = false;
for (var i = 0; i < this.data.LinkedList.length; i++) {
let item = this.data.LinkedList[i];
if (bleId) {
if (item.deviceId == bleId) {
this.data.LinkedList.splice(i, 1);
flag = true;
break;
}
} else {
if (item.device && item.device.id) {
this.data.LinkedList.splice(i, 1);
flag = true;
break;
}
}
}
if (flag) {
this.updateCache();
}
}
updateCache() {
console.log("this.StorageKey=", this.StorageKey)
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
}
linkAllDevices() {
console.log("模块启动时,自动连接已连接过的设备", this.data.LinkedList);
@ -162,7 +192,7 @@ class BleHelper {
let key = currKey ? currKey : this.getCurrentPagePath();
if (key) {
console.log("key=" + key);
// console.log("key=" + key);
let f = this.cfg.onDeviceFound.findIndex((v) => {
return v.key == key;
});
@ -205,7 +235,7 @@ class BleHelper {
removeReceiveCallback(currKey) {
let key = currKey ? currKey : this.getCurrentPagePath();
if (key) {
console.log("移除消息回调:" + key);
// console.log("移除消息回调:" + key);
let f = this.cfg.receivDataCallback.findIndex((v) => {
return v.key == key;
});
@ -399,8 +429,8 @@ class BleHelper {
v.notifyState = false;
return true;
});
uni.setStorageSync(this.StorageKey, this.data
.LinkedList);
this.updateCache();
}
});
@ -417,8 +447,7 @@ class BleHelper {
}
return false;
});
uni.setStorageSync(this.StorageKey, this.data
.LinkedList);
this.updateCache();
} else {
// console.log("蓝牙连接已恢复,", res);
}
@ -464,25 +493,26 @@ class BleHelper {
let header = "mac address:";
if (str.indexOf(header) == 0) { //650以文本传输mac
console.log("str=", str);
this.data.LinkedList.find((v) => {
if (v.deviceId == receive
.deviceId) {
str = str.replace(
let macStr = str.replace(
header, "");
if (str.includes(':')) {
v.macAddress = str;
if (macStr.includes(':')) {
v.macAddress = macStr;
} else {
v.macAddress = str.replace(
v.macAddress = macStr
.replace(
/(.{2})/g, '$1:')
.slice(0, -1)
}
str = header + v.macAddress;
console.log("收到mac地址:", v
.macAddress)
}
});
uni.setStorageSync(this.StorageKey, this.data
.LinkedList);
this.updateCache();
}
if (bytes[0] == 0xFC) { //6155以0xFC开头代表mac地址
@ -498,8 +528,7 @@ class BleHelper {
// console.log("收到mac地址:", str)
}
});
uni.setStorageSync(this.StorageKey, this
.data.LinkedList);
this.updateCache();
}
}
console.log("str=", str);
@ -514,20 +543,26 @@ class BleHelper {
str: str,
hexs: hexs
};
console.log("监听到特征值:" + JSON.stringify(recData));
// console.log("监听到特征值:" + JSON.stringify(recData));
try {
if (this.cfg.receivDataCallback) {
if (this.cfg.receivDataCallback.length > 0) {
console.log("有人订阅消息")
let path = this.getCurrentPagePath();
console.log("有人订阅消息")
this.cfg.receivDataCallback.forEach((
rec) => {
rec) => {
console.log("有人订阅消息111",)
if (rec.callback) {
try{
rec.callback(recData, f,
path);
path, this.cfg
.receivDataCallback);
}catch(err){
console.log("订阅消息出现异常",err);
}
}
})
} else {
@ -692,10 +727,10 @@ class BleHelper {
}
// console.log("c=", c);
let startSubScribe = (id, serviceId, characteristicId) => {
console.log("serviceId=", serviceId);
console.log("characteristicId=", characteristicId);
let p1= new Promise((succ, err) => {
// console.log("serviceId=", serviceId);
// console.log("characteristicId=", characteristicId);
let p1 = new Promise((succ, err) => {
uni.notifyBLECharacteristicValueChange({
deviceId: id,
serviceId: serviceId,
@ -713,8 +748,10 @@ class BleHelper {
v.notifyState = state;
}
});
console.log("success SubScribe");
succ(res);//见了鬼了有时候执行了succ但promise永远pending了
// console.log("success SubScribe");
succ(
res
); //见了鬼了有时候执行了succ但promise永远pending了
},
fail: (ex) => {
@ -722,22 +759,24 @@ class BleHelper {
}
});
});
let p2=new Promise((succ,err)=>{
setTimeout(()=>{
err({code:-1});
},50);
let p2 = new Promise((succ, err) => {
setTimeout(() => {
err({
code: -1
});
}, 50);
});
return new Promise((succ,err)=>{
Promise.race([p1,p2]).then(succ).catch(ex=>{
if(ex.code==-1){
return new Promise((succ, err) => {
Promise.race([p1, p2]).then(succ).catch(ex => {
if (ex.code == -1) {
succ();
return;
}
err(ex);
})
});
}
let promies = new Array();
if (c.Characteristics && c.Characteristics.length) {
@ -754,27 +793,27 @@ class BleHelper {
if (promies.length > 0) {
console.log("234324324324");
// console.log("234324324324");
Promise.allSettled(promies).then((results) => {
console.log("11111");
// console.log("11111");
results.forEach((result, index) => {
if (result.status === "fulfilled") {
console.log(`操作${index + 1}成功:`, result.value);
// console.log(`操作${index + 1}成功:`, result.value);
} else {
console.log(`操作${index + 1}失败:`, result.reason
.message);
// console.log(`操作${index + 1}失败:`, result.reason
// .message);
}
});
resolve();
}).catch((ex) => {
console.log("222222");
// console.log("222222");
reject(ex);
}).finally(()=>{
console.log("finally")
}).finally(() => {
// console.log("finally")
});
} else {
console.log("33333");
// console.log("33333");
resolve();
}
@ -806,8 +845,7 @@ class BleHelper {
v.services = res.services;
}
});
uni.setStorageSync(this.StorageKey,
this.data.LinkedList);
this.updateCache();
var promises = [];
let se = res.services.find((v) => {
@ -947,7 +985,7 @@ class BleHelper {
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
this.updateCache();
resolve(res);
},
fail: (ex) => {
@ -989,12 +1027,12 @@ class BleHelper {
var linkDevice = () => {
// 添加重试次数限制
let retryCount = 0;
const maxRetries = 1; // 最大重试次数
const maxRetries = 5; // 最大重试次数
const connect = () => {
return new Promise((resolve, reject) => {
if (fIndex > -1 && f?.Linked) {
console.log("当前已连接,跳过其他步骤");
// console.log("当前已连接,跳过其他步骤");
resolve(false);
return;
}
@ -1027,10 +1065,9 @@ class BleHelper {
this.data.LinkedList.push(cr);
}
uni.setStorageSync(this.StorageKey, this.data
.LinkedList);
this.updateCache();
console.log("LinkedList=", this.data
.LinkedList);
.LinkedList);
// 处理 MTU 设置
if (plus.os.name === 'Android') {
@ -1045,7 +1082,8 @@ class BleHelper {
fail: () => {
console.log("mtu设置失败");
resolve(
true); // MTU设置失败不影响连接成功
true
); // MTU设置失败不影响连接成功
}
});
} else {
@ -1088,15 +1126,15 @@ class BleHelper {
}).then((res) => {
if (res) { //新连接
console.log("11111111");
// console.log("11111111");
if (fIndex == -1) {
console.log("开始获取服务", targetServiceId)
// console.log("开始获取服务", targetServiceId)
return this.getService(deviceId, targetServiceId, writeCharId,
notifyCharId); //获取服务
} else {
if (f.wirteCharactId && f.notifyCharactId) {
if (!f.notifyState) {
console.log("开始订阅特征");
// console.log("开始订阅特征");
this.subScribe(deviceId, true);
} else {
console.log("不订阅消息");
@ -1111,7 +1149,7 @@ class BleHelper {
}
} else { //已连接过,直接订阅消息
console.log("11111111");
// console.log("11111111");
if (fIndex > -1 && f && !f.notifyState) {
this.subScribe(deviceId, true);
@ -1120,7 +1158,7 @@ class BleHelper {
}
}).then(() => {
console.log("then.....");
// console.log("then.....");
setTimeout(() => {
resolve();
@ -1235,14 +1273,14 @@ class BleHelper {
})); //没有找到指定设备
return;
}
console.log("device=", device);
// console.log("device=", device);
uni.writeBLECharacteristicValue({
deviceId: device.deviceId,
serviceId: device.writeServiceId,
characteristicId: device.wirteCharactId,
value: buffer,
success: () => {
console.log("发送数据成功");
// console.log("发送数据成功");
succ();
},
fail: (ex) => {
@ -1340,9 +1378,7 @@ class BleHelper {
return result;
}
setBleData() {
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
}
}
let instance = null;

View File

@ -44,21 +44,21 @@ class BleReceive {
}
ReceiveData(receive,f,path) {
ReceiveData(receive,f,path,recArr) {
if(f && f.macAddress && f.device && f.device.id){
let data={};
if(f.device.detailPageUrl=='/pages/6155/deviceDetail'){
// console.log("该设备是6155");
data= this.Receive_6155(receive,f,path);
data= this.Receive_6155(receive,f,path,recArr);
}
if(f.device.detailPageUrl=='/pages/650/HBY650'){
// console.log("该设备是650");
data= this.Receive_650(receive,f,path);
data= this.Receive_650(receive,f,path,recArr);
}
if(f.device.detailPageUrl=='/pages/670/HBY670'){
// console.log("该设备是670");
data= this.Receive_670(receive,f,path);
data= this.Receive_670(receive,f,path,recArr);
}
// console.log("收到数据并处理完毕,",data);
return data;
@ -70,7 +70,7 @@ class BleReceive {
}
Receive_650(receive,f,path) {
Receive_650(receive,f,path,recArr) {
console.log("通用程序正在处理650的数据",receive);
var parseData = () => {
@ -253,8 +253,8 @@ class BleReceive {
}
Receive_670(receive,f,path){
Receive_670(receive,f,path,recArr){
console.log("pagh=",path);
var todo = (bytes) =>{
// console.log("todo",receive);
let receiveData = {};
@ -316,6 +316,21 @@ class BleReceive {
receiveData.staticWarn = staticWarn;
receiveData.fourGStrenth = fourGStrenth;
receiveData.SOS=sosTxt;
this.setBleFormData(receiveData,f);
console.log("recArr=",recArr);
let recCnt=recArr.find(v=>{
return v.key == f.device.detailPageUrl;
});
if(!recCnt){
if (this.formData.staticWarn) { //有静止报警
uni.showModal({
title:"警告",
content:"设备静止报警中",
showCancel:false
});
}
}
} catch(error) {
console.log('数据解析错误:', error);
}
@ -394,7 +409,7 @@ class BleReceive {
return data;
}
Receive_6155(receive,f,path) {
Receive_6155(receive,f,path,recArr) {
let bytes=receive.bytes;
if (bytes[0] == 0xFB && bytes[1] == 0x64 && bytes.length >= 8) {
try {

View File

@ -1,3 +1,5 @@
import request,{baseURL} from '@/utils/request.js'
/**
* 检查并执行wgt热更新
* @param {String} updateUrl - 检查更新的接口地址
@ -13,53 +15,90 @@ function checkAndUpdateWgt(updateUrl) {
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
const currentVersion = widgetInfo.version;
console.log("当前版本:" + currentVersion);
// 2. 调用后端接口检查是否有更新
uni.request({
url: updateUrl,
method: 'GET',
data: {
currentVersion: currentVersion,
platform: uni.getSystemInfoSync().platform
},
success: (res) => {
uni.hideLoading();
console.log("res=", res)
if (res.statusCode === 200) {
const updateInfo = res.data.data;
if (!updateInfo.hasUpdate) {
return;
}
// 3. 显示更新提示
uni.showModal({
title: '检测到更新',
content: updateInfo.description || '有新版本可用,是否立即更新?',
confirmText: '立即更新',
cancelText: '稍后更新',
success: (modalRes) => {
if (modalRes.confirm) {
downloadAndInstallWgt(updateInfo.downloadUrl);
}
}
});
} else {
uni.showToast({
title: '当前已是最新版本',
icon: 'none',
duration: 2000
});
}
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: '检查更新失败',
icon: 'none',
duration: 2000
});
console.error('检查更新失败:', err);
request({
url:'/app/auth/version',
method: 'get'
}).then(res=>{
console.log("检查版本更新:",res);
let os=plus.os.name.toLowerCase();
if(res.code!=200){
return;
}
let f=res.data.find(v=>{
if(v.dictLabel.toLowerCase()==os){
return true;
}
return false;
});
if(f){
if(f.dictValue==currentVersion){
return;
}
uni.showModal({
title: '检测到更新',
content: '当前版本“'+currentVersion+'”,发现新版本“'+f.dictValue+'”,是否立即更新?',
confirmText: '立即更新',
cancelText: '稍后更新',
success: (modalRes) => {
if (modalRes.confirm) {
downloadAndInstallWgt(f.remark);
}
}
});
}
}).catch(ex=>{
console.log("检查更新失败:",ex);
});
// 2. 调用后端接口检查是否有更新
// uni.request({
// url: updateUrl,
// method: 'GET',
// data: {
// currentVersion: currentVersion,
// platform: uni.getSystemInfoSync().platform
// },
// success: (res) => {
// uni.hideLoading();
// console.log("res=", res)
// if (res.statusCode === 200) {
// const updateInfo = res.data.data;
// if (!updateInfo.hasUpdate) {
// return;
// }
// // 3. 显示更新提示
// uni.showModal({
// title: '检测到更新',
// content: updateInfo.description || '有新版本可用,是否立即更新?',
// confirmText: '立即更新',
// cancelText: '稍后更新',
// success: (modalRes) => {
// if (modalRes.confirm) {
// downloadAndInstallWgt(updateInfo.downloadUrl);
// }
// }
// });
// } else {
// uni.showToast({
// title: '当前已是最新版本',
// icon: 'none',
// duration: 2000
// });
// }
// },
// fail: (err) => {
// uni.hideLoading();
// uni.showToast({
// title: '检查更新失败',
// icon: 'none',
// duration: 2000
// });
// console.error('检查更新失败:', err);
// }
// });
});
}