diff --git a/utils/common/device-add.helper.ts b/utils/common/device-add.helper.ts index 315d5b1..a0cfe55 100644 --- a/utils/common/device-add.helper.ts +++ b/utils/common/device-add.helper.ts @@ -208,20 +208,23 @@ export async function selectDeviceCategory(driver: DeviceDriver, categoryName: s export async function waitForDeviceInScan( driver: DeviceDriver, deviceKeyword: string, - timeout = 30000 + timeout = 30000, + advanceKeywords?: string[] ): Promise { - if (driver.platform === 'android') { - const start = Date.now(); - while (Date.now() - start < timeout) { - const el = await driver.findElementRaw('-android uiautomator', `new UiSelector().textContains("${deviceKeyword}")`); - if (el) return el; - await sleep(1000); + // 轮询:出现"扫描列表里的设备项"→返回它;若已**直达配网/连接页**(自动连接型设备,无选设备步)→返回 null(由调用方识别"已推进")。 + const start = Date.now(); + while (Date.now() - start < timeout) { + const el = driver.platform === 'android' + ? await driver.findElementRaw('-android uiautomator', `new UiSelector().textContains("${deviceKeyword}")`).catch(() => null) + : await driver.findElementRaw('name', deviceKeyword).catch(() => null); + if (el) return el; + if (advanceKeywords && advanceKeywords.length) { + const src = await driver.getSource(); + if (advanceKeywords.some((k) => src.includes(k)) || /Configure Wi-?Fi|Wi-?Fi password|Enter Wi-?Fi|Connecting|Verifying|连接中|正在连接/i.test(src)) return null; } - return null; + await sleep(1000); } - - // iOS - return await waitForElement(driver, 'name', deviceKeyword, timeout); + return null; } export async function waitForConnection( @@ -270,24 +273,31 @@ export async function addDeviceViaBLE(driver: DeviceDriver, options: AddDeviceOp // Step 3: Wait for device in BLE scan (skip for auto-connect devices like AI Hub) if (!skipScanStep) { - const deviceEl = await waitForDeviceInScan(driver, deviceKeyword, scanTimeout); - if (!deviceEl) { - console.log(`FAIL: ${deviceKeyword} not found in BLE scan`); - return false; - } - await driver.tapElement(deviceEl); - await sleep(500); + const advanceKw = connectionKeywords || DEFAULT_CONNECTION_KEYWORDS; + const deviceEl = await waitForDeviceInScan(driver, deviceKeyword, scanTimeout, advanceKw); + if (deviceEl) { + await driver.tapElement(deviceEl); + await sleep(500); - // Step 4: Tap Next (some products skip this) - if (!skipNextButton) { - if (driver.platform === 'android') { - const nextEl = await driver.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")'); - if (nextEl) await driver.tapElement(nextEl); - } else { - const nextEl = await driver.findElementRaw('name', 'Next'); - if (nextEl) await driver.tapElement(nextEl); + // Step 4: Tap Next (some products skip this) + if (!skipNextButton) { + if (driver.platform === 'android') { + const nextEl = await driver.findElementRaw('-android uiautomator', 'new UiSelector().text("Next")'); + if (nextEl) await driver.tapElement(nextEl); + } else { + const nextEl = await driver.findElementRaw('name', 'Next'); + if (nextEl) await driver.tapElement(nextEl); + } + await sleep(2000); } - await sleep(2000); + } else { + // 没在扫描列表选到设备 → 但很多设备(彩灯等)是**自动连接、直接进配网/连接页**(无"从列表选设备"步)。 + // 已进配网/连接页则跳过选择继续(原来一律报 not found → 误判失败,实际已在配网页)。 + const src = await driver.getSource(); + const advanced = advanceKw.some((k) => src.includes(k)) + || /Configure Wi-?Fi|Wi-?Fi password|Enter Wi-?Fi|Connecting|Verifying|连接中|正在连接/i.test(src); + if (!advanced) { console.log(`FAIL: ${deviceKeyword} not found in BLE scan`); return false; } + console.log(`${deviceKeyword} 已自动连接/直达配网页(无扫描列表选择步,跳过)`); } }