fix(equipment): 连接设备增加超时与失败兜底,避免加载一直转圈

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-09-01 18:10:39 +08:00
co-authored by Claude Sonnet 4.6
parent 4be9712000
commit 680794702e
+38 -2
View File
@@ -54,7 +54,9 @@ let unsubDevicesList: (() => void) | null = null
let unsubConnect: (() => void) | null = null
let unsubDisconnected: (() => void) | null = null
let unsubDeviceConnect: (() => void) | null = null
let unsubConnectState: (() => void) | null = null
let scanTimer: number | null = null
let connectTimer: number | null = null
Page({
data: {
@@ -90,7 +92,10 @@ Page({
unsubDevicesList = null
unsubConnect?.()
unsubConnect = null
unsubConnectState?.()
unsubConnectState = null
if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null }
if (connectTimer !== null) { clearTimeout(connectTimer); connectTimer = null }
},
/** 订阅断开事件:设备长时间未操作或意外断开时,同步页面状态为未连接 */
@@ -199,7 +204,10 @@ Page({
unsubDevicesList = null
unsubConnect?.()
unsubConnect = null
unsubConnectState?.()
unsubConnectState = null
if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null }
if (connectTimer !== null) { clearTimeout(connectTimer); connectTimer = null }
wx.showLoading({ title: '连接中...', mask: true })
wx.openBluetoothAdapter({
@@ -218,12 +226,40 @@ Page({
unsubDevicesList?.()
unsubDevicesList = null
leFuService.stopScan()
leFuService.connect(matched.raw)
unsubConnect = leFuService.onDeviceConnect(() => {
// 连接超时兜底:15 秒未连上则结束 loading
connectTimer = setTimeout(() => {
connectTimer = null
unsubConnect?.()
unsubConnect = null
unsubConnectState?.()
unsubConnectState = null
wx.hideLoading()
wx.showToast({ title: '连接失败,请重试', icon: 'none' })
}, 15000)
// 连接失败:立即结束 loading
unsubConnectState = leFuService.onConnectState((state) => {
if (state !== leFuService.BLUE_STATE.CONNECTFAILED) return
if (connectTimer !== null) { clearTimeout(connectTimer); connectTimer = null }
unsubConnectState?.()
unsubConnectState = null
unsubConnect?.()
unsubConnect = null
wx.hideLoading()
wx.showToast({ title: '连接失败,请重试', icon: 'none' })
})
leFuService.connect(matched.raw)
unsubConnect = leFuService.onDeviceConnect(() => {
if (connectTimer !== null) { clearTimeout(connectTimer); connectTimer = null }
unsubConnect?.()
unsubConnect = null
unsubConnectState?.()
unsubConnectState = null
wx.hideLoading()
wx.setStorageSync('connectDeviceInfo', matched.raw)
this._updateStatus(targetDeviceId, true)
})
})
leFuService.startScan()