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:
17792275749
2026-07-21 09:43:49 +08:00
co-authored by Claude Sonnet 4.6
parent 15e089f5e3
commit 439f560218
7 changed files with 60 additions and 12 deletions
+2 -2
View File
@@ -326,7 +326,7 @@ Page({
get<any>('weighingScale/v3/getUserInfoByIdcard', {
sn: lefuService.deviceInfo?.serialNumber ?? '',
idCard,
idCard: idCard || null,
realname: name
}, { loading: true })
.then(res => {
@@ -491,7 +491,7 @@ Page({
sn: lefuService.deviceInfo?.serialNumber ?? '',
scaleDeviceId: lefuService.deviceInfo?.serialNumber ?? '',
realname: name.trim(),
idCard,
idCard: idCard || null,
sex: finalSex,
birthday,
height: heightNum,
@@ -28,6 +28,11 @@ Page({
onShow() {
console.log(TAG, 'onShow')
// 回到页面时同步实际连接状态,避免回调被其他页面覆盖导致状态不同步
if (!lefuService.isConnected) {
const list = this.data.deviceList.map(item => ({ ...item, connected: false }))
this.setData({ deviceList: list })
}
},
onHide() {
@@ -169,6 +174,10 @@ Page({
this.setData({ _autoConnectTried: true })
this._autoConnectByIndex(matchedIndex)
})
lefuService.onDisconnected(() => {
const list = this.data.deviceList.map(item => ({ ...item, connected: false }))
this.setData({ deviceList: list })
})
lefuService.startScan()
console.log(TAG, 'lefuService.startScan() 已调用')
},
@@ -25,7 +25,7 @@ Page({
_toBleStatus(state: string): BleStatus {
const bs = lefuService.BLUE_STATE
if (state === bs.CONNECTSUCCESS || state === bs.WIFISUCCESS) return 'connected'
if (state === bs.CONNECTFAILED) return 'failed'
if (state === bs.CONNECTFAILED || state === bs.UNAVAILABLE) return 'failed'
return 'connecting'
},
@@ -34,6 +34,11 @@ Page({
lefuService.onDeviceInfo((info) => {
this.setData({ deviceName: info.modelNumber })
})
// 设备断开即退出配网页
lefuService.onDisconnected(() => {
wx.showToast({ title: '设备已断开连接', icon: 'none', duration: 1500 })
setTimeout(() => wx.navigateBack(), 1500)
})
this.setData({
bleStatus: this._toBleStatus(lefuService.connectState),
deviceName: lefuService.deviceInfo?.modelNumber ?? '智能体重秤',
@@ -43,15 +48,13 @@ Page({
onShow() {
lefuService.onConnectState((state) => {
const bs = lefuService.BLUE_STATE
if (state === bs.CONNECTSUCCESS || state === bs.WIFISUCCESS || state === bs.CONNECTFAILED) {
this.setData({ bleStatus: this._toBleStatus(state) })
}
this.setData({ bleStatus: this._toBleStatus(state) })
})
},
onUnload() {
wx.hideLoading()
lefuService.onDisconnected(() => {})
if (this.data.status === 'configuring') {
lefuService.disconnect()
}
+10
View File
@@ -68,6 +68,7 @@ Page({
lefuService.stopScan()
lefuService.onDeviceConnect(() => {})
lefuService.onDeviceInfo(() => {})
lefuService.onDisconnected(() => {})
},
/** 拉取已绑定设备列表,加载完成后检查缓存自动连接 */
@@ -112,6 +113,9 @@ Page({
lefuService.onDeviceInfo((info) => {
this._markConnected(info.serialNumber as string)
})
lefuService.onDisconnected(() => {
this._markAllDisconnected()
})
lefuService.autoConnect()
},
@@ -124,6 +128,12 @@ Page({
this.setData({ deviceList: list })
},
/** 将所有设备标记为未连接 */
_markAllDisconnected() {
const list = this.data.deviceList.map(item => ({ ...item, connected: false }))
this.setData({ deviceList: list })
},
/** 点击「连接」:扫描周边设备,找到 deviceId 匹配的设备后连接 */
onTapConnect(e: WechatMiniprogram.TouchEvent) {
const userInfo = e.currentTarget.dataset.userInfo as DeviceItem