feat: add BLE scan/connect/service discovery console logs

这个提交包含在:
Guoguo
2026-06-05 22:30:01 -07:00
父节点 810a3a19a5
当前提交 e4d11d30a8
+11 -2
查看文件
@@ -204,9 +204,11 @@ function discoverServices(deviceId, callbacks) {
deviceId: deviceId, deviceId: deviceId,
success: function (res) { success: function (res) {
var services = res.services var services = res.services
console.log('[BLE] 发现服务数量:', services.length)
var serviceMap = {} var serviceMap = {}
for (var i = 0; i < services.length; i++) { for (var i = 0; i < services.length; i++) {
var uuid = services[i].uuid.toUpperCase() var uuid = services[i].uuid.toUpperCase()
console.log('[BLE] 服务:', uuid)
if (uuid.indexOf(SERVICE.DEVICE_INFO) !== -1) { if (uuid.indexOf(SERVICE.DEVICE_INFO) !== -1) {
serviceMap.deviceInfo = services[i].uuid serviceMap.deviceInfo = services[i].uuid
} else if (uuid.indexOf(SERVICE.DATA_COMM) !== -1) { } else if (uuid.indexOf(SERVICE.DATA_COMM) !== -1) {
@@ -215,6 +217,7 @@ function discoverServices(deviceId, callbacks) {
serviceMap.ota = services[i].uuid serviceMap.ota = services[i].uuid
} }
} }
console.log('[BLE] 匹配服务:', JSON.stringify(serviceMap))
var tasks = [] var tasks = []
if (serviceMap.deviceInfo) { if (serviceMap.deviceInfo) {
@@ -252,16 +255,19 @@ function startScan(callbacks) {
var d = devices[i] var d = devices[i]
var name = (d.name || '').toUpperCase() var name = (d.name || '').toUpperCase()
var localName = (d.localName || '').toUpperCase() var localName = (d.localName || '').toUpperCase()
console.log('[BLE] 发现设备:', d.name, '| localName:', d.localName, '| deviceId:', d.deviceId)
var matched = false var matched = false
if (targetName) { if (targetName) {
matched = name === targetName || localName === targetName matched = name === targetName || localName === targetName
} else { } 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('HOX') !== -1 || localName.indexOf('HOX') !== -1 ||
name.indexOf('LIGHTMASK') !== -1 || localName.indexOf('LIGHTMASK') !== -1 || name.indexOf('LIGHTMASK') !== -1 || localName.indexOf('LIGHTMASK') !== -1 ||
name.indexOf('SIMPLE PERIPHERAL') !== -1 || localName.indexOf('SIMPLE PERIPHERAL') !== -1 name.indexOf('SIMPLE PERIPHERAL') !== -1 || localName.indexOf('SIMPLE PERIPHERAL') !== -1
} }
if (matched) { if (matched) {
console.log('[BLE] 匹配成功:', d.name, '| localName:', d.localName, targetName ? '(精确匹配)' : '(模糊匹配)')
wx.stopBluetoothDevicesDiscovery({}) wx.stopBluetoothDevicesDiscovery({})
if (callbacks.onFound) callbacks.onFound(d) if (callbacks.onFound) callbacks.onFound(d)
connect(d.deviceId, callbacks) connect(d.deviceId, callbacks)
@@ -287,6 +293,7 @@ function stopScan() {
} }
function connect(deviceId, callbacks) { function connect(deviceId, callbacks) {
console.log('[BLE] 开始连接:', deviceId)
_deviceId = deviceId _deviceId = deviceId
_chars = {} _chars = {}
@@ -294,12 +301,14 @@ function connect(deviceId, callbacks) {
deviceId: deviceId, deviceId: deviceId,
timeout: 10000, timeout: 10000,
success: function () { success: function () {
console.log('[BLE] 连接成功:', deviceId)
_connected = true _connected = true
requestMtu(deviceId).then(function () { requestMtu(deviceId).then(function () {
discoverServices(deviceId, callbacks) discoverServices(deviceId, callbacks)
}) })
}, },
fail: function () { fail: function (err) {
console.log('[BLE] 连接失败:', deviceId, err)
_connected = false _connected = false
if (callbacks.onError) callbacks.onError({ msg: '连接失败' }) if (callbacks.onError) callbacks.onError({ msg: '连接失败' })
} }