fix(lefu): 完善蓝牙连接状态处理,修复断开后 UI 不同步问题
- connectState 回调增加 UNAVAILABLE 清理和 READY 自动重连 - disconnect() 增加 _intentionalConnect 标记避免误重连 - deviceWillDisconnect 增加 _activeProtocol 防重入判断 - connectedDevice/equipment/connectedWifi 增加断开状态响应 - connectedWifi 设备断开后自动退出配网页 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
15e089f5e3
commit
439f560218
@@ -168,6 +168,7 @@ class LeFuService {
|
||||
this._onConnectStateCb?.(res)
|
||||
if (res === plugin.BLUE_STATE.CONNECTSUCCESS) {
|
||||
this._startKeepAlive()
|
||||
return
|
||||
}
|
||||
if (res === plugin.BLUE_STATE.CONNECTFAILED) {
|
||||
this._stopKeepAlive()
|
||||
@@ -176,6 +177,28 @@ class LeFuService {
|
||||
} else {
|
||||
this._doReconnect()
|
||||
}
|
||||
return
|
||||
}
|
||||
// 蓝牙不可用:做完整清理
|
||||
if (res === plugin.BLUE_STATE.UNAVAILABLE) {
|
||||
this._stopKeepAlive()
|
||||
this._stopReconnectTimer()
|
||||
if (this._activeProtocol) {
|
||||
this._activeProtocol = null
|
||||
this._onDisconnectedCb?.()
|
||||
}
|
||||
return
|
||||
}
|
||||
// 蓝牙就绪:如有缓存设备且非主动断开,尝试连接
|
||||
if (res === plugin.BLUE_STATE.READY) {
|
||||
if (this._intentionalConnect) {
|
||||
this._intentionalConnect = false
|
||||
} else if (this._lastRawDevice && !this._activeProtocol) {
|
||||
this._stopReconnectTimer()
|
||||
wx.openBluetoothAdapter({
|
||||
success: () => this.connect(this._lastRawDevice!),
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -189,9 +212,11 @@ class LeFuService {
|
||||
|
||||
plugin.bus.subscribe('deviceWillDisconnect', () => {
|
||||
console.warn(TAG, 'bus[deviceWillDisconnect],intentionalConnect:', this._intentionalConnect)
|
||||
this._activeProtocol = null
|
||||
this._stopKeepAlive()
|
||||
this._onDisconnectedCb?.()
|
||||
if (this._activeProtocol) {
|
||||
this._activeProtocol = null
|
||||
this._onDisconnectedCb?.()
|
||||
}
|
||||
if (!this._intentionalConnect) {
|
||||
this._doReconnect()
|
||||
}
|
||||
@@ -221,6 +246,7 @@ class LeFuService {
|
||||
|
||||
disconnect(): void {
|
||||
console.log(TAG, 'disconnect()')
|
||||
this._intentionalConnect = true
|
||||
this._stopKeepAlive()
|
||||
plugin.Blue.disconnect()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user