解决订阅消息有时候会永远pending的问题

This commit is contained in:
liub
2025-09-16 17:30:46 +08:00
parent 6331190f0d
commit ed8f02fb39

View File

@ -473,9 +473,12 @@ class BleHelper {
if (str.includes(':')) {
v.macAddress = str;
} else {
v.macAddress=str.replace(/(.{2})/g, '$1:').slice(0, -1)
v.macAddress = str.replace(
/(.{2})/g, '$1:')
.slice(0, -1)
}
console.log("收到mac地址:", v.macAddress)
console.log("收到mac地址:", v
.macAddress)
}
});
uni.setStorageSync(this.StorageKey, this.data
@ -519,10 +522,12 @@ class BleHelper {
console.log("有人订阅消息")
let path = this.getCurrentPagePath();
this.cfg.receivDataCallback.forEach((rec) => {
this.cfg.receivDataCallback.forEach((
rec) => {
if (rec.callback) {
rec.callback(recData, f, path);
rec.callback(recData, f,
path);
}
})
} else {
@ -689,7 +694,8 @@ class BleHelper {
let startSubScribe = (id, serviceId, characteristicId) => {
console.log("serviceId=", serviceId);
console.log("characteristicId=", characteristicId);
return new Promise((succ, err) => {
let p1= new Promise((succ, err) => {
uni.notifyBLECharacteristicValueChange({
deviceId: id,
serviceId: serviceId,
@ -707,7 +713,8 @@ class BleHelper {
v.notifyState = state;
}
});
succ();
console.log("success SubScribe");
succ(res);//见了鬼了有时候执行了succ但promise永远pending了
},
fail: (ex) => {
@ -715,6 +722,22 @@ class BleHelper {
}
});
});
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){
succ();
return;
}
err(ex);
})
});
}
let promies = new Array();
if (c.Characteristics && c.Characteristics.length) {
@ -730,8 +753,10 @@ class BleHelper {
}
if (promies.length > 0) {
Promise.allSettled(promies).then((results) => {
console.log("234324324324");
Promise.allSettled(promies).then((results) => {
console.log("11111");
results.forEach((result, index) => {
if (result.status === "fulfilled") {
console.log(`操作${index + 1}成功:`, result.value);
@ -743,9 +768,13 @@ class BleHelper {
resolve();
}).catch((ex) => {
console.log("222222");
reject(ex);
}).finally(()=>{
console.log("finally")
});
} else {
console.log("33333");
resolve();
}
@ -971,7 +1000,9 @@ class BleHelper {
}
if (!this.data.available) {
reject(this.getError({ code: 10001 }));
reject(this.getError({
code: 10001
}));
return;
}
@ -996,8 +1027,10 @@ class BleHelper {
this.data.LinkedList.push(cr);
}
uni.setStorageSync(this.StorageKey, this.data.LinkedList);
console.log("LinkedList=", this.data.LinkedList);
uni.setStorageSync(this.StorageKey, this.data
.LinkedList);
console.log("LinkedList=", this.data
.LinkedList);
// 处理 MTU 设置
if (plus.os.name === 'Android') {
@ -1005,12 +1038,14 @@ class BleHelper {
deviceId: deviceId,
mtu: 512,
success: (mtu) => {
console.log("mtu设置成功", mtu);
console.log("mtu设置成功",
mtu);
resolve(true);
},
fail: () => {
console.log("mtu设置失败");
resolve(true); // MTU设置失败不影响连接成功
resolve(
true); // MTU设置失败不影响连接成功
}
});
} else {
@ -1085,13 +1120,13 @@ class BleHelper {
}
}).then(() => {
console.log("then.....");
setTimeout(() => {
resolve();
}, 500);
}).catch((ex) => {
////console.log("出现异常", ex);
console.log("出现异常", ex);
reject(ex);
});
});