From 24d36774152255068ee02f4005ec7584dec52762 Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Fri, 15 May 2026 16:54:09 +0800 Subject: [PATCH] =?UTF-8?q?[AI=20Generated]:=20feat(equipment):=20?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E4=B8=8E=E8=87=AA=E5=8A=A8=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E6=89=AB=E6=8F=8F=E5=8C=B9=E9=85=8D=20device?= =?UTF-8?q?Id=20=E5=90=8E=E5=86=8D=E5=BB=BA=E7=AB=8B=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/pages/equipment/equipment.ts | 78 ++++++++++++++++-------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/miniprogram/pages/equipment/equipment.ts b/miniprogram/pages/equipment/equipment.ts index 0ce7d57..5b2c1e1 100644 --- a/miniprogram/pages/equipment/equipment.ts +++ b/miniprogram/pages/equipment/equipment.ts @@ -93,10 +93,11 @@ Page({ }) }, - /** 检查缓存设备:已连接则直接标记,未连接则扫描自动连接 */ + /** 检查缓存设备:已连接则直接标记,未连接则扫描匹配 deviceId 后自动连接 */ _checkAndAutoConnect() { const cachedRaw = wx.getStorageSync('connectDeviceInfo') as RawDevice | null - if (!cachedRaw) return + const targetDeviceId = cachedRaw?.deviceId as string | undefined + if (!targetDeviceId) return // 已连接且 deviceInfo 就绪,直接标记列表 if (lefuService.isConnected && lefuService.deviceInfo?.serialNumber) { @@ -104,19 +105,34 @@ Page({ return } - // 未连接,直接用缓存对象连,不走扫描 + let scanTimer: number | null = null + wx.showLoading({ title: '连接中...', mask: true }) wx.openBluetoothAdapter({ success: () => { - lefuService.connect(cachedRaw) - lefuService.onDeviceConnect(() => { + scanTimer = setTimeout(() => { + lefuService.stopScan() wx.hideLoading() + }, 15000) + + lefuService.onDevicesList((devices) => { + const matched = devices.find(d => d.raw.deviceId === targetDeviceId) + if (!matched) return + if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } + lefuService.stopScan() + lefuService.connect(matched.raw) + lefuService.onDeviceConnect(() => { + wx.hideLoading() + wx.setStorageSync('connectDeviceInfo', matched.raw) + }) + lefuService.onDeviceInfo((info) => { + this._markConnected(info.serialNumber as string) + }) }) - lefuService.onDeviceInfo((info) => { - this._markConnected(info.serialNumber as string) - }) + lefuService.startScan() }, fail: () => { + if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } wx.hideLoading() }, }) @@ -131,32 +147,46 @@ Page({ this.setData({ deviceList: list }) }, - /** 点击「连接」:断开当前、直接用 connectDeviceInfo 连接指定设备 */ + /** 点击「连接」:扫描周边设备,找到 deviceId 匹配的设备后连接 */ onTapConnect(e: WechatMiniprogram.TouchEvent) { const connectDeviceInfoStr = e.currentTarget.dataset.connectDeviceInfo as string - let rawDevice: RawDevice | null = null + let cachedDevice: RawDevice | null = null try { - rawDevice = JSON.parse(connectDeviceInfoStr || '{}') + cachedDevice = JSON.parse(connectDeviceInfoStr || '{}') } catch {} - if (!rawDevice) return + const targetDeviceId = cachedDevice?.deviceId as string | undefined + if (!targetDeviceId) return - if (lefuService.isConnected) { - lefuService.disconnect() - } - const list = this.data.deviceList.map(item => ({ ...item, connected: false })) - this.setData({ deviceList: list }) + if (lefuService.isConnected) lefuService.disconnect() + this.setData({ deviceList: this.data.deviceList.map(item => ({ ...item, connected: false })) }) - wx.showLoading({ title: '连接中...', mask: true }) + let scanTimer: number | null = null + + wx.showLoading({ title: '扫描中...', mask: true }) wx.openBluetoothAdapter({ success: () => { - lefuService.connect(rawDevice!) - lefuService.onDeviceConnect(() => { + scanTimer = setTimeout(() => { + lefuService.stopScan() wx.hideLoading() - wx.setStorageSync('connectDeviceInfo', rawDevice) - }) - lefuService.onDeviceInfo((info) => { - this._markConnected(info.serialNumber as string) + wx.showToast({ title: '未找到设备', icon: 'none' }) + }, 15000) + + lefuService.onDevicesList((devices) => { + const matched = devices.find(d => d.raw.deviceId === targetDeviceId) + if (!matched) return + if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } + lefuService.stopScan() + wx.showLoading({ title: '连接中...', mask: true }) + lefuService.connect(matched.raw) + lefuService.onDeviceConnect(() => { + wx.hideLoading() + wx.setStorageSync('connectDeviceInfo', matched.raw) + }) + lefuService.onDeviceInfo((info) => { + this._markConnected(info.serialNumber as string) + }) }) + lefuService.startScan() }, fail: () => { wx.hideLoading()