解决订阅消息有时候会永远pending的问题
This commit is contained in:
@ -473,9 +473,12 @@ class BleHelper {
|
|||||||
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
|
||||||
@ -519,10 +522,12 @@ class BleHelper {
|
|||||||
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 {
|
||||||
@ -689,7 +694,8 @@ class BleHelper {
|
|||||||
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,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();
|
let promies = new Array();
|
||||||
if (c.Characteristics && c.Characteristics.length) {
|
if (c.Characteristics && c.Characteristics.length) {
|
||||||
@ -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 {
|
||||||
@ -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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user