From fb1c13e0862ad0d985549ac7d960f8765295680c Mon Sep 17 00:00:00 2001 From: woan <798680981@qq.com> Date: Thu, 25 Jun 2026 14:59:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E8=87=AA=E5=8A=A8=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=9E=8B=E8=AE=BE=E5=A4=87=E8=AF=AF=E5=88=A4=20not=20found=20i?= =?UTF-8?q?n=20BLE=20scan(=E5=BD=A9=E7=81=AF=E7=AD=89)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 彩灯等设备配对后自动连接、直接进配网页,无"从扫描列表选设备"步 → waitForDeviceInScan 等不到设备名元素 30s 超时报 not found in BLE scan,实际已在配网页。 修:waitForDeviceInScan 增 advanceKeywords,轮询时若已直达配网/连接页则提前返回;Step3 未选到设备时检查"是否已推进到配网/连接页",已推进则跳过选设备步继续。 验证:Color Bulb 现完整 PASS(自动连接→ASUS_30 配网成功 15s→落账号)。 Co-Authored-By: Claude Opus 4.8 --- utils/common/device-add.helper.ts | 64 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 27 deletions(-) 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} 已自动连接/直达配网页(无扫描列表选择步,跳过)`); } }