From f750d15150ae01b1e2a6a5daf9ad5d3e5ad44380 Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Mon, 24 Aug 2026 17:06:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(device):=20=E4=BF=AE=E5=A4=8D=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=88=87=E6=8D=A2=E8=BF=9E=E6=8E=A5=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E7=B4=AF=E7=A7=AF=E4=B8=8E=E8=87=AA=E5=8A=A8=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=EF=BC=8C=E8=BF=9E=E6=8E=A5=E5=90=8E=E6=9B=B4=E6=96=B0=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- miniprogram/lefu/service.ts | 7 +- miniprogram/pages/equipment/equipment.less | 17 +++-- miniprogram/pages/equipment/equipment.ts | 80 +++++++++++++++++++++- miniprogram/pages/equipment/equipment.wxml | 41 ++++++++--- 4 files changed, 120 insertions(+), 25 deletions(-) diff --git a/miniprogram/lefu/service.ts b/miniprogram/lefu/service.ts index 9fd902a..1d350c6 100644 --- a/miniprogram/lefu/service.ts +++ b/miniprogram/lefu/service.ts @@ -94,6 +94,7 @@ class LeFuService { */ startScan(): void { console.log(TAG, 'startScan()') + this._intentionalConnect = true plugin.Blue.setDeviceSetting(DEVICE_SETTINGS) this._subscribeBus() plugin.Blue.start(DEVICE_SETTINGS.map(s => s.deviceName), false) @@ -185,11 +186,9 @@ class LeFuService { } return } - // 蓝牙就绪:如有缓存设备且非主动断开,尝试重连 + // 蓝牙就绪:非主动操作(意外断开)且有缓存设备时自动重连,主动扫描/连接期间不重连 if (res === plugin.BLUE_STATE.READY) { - if (this._intentionalConnect) { - this._intentionalConnect = false - } else if (this._lastRawDevice && !this._activeProtocol) { + if (!this._intentionalConnect && this._lastRawDevice && !this._activeProtocol && this._onDevicesListCbs.size === 0) { this._stopReconnectTimer() wx.openBluetoothAdapter({ success: () => this.connect(this._lastRawDevice!), diff --git a/miniprogram/pages/equipment/equipment.less b/miniprogram/pages/equipment/equipment.less index af5ea13..c4e4a1f 100644 --- a/miniprogram/pages/equipment/equipment.less +++ b/miniprogram/pages/equipment/equipment.less @@ -128,7 +128,6 @@ overflow: hidden; margin-right: 24rpx; border-radius: 24rpx; - background-color: #1385FA; } .device-info { @@ -187,17 +186,17 @@ font-size: 22rpx; line-height: 22rpx; } + } - &.connected { - background-color: #EAFAF6; + .connected { + background-color: #EAFAF6 !important; - &.status-dot { - background-color: #2AC79F; - } + .status-dot { + background-color: #2AC79F !important; + } - &.status-text { - color: #2AC79F; - } + .status-text { + color: #2AC79F !important; } } } diff --git a/miniprogram/pages/equipment/equipment.ts b/miniprogram/pages/equipment/equipment.ts index 0b48968..9476aa0 100644 --- a/miniprogram/pages/equipment/equipment.ts +++ b/miniprogram/pages/equipment/equipment.ts @@ -1,4 +1,5 @@ import { get } from '../../utils/request/index' +import { leFuService } from '../../lefu/index' import type { UserInfo } from '../../api/index' /** 体重设备(对应 /weighingScale/list/device 返回的 result 元素) */ @@ -30,6 +31,10 @@ interface DeviceItem { connectDeviceInfo: string } +let unsubDevicesList: (() => void) | null = null +let unsubConnect: (() => void) | null = null +let scanTimer: number | null = null + Page({ data: { devices: [] as DeviceItem[], @@ -57,7 +62,7 @@ Page({ return { id: item.id, name: info.name ?? '', - status: false, + status: leFuService.isConnected && leFuService.lastRawDevice?.deviceId === info.deviceId, deviceId: info.deviceId ?? '', sn: item.scaleDeviceId, userNum: item.userNum ?? 0, @@ -81,6 +86,79 @@ Page({ }) }, + /** 点击设备状态:已连接则断开,未连接则扫描连接 */ + onTapStatus(e: WechatMiniprogram.TouchEvent) { + const item = e.currentTarget.dataset.item as DeviceItem + if (item.status) { + leFuService.disconnect() + this._updateStatus(item.deviceId, false) + return + } + this._connectDevice(item) + }, + + /** 扫描蓝牙,找到 deviceId 匹配的设备后连接 */ + _connectDevice(item: DeviceItem) { + const targetDeviceId = item.deviceId + if (!targetDeviceId) return + if (leFuService.isConnected) { + leFuService.disconnect() + // 断开后全部置未连接,等新设备连上再标回 + this.setData({ + devices: this.data.devices.map(d => ({ ...d, status: false })), + }) + } + + // 清理上一次连接流程的订阅与定时器,避免重复订阅累积 + unsubDevicesList?.() + unsubDevicesList = null + unsubConnect?.() + unsubConnect = null + if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } + + wx.showLoading({ title: '连接中...', mask: true }) + wx.openBluetoothAdapter({ + success: () => { + scanTimer = setTimeout(() => { + unsubDevicesList?.() + unsubDevicesList = null + wx.hideLoading() + wx.showToast({ title: '未找到设备', icon: 'none' }) + }, 15000) + + unsubDevicesList = leFuService.onDevicesList((devices) => { + const matched = devices.find(d => d.raw.deviceId === targetDeviceId) + if (!matched) return + if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } + unsubDevicesList?.() + unsubDevicesList = null + leFuService.stopScan() + leFuService.connect(matched.raw) + unsubConnect = leFuService.onDeviceConnect(() => { + unsubConnect?.() + unsubConnect = null + wx.hideLoading() + wx.setStorageSync('connectDeviceInfo', matched.raw) + this._updateStatus(targetDeviceId, true) + }) + }) + leFuService.startScan() + }, + fail: () => { + wx.hideLoading() + wx.showToast({ title: '蓝牙未开启', icon: 'none' }) + }, + }) + }, + + _updateStatus(deviceId: string, status: boolean) { + this.setData({ + devices: this.data.devices.map(item => + item.deviceId === deviceId ? { ...item, status } : item + ), + }) + }, + /** 底部操作:数据补传 / 设备连接 弹 loading */ onTapAction(e: WechatMiniprogram.TouchEvent) { const action = e.currentTarget.dataset.action as string diff --git a/miniprogram/pages/equipment/equipment.wxml b/miniprogram/pages/equipment/equipment.wxml index cb1e968..56d93d4 100644 --- a/miniprogram/pages/equipment/equipment.wxml +++ b/miniprogram/pages/equipment/equipment.wxml @@ -1,9 +1,9 @@ - - - - + + 当前为:运维模式 + 关闭运维模式 + @@ -22,17 +22,26 @@ + - + + + {{ item.name }} 取消配对 - + {{ item.status ? '已连接' : '未连接' }} @@ -53,12 +62,22 @@ - - - + + + + + + 设备WIFI - 设备WIFI - + + + + + + + 设备连接 + +