From e4d11d30a8a9e6e89afaad036e19e8fc91d18fbc Mon Sep 17 00:00:00 2001 From: Guoguo Date: Fri, 5 Jun 2026 22:30:01 -0700 Subject: [PATCH] feat: add BLE scan/connect/service discovery console logs --- miniprogram/services/ble/connection.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/miniprogram/services/ble/connection.js b/miniprogram/services/ble/connection.js index 9105085..aad675b 100644 --- a/miniprogram/services/ble/connection.js +++ b/miniprogram/services/ble/connection.js @@ -204,9 +204,11 @@ function discoverServices(deviceId, callbacks) { deviceId: deviceId, success: function (res) { var services = res.services + console.log('[BLE] 发现服务数量:', services.length) var serviceMap = {} for (var i = 0; i < services.length; i++) { var uuid = services[i].uuid.toUpperCase() + console.log('[BLE] 服务:', uuid) if (uuid.indexOf(SERVICE.DEVICE_INFO) !== -1) { serviceMap.deviceInfo = services[i].uuid } else if (uuid.indexOf(SERVICE.DATA_COMM) !== -1) { @@ -215,6 +217,7 @@ function discoverServices(deviceId, callbacks) { serviceMap.ota = services[i].uuid } } + console.log('[BLE] 匹配服务:', JSON.stringify(serviceMap)) var tasks = [] if (serviceMap.deviceInfo) { @@ -252,16 +255,19 @@ function startScan(callbacks) { var d = devices[i] var name = (d.name || '').toUpperCase() var localName = (d.localName || '').toUpperCase() + console.log('[BLE] 发现设备:', d.name, '| localName:', d.localName, '| deviceId:', d.deviceId) var matched = false if (targetName) { matched = name === targetName || localName === targetName } else { - matched = name.indexOf('JW_') !== -1 || localName.indexOf('JW_') !== -1 || + matched = localName.indexOf('JW_') !== -1 || + name.indexOf('JW_') !== -1 || name.indexOf('HOX') !== -1 || localName.indexOf('HOX') !== -1 || name.indexOf('LIGHTMASK') !== -1 || localName.indexOf('LIGHTMASK') !== -1 || name.indexOf('SIMPLE PERIPHERAL') !== -1 || localName.indexOf('SIMPLE PERIPHERAL') !== -1 } if (matched) { + console.log('[BLE] 匹配成功:', d.name, '| localName:', d.localName, targetName ? '(精确匹配)' : '(模糊匹配)') wx.stopBluetoothDevicesDiscovery({}) if (callbacks.onFound) callbacks.onFound(d) connect(d.deviceId, callbacks) @@ -287,6 +293,7 @@ function stopScan() { } function connect(deviceId, callbacks) { + console.log('[BLE] 开始连接:', deviceId) _deviceId = deviceId _chars = {} @@ -294,12 +301,14 @@ function connect(deviceId, callbacks) { deviceId: deviceId, timeout: 10000, success: function () { + console.log('[BLE] 连接成功:', deviceId) _connected = true requestMtu(deviceId).then(function () { discoverServices(deviceId, callbacks) }) }, - fail: function () { + fail: function (err) { + console.log('[BLE] 连接失败:', deviceId, err) _connected = false if (callbacks.onError) callbacks.onError({ msg: '连接失败' }) }