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

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

View File

@ -111,7 +111,7 @@ class BleHelper {
for (var i = 0; i < this.data.LinkedList.length; i++) {
let device = this.data.LinkedList[i];
// console.log("自动连接绑定过的设备:" + device.deviceId);
if(device.macAddress && device.device && device.device.id){
if (device.macAddress && device.device && device.device.id) {
this.LinkBlue(device.deviceId, device.writeServiceId, device.wirteCharactId, device
.notifyCharactId);
}
@ -468,14 +468,17 @@ class BleHelper {
this.data.LinkedList.find((v) => {
if (v.deviceId == receive
.deviceId) {
str= str.replace(
str = str.replace(
header, "");
if(str.includes(':')){
v.macAddress =str;
}else{
v.macAddress=str.replace(/(.{2})/g, '$1:').slice(0, -1)
if (str.includes(':')) {
v.macAddress = str;
} else {
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
@ -512,27 +515,29 @@ class BleHelper {
hexs: hexs
};
console.log("监听到特征值:" + JSON.stringify(recData));
try{
try {
if (this.cfg.receivDataCallback) {
if (this.cfg.receivDataCallback.length > 0) {
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{
} else {
console.log("无人订阅消息");
}
} else {
console.log("无人订阅receivDataCallback,不处理数据");
}
}catch(ex){
console.log("ex=",ex);
} catch (ex) {
console.log("ex=", ex);
}
});
@ -687,9 +692,10 @@ class BleHelper {
}
// console.log("c=", c);
let startSubScribe = (id, serviceId, characteristicId) => {
console.log("serviceId=",serviceId);
console.log("characteristicId=",characteristicId);
return new Promise((succ, err) => {
console.log("serviceId=", serviceId);
console.log("characteristicId=", characteristicId);
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,9 +722,25 @@ 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){
if (c.Characteristics && c.Characteristics.length) {
for (var i = 0; i < c.Characteristics.length; i++) {
let item = c.Characteristics[i];
let serviceId = item.serviceId;
@ -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 {
@ -1059,15 +1094,15 @@ class BleHelper {
return this.getService(deviceId, targetServiceId, writeCharId,
notifyCharId); //获取服务
} else {
if(f.wirteCharactId && f.notifyCharactId){
if (f.wirteCharactId && f.notifyCharactId) {
if (!f.notifyState) {
console.log("开始订阅特征");
this.subScribe(deviceId, true);
}else{
} else {
console.log("不订阅消息");
}
return Promise.resolve(true);
}else{
} else {
console.log("开始获取服务", targetServiceId)
return this.getService(deviceId, targetServiceId, writeCharId,
notifyCharId);
@ -1085,13 +1120,13 @@ class BleHelper {
}
}).then(() => {
console.log("then.....");
setTimeout(() => {
resolve();
}, 500);
}).catch((ex) => {
////console.log("出现异常", ex);
console.log("出现异常", ex);
reject(ex);
});
});