解决订阅消息有时候会永远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++) { for (var i = 0; i < this.data.LinkedList.length; i++) {
let device = this.data.LinkedList[i]; let device = this.data.LinkedList[i];
// console.log("自动连接绑定过的设备:" + device.deviceId); // 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 this.LinkBlue(device.deviceId, device.writeServiceId, device.wirteCharactId, device
.notifyCharactId); .notifyCharactId);
} }
@ -468,14 +468,17 @@ class BleHelper {
this.data.LinkedList.find((v) => { this.data.LinkedList.find((v) => {
if (v.deviceId == receive if (v.deviceId == receive
.deviceId) { .deviceId) {
str= str.replace( str = str.replace(
header, ""); header, "");
if(str.includes(':')){ if (str.includes(':')) {
v.macAddress =str; v.macAddress = str;
}else{ } 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 uni.setStorageSync(this.StorageKey, this.data
@ -512,27 +515,29 @@ class BleHelper {
hexs: hexs hexs: hexs
}; };
console.log("监听到特征值:" + JSON.stringify(recData)); console.log("监听到特征值:" + JSON.stringify(recData));
try{ try {
if (this.cfg.receivDataCallback) { if (this.cfg.receivDataCallback) {
if (this.cfg.receivDataCallback.length > 0) { if (this.cfg.receivDataCallback.length > 0) {
console.log("有人订阅消息") console.log("有人订阅消息")
let path = this.getCurrentPagePath(); let path = this.getCurrentPagePath();
this.cfg.receivDataCallback.forEach((rec) => { this.cfg.receivDataCallback.forEach((
rec) => {
if (rec.callback) { if (rec.callback) {
rec.callback(recData, f, path); rec.callback(recData, f,
path);
} }
}) })
}else{ } else {
console.log("无人订阅消息"); console.log("无人订阅消息");
} }
} else { } else {
console.log("无人订阅receivDataCallback,不处理数据"); console.log("无人订阅receivDataCallback,不处理数据");
} }
}catch(ex){ } catch (ex) {
console.log("ex=",ex); console.log("ex=", ex);
} }
}); });
@ -687,9 +692,10 @@ class BleHelper {
} }
// console.log("c=", c); // console.log("c=", c);
let startSubScribe = (id, serviceId, characteristicId) => { let startSubScribe = (id, serviceId, characteristicId) => {
console.log("serviceId=",serviceId); console.log("serviceId=", serviceId);
console.log("characteristicId=",characteristicId); console.log("characteristicId=", characteristicId);
return new Promise((succ, err) => { let p1= new Promise((succ, err) => {
uni.notifyBLECharacteristicValueChange({ uni.notifyBLECharacteristicValueChange({
deviceId: id, deviceId: id,
serviceId: serviceId, serviceId: serviceId,
@ -707,7 +713,8 @@ class BleHelper {
v.notifyState = state; v.notifyState = state;
} }
}); });
succ(); console.log("success SubScribe");
succ(res);//见了鬼了有时候执行了succ但promise永远pending了
}, },
fail: (ex) => { 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(); 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++) { for (var i = 0; i < c.Characteristics.length; i++) {
let item = c.Characteristics[i]; let item = c.Characteristics[i];
let serviceId = item.serviceId; let serviceId = item.serviceId;
@ -730,8 +753,10 @@ class BleHelper {
} }
if (promies.length > 0) { if (promies.length > 0) {
Promise.allSettled(promies).then((results) => {
console.log("234324324324");
Promise.allSettled(promies).then((results) => {
console.log("11111");
results.forEach((result, index) => { results.forEach((result, index) => {
if (result.status === "fulfilled") { if (result.status === "fulfilled") {
console.log(`操作${index + 1}成功:`, result.value); console.log(`操作${index + 1}成功:`, result.value);
@ -743,9 +768,13 @@ class BleHelper {
resolve(); resolve();
}).catch((ex) => { }).catch((ex) => {
console.log("222222");
reject(ex); reject(ex);
}).finally(()=>{
console.log("finally")
}); });
} else { } else {
console.log("33333");
resolve(); resolve();
} }
@ -971,7 +1000,9 @@ class BleHelper {
} }
if (!this.data.available) { if (!this.data.available) {
reject(this.getError({ code: 10001 })); reject(this.getError({
code: 10001
}));
return; return;
} }
@ -996,8 +1027,10 @@ class BleHelper {
this.data.LinkedList.push(cr); this.data.LinkedList.push(cr);
} }
uni.setStorageSync(this.StorageKey, this.data.LinkedList); uni.setStorageSync(this.StorageKey, this.data
console.log("LinkedList=", this.data.LinkedList); .LinkedList);
console.log("LinkedList=", this.data
.LinkedList);
// 处理 MTU 设置 // 处理 MTU 设置
if (plus.os.name === 'Android') { if (plus.os.name === 'Android') {
@ -1005,12 +1038,14 @@ class BleHelper {
deviceId: deviceId, deviceId: deviceId,
mtu: 512, mtu: 512,
success: (mtu) => { success: (mtu) => {
console.log("mtu设置成功", mtu); console.log("mtu设置成功",
mtu);
resolve(true); resolve(true);
}, },
fail: () => { fail: () => {
console.log("mtu设置失败"); console.log("mtu设置失败");
resolve(true); // MTU设置失败不影响连接成功 resolve(
true); // MTU设置失败不影响连接成功
} }
}); });
} else { } else {
@ -1059,15 +1094,15 @@ class BleHelper {
return this.getService(deviceId, targetServiceId, writeCharId, return this.getService(deviceId, targetServiceId, writeCharId,
notifyCharId); //获取服务 notifyCharId); //获取服务
} else { } else {
if(f.wirteCharactId && f.notifyCharactId){ if (f.wirteCharactId && f.notifyCharactId) {
if (!f.notifyState) { if (!f.notifyState) {
console.log("开始订阅特征"); console.log("开始订阅特征");
this.subScribe(deviceId, true); this.subScribe(deviceId, true);
}else{ } else {
console.log("不订阅消息"); console.log("不订阅消息");
} }
return Promise.resolve(true); return Promise.resolve(true);
}else{ } else {
console.log("开始获取服务", targetServiceId) console.log("开始获取服务", targetServiceId)
return this.getService(deviceId, targetServiceId, writeCharId, return this.getService(deviceId, targetServiceId, writeCharId,
notifyCharId); notifyCharId);
@ -1085,13 +1120,13 @@ class BleHelper {
} }
}).then(() => { }).then(() => {
console.log("then.....");
setTimeout(() => { setTimeout(() => {
resolve(); resolve();
}, 500); }, 500);
}).catch((ex) => { }).catch((ex) => {
////console.log("出现异常", ex); console.log("出现异常", ex);
reject(ex); reject(ex);
}); });
}); });