From 5bce7060ac43a88321b2b806901b5d5b5dad9024 Mon Sep 17 00:00:00 2001 From: Guoguo Date: Fri, 5 Jun 2026 22:38:07 -0700 Subject: [PATCH] fix: BLE scan match use indexOf instead of exact equals Fixes timeout during binding when targetDeviceId is set but exact string match fails due to encoding differences. Also skips devices with no name, and logs match result for every candidate. --- miniprogram/services/ble/connection.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/miniprogram/services/ble/connection.js b/miniprogram/services/ble/connection.js index aad675b..1dad359 100644 --- a/miniprogram/services/ble/connection.js +++ b/miniprogram/services/ble/connection.js @@ -255,10 +255,11 @@ function startScan(callbacks) { var d = devices[i] var name = (d.name || '').toUpperCase() var localName = (d.localName || '').toUpperCase() + if (!name && !localName) continue console.log('[BLE] 发现设备:', d.name, '| localName:', d.localName, '| deviceId:', d.deviceId) var matched = false if (targetName) { - matched = name === targetName || localName === targetName + matched = name.indexOf(targetName) !== -1 || localName.indexOf(targetName) !== -1 } else { matched = localName.indexOf('JW_') !== -1 || name.indexOf('JW_') !== -1 || @@ -266,8 +267,8 @@ function startScan(callbacks) { name.indexOf('LIGHTMASK') !== -1 || localName.indexOf('LIGHTMASK') !== -1 || name.indexOf('SIMPLE PERIPHERAL') !== -1 || localName.indexOf('SIMPLE PERIPHERAL') !== -1 } + console.log('[BLE] 匹配结果:', matched, targetName ? '(精确:' + targetName + ')' : '(模糊)') if (matched) { - console.log('[BLE] 匹配成功:', d.name, '| localName:', d.localName, targetName ? '(精确匹配)' : '(模糊匹配)') wx.stopBluetoothDevicesDiscovery({}) if (callbacks.onFound) callbacks.onFound(d) connect(d.deviceId, callbacks)