递归获取设备服务
_getCharacteristics(serviceId: string) {
const deviceId = this._deviceId;
const maxAttempts = 10;
let attempt = 0;
const tryGetCharacteristics = (): Promise<any[]> => {
return new Promise((resolve, reject) => {
uni.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success(res: any) {
if (res.characteristics && res.characteristics.length > 0) {
resolve(res.characteristics);
} else if (attempt < maxAttempts - 1) {
attempt++;
setTimeout(() => {
tryGetCharacteristics().then(resolve).catch(reject);
}, 1000);
} else {
resolve(res.characteristics);
}
},
fail(e: any) {
if (attempt < maxAttempts - 1) {
attempt++;
setTimeout(() => {
tryGetCharacteristics().then(resolve).catch(reject);
}, 1000);
} else {
reject(new BluetoothError(e));
}
},
});
});
};
return tryGetCharacteristics();
}
递归获取设备特征值
_getCharacteristics(serviceId: string) {
const deviceId = this._deviceId;
const maxAttempts = 10;
let attempt = 0;
const tryGetCharacteristics = (): Promise<any[]> => {
return new Promise((resolve, reject) => {
uni.getBLEDeviceCharacteristics({
deviceId,
serviceId,
success(res: any) {
if (res.characteristics && res.characteristics.length > 0) {
resolve(res.characteristics);
} else if (attempt < maxAttempts - 1) {
attempt++;
setTimeout(() => {
tryGetCharacteristics().then(resolve).catch(reject);
}, 1000);
} else {
resolve(res.characteristics);
}
},
fail(e: any) {
if (attempt < maxAttempts - 1) {
attempt++;
setTimeout(() => {
tryGetCharacteristics().then(resolve).catch(reject);
}, 1000);
} else {
reject(new BluetoothError(e));
}
},
});
});
};
return tryGetCharacteristics();
}
0 个回复