// 写入特征值
async writeCharacteristicValue(deviceId:string,serviceId:string,characteristicId:string,value:any,retryCount?:Number,callStatus?:boolean):Promise<Number>{
if(callStatus){
if(deviceMap[deviceId].subCharacteristic[characteristicId]){
await this.subscribe(deviceId,serviceId,characteristicId);
}
}
const write = new Promise((resolve,reject)=>{
console.log(deviceId,serviceId,characteristicId,value)
uni.writeBLECharacteristicValue({
deviceId: deviceId,
serviceId: serviceId,
characteristicId: characteristicId,
// writeType:'writeNoResponse',
value: value,
success: (res) => {
resolve(res);
},
fail: async (error) => {
console.log("失败")
await new Promise(reject=>setTimeout(reject,300));
if(retryCount){
console.log("当前失败retryCount:"+retryCount+"次");
const a = await this.writeCharacteristicValue(deviceId,serviceId,characteristicId,value,Number(retryCount)-1);
console.log(a)
if(a == 1){
resolve(1);
}else{
resolve(0);
}
}else{
// uni.hideLoading();
// uni.showToast({
// title: "发送失败",
// icon: "error",
// duration: 1000
// })
// resolve(error);
reject(error);
}
// reject(error);
},
complete: (res) => {
console.log("通用")
}
})
})
let status = 0;
await write.then(res => {
status = 1;
}).catch(res => {
status = 0;
});
return status;
}
0 个回复