完成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;