diff --git a/packages/kit-bg/src/services/ServiceHardware.ts b/packages/kit-bg/src/services/ServiceHardware.ts index 056a8e765c6..381dedf9127 100644 --- a/packages/kit-bg/src/services/ServiceHardware.ts +++ b/packages/kit-bg/src/services/ServiceHardware.ts @@ -42,6 +42,7 @@ import { import { isPassphraseWallet } from '@onekeyhq/shared/src/engine/engineUtils'; import debugLogger from '@onekeyhq/shared/src/logger/debugLogger'; import platformEnv from '@onekeyhq/shared/src/platformEnv'; +import { createPromiseWithTimeout } from '@onekeyhq/shared/src/utils/promiseUtils'; import { equalsIgnoreCase } from '@onekeyhq/shared/src/utils/stringUtils'; import type { EOnekeyDomain, @@ -433,10 +434,11 @@ class ServiceHardware extends ServiceBase { maxTryCount = 10, bootloaderMode = false, ) { + const hardwareSDK = await this.getSDKInstance(); return new Promise((resolve) => { let tryCount = 0; deviceUtils.startDeviceScan( - (response) => { + async (response) => { tryCount += 1; if (tryCount > maxTryCount) { deviceUtils.stopScan(); @@ -451,9 +453,16 @@ class ServiceHardware extends ServiceBase { : (response.payload ?? []).find((d) => equalsIgnoreCase(d.connectId, connectId), ); + if (deviceExist) { - deviceUtils.stopScan(); - resolve(true); + const res = await createPromiseWithTimeout( + hardwareSDK.getFeatures(connectId), + 2000, + ); + if (res?.success) { + deviceUtils.stopScan(); + resolve(true); + } } }, () => {}, @@ -470,7 +479,6 @@ class ServiceHardware extends ServiceBase { // eslint-disable-next-line no-async-promise-executor return new Promise(async (resolve) => { const hardwareSDK = await this.getSDKInstance(); - // restart count down await wait(8000); let tryCount = 0; // polling device when restart success diff --git a/packages/shared/src/utils/promiseUtils.ts b/packages/shared/src/utils/promiseUtils.ts index 23459eaf521..479cafd87a3 100644 --- a/packages/shared/src/utils/promiseUtils.ts +++ b/packages/shared/src/utils/promiseUtils.ts @@ -18,4 +18,25 @@ const createAnyPromise = (promises: Promise[]): Promise => (val) => Promise.resolve(val), ); -export { createDelayPromise, createAnyPromise }; +function createPromiseWithTimeout( + promise: Promise, + timeout: number, +): Promise { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => { + reject(new Error('Operation timed out')); + }, timeout); + + promise + .then((value) => { + clearTimeout(timer); + resolve(value); + }) + .catch((err) => { + clearTimeout(timer); + reject(err); + }); + }); +} + +export { createDelayPromise, createAnyPromise, createPromiseWithTimeout };