From 15e089f5e397554b323a2286c58d725c9dac3aea Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Mon, 20 Jul 2026 16:08:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(equipment):=20=E8=BF=9B=E5=85=A5=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E4=B8=8D=E5=86=8D=E4=BF=A1=E4=BB=BB=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=9A=84=E8=93=9D=E7=89=99=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81?= =?UTF-8?q?=EF=BC=8C=E6=AF=8F=E6=AC=A1=E8=B5=B0=E7=9C=9F=E5=AE=9E=E6=89=AB?= =?UTF-8?q?=E6=8F=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - _checkAndAutoConnect 去掉 isConnected+deviceInfo 缓存短路,始终走 autoConnect - autoConnect 去掉 isConnected 短路,每次真实扫描 - connect() 先断开旧连接再建新连接,避免新旧连接冲突 - connectState CONNECTFAILED 时重置 _intentionalConnect,防止标记卡死 - deviceConnect handler 移除冗余的 _connectState 赋值 Co-Authored-By: Claude Opus 4.7 --- miniprogram/lefu/service.ts | 22 +++++++++++++--------- miniprogram/pages/equipment/equipment.ts | 9 +-------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/miniprogram/lefu/service.ts b/miniprogram/lefu/service.ts index b92b824..61dd54e 100644 --- a/miniprogram/lefu/service.ts +++ b/miniprogram/lefu/service.ts @@ -20,10 +20,10 @@ const plugin = requirePlugin('ppScale-plugin') as LeFuPlugin const LEFU_CONFIG: LeFuConfig = { key: 'lefudc91611833e18d94', secret: 'eQ50JYimMp0X7uhNLj6D3lTEk4TUUvEG7dvaqKM9t1U=', - domain1: 'http://10.10.10.10:8889', - domain2: 'http://10.10.10.10:8889/weight', - // domain1: 'http://device.shuziweidao.com:80/gateway', - // domain2: 'http://device.shuziweidao.com:80/weight', + // domain1: 'http://10.10.10.10:8889', + // domain2: 'http://10.10.10.10:8889/weight', + domain1: 'http://device.shuziweidao.com:80/gateway', + domain2: 'http://device.shuziweidao.com:80/weight', } /** 支持的设备型号配置列表(5款) */ @@ -153,7 +153,6 @@ class LeFuService { this._reconnectCount = 0 plugin.ScaleAction.startDataProgress(true) this._activeProtocol = plugin.ScaleAction.getActiveProtocol() - this._connectState = plugin.BLUE_STATE.CONNECTSUCCESS this._activeProtocol.codeUpdateMTU((mtuRes: any) => { console.log(TAG, 'codeUpdateMTU:', mtuRes) this._activeProtocol.codeSyncTime((syncRes: any) => { @@ -172,7 +171,9 @@ class LeFuService { } if (res === plugin.BLUE_STATE.CONNECTFAILED) { this._stopKeepAlive() - if (!this._intentionalConnect) { + if (this._intentionalConnect) { + this._intentionalConnect = false + } else { this._doReconnect() } } @@ -203,6 +204,10 @@ class LeFuService { this._intentionalConnect = true this._stopKeepAlive() this._stopReconnectTimer() + // 先断开旧连接(此时状态仍在),再清空内部状态 + if (this.isConnected) { + plugin.Blue.disconnect() + } this._activeProtocol = null this._connectState = '' this._deviceInfo = null @@ -210,7 +215,6 @@ class LeFuService { plugin.Blue.setDeviceSetting(DEVICE_SETTINGS) this._subscribeBus() plugin.Blue.stopBluetoothDevicesDiscovery() - // 0.0.25 新增:重新连接前必须清除协议缓存 plugin.Blue.clearProtocol() plugin.Blue.createBLEConnection(rawDevice) } @@ -221,9 +225,9 @@ class LeFuService { plugin.Blue.disconnect() } - /** 读缓存设备信息,自动扫描匹配后连接,已连接或无缓存时直接返回 */ + /** 读缓存设备信息,自动扫描匹配后连接,无缓存时直接返回 */ autoConnect(): void { - if (this.isConnected) return + const cached = wx.getStorageSync('connectDeviceInfo') as RawDevice | null const targetDeviceId = cached?.deviceId as string | undefined if (!targetDeviceId) return diff --git a/miniprogram/pages/equipment/equipment.ts b/miniprogram/pages/equipment/equipment.ts index 349b6a6..1a612c6 100644 --- a/miniprogram/pages/equipment/equipment.ts +++ b/miniprogram/pages/equipment/equipment.ts @@ -107,15 +107,8 @@ Page({ }) }, - /** 检查缓存设备:已连接则直接标记,未连接则调 autoConnect 扫描重连 */ + /** 每次进入页面都走真实扫描连接,确保状态最新 */ _checkAndAutoConnect() { - // 已连接且 deviceInfo 就绪,直接标记列表 - if (lefuService.isConnected && lefuService.deviceInfo?.serialNumber) { - this._markConnected(lefuService.deviceInfo.serialNumber as string) - return - } - - // 注册回调:连接成功后更新列表状态 lefuService.onDeviceInfo((info) => { this._markConnected(info.serialNumber as string) })