fix(equipment): 进入页面不再信任缓存的蓝牙连接状态,每次走真实扫描
- _checkAndAutoConnect 去掉 isConnected+deviceInfo 缓存短路,始终走 autoConnect - autoConnect 去掉 isConnected 短路,每次真实扫描 - connect() 先断开旧连接再建新连接,避免新旧连接冲突 - connectState CONNECTFAILED 时重置 _intentionalConnect,防止标记卡死 - deviceConnect handler 移除冗余的 _connectState 赋值 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
5705f9e622
commit
15e089f5e3
@@ -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
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user