[AI Generated]: feat(equipment): 连接与自动重连改为扫描匹配 deviceId 后再建立连接

This commit is contained in:
17792275749
2026-05-15 16:54:09 +08:00
parent 33b6939cc6
commit 24d3677415
+46 -16
View File
@@ -93,10 +93,11 @@ Page({
}) })
}, },
/** 检查缓存设备:已连接则直接标记,未连接则扫描自动连接 */ /** 检查缓存设备:已连接则直接标记,未连接则扫描匹配 deviceId 后自动连接 */
_checkAndAutoConnect() { _checkAndAutoConnect() {
const cachedRaw = wx.getStorageSync('connectDeviceInfo') as RawDevice | null const cachedRaw = wx.getStorageSync('connectDeviceInfo') as RawDevice | null
if (!cachedRaw) return const targetDeviceId = cachedRaw?.deviceId as string | undefined
if (!targetDeviceId) return
// 已连接且 deviceInfo 就绪,直接标记列表 // 已连接且 deviceInfo 就绪,直接标记列表
if (lefuService.isConnected && lefuService.deviceInfo?.serialNumber) { if (lefuService.isConnected && lefuService.deviceInfo?.serialNumber) {
@@ -104,19 +105,34 @@ Page({
return return
} }
// 未连接,直接用缓存对象连,不走扫描 let scanTimer: number | null = null
wx.showLoading({ title: '连接中...', mask: true }) wx.showLoading({ title: '连接中...', mask: true })
wx.openBluetoothAdapter({ wx.openBluetoothAdapter({
success: () => { success: () => {
lefuService.connect(cachedRaw) scanTimer = setTimeout(() => {
lefuService.stopScan()
wx.hideLoading()
}, 15000)
lefuService.onDevicesList((devices) => {
const matched = devices.find(d => d.raw.deviceId === targetDeviceId)
if (!matched) return
if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null }
lefuService.stopScan()
lefuService.connect(matched.raw)
lefuService.onDeviceConnect(() => { lefuService.onDeviceConnect(() => {
wx.hideLoading() wx.hideLoading()
wx.setStorageSync('connectDeviceInfo', matched.raw)
}) })
lefuService.onDeviceInfo((info) => { lefuService.onDeviceInfo((info) => {
this._markConnected(info.serialNumber as string) this._markConnected(info.serialNumber as string)
}) })
})
lefuService.startScan()
}, },
fail: () => { fail: () => {
if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null }
wx.hideLoading() wx.hideLoading()
}, },
}) })
@@ -131,32 +147,46 @@ Page({
this.setData({ deviceList: list }) this.setData({ deviceList: list })
}, },
/** 点击「连接」:断开当前、直接用 connectDeviceInfo 连接指定设备 */ /** 点击「连接」:扫描周边设备,找到 deviceId 匹配的设备后连接 */
onTapConnect(e: WechatMiniprogram.TouchEvent) { onTapConnect(e: WechatMiniprogram.TouchEvent) {
const connectDeviceInfoStr = e.currentTarget.dataset.connectDeviceInfo as string const connectDeviceInfoStr = e.currentTarget.dataset.connectDeviceInfo as string
let rawDevice: RawDevice | null = null let cachedDevice: RawDevice | null = null
try { try {
rawDevice = JSON.parse(connectDeviceInfoStr || '{}') cachedDevice = JSON.parse(connectDeviceInfoStr || '{}')
} catch {} } catch {}
if (!rawDevice) return const targetDeviceId = cachedDevice?.deviceId as string | undefined
if (!targetDeviceId) return
if (lefuService.isConnected) { if (lefuService.isConnected) lefuService.disconnect()
lefuService.disconnect() this.setData({ deviceList: this.data.deviceList.map(item => ({ ...item, connected: false })) })
}
const list = this.data.deviceList.map(item => ({ ...item, connected: false }))
this.setData({ deviceList: list })
wx.showLoading({ title: '连接中...', mask: true }) let scanTimer: number | null = null
wx.showLoading({ title: '扫描中...', mask: true })
wx.openBluetoothAdapter({ wx.openBluetoothAdapter({
success: () => { success: () => {
lefuService.connect(rawDevice!) scanTimer = setTimeout(() => {
lefuService.stopScan()
wx.hideLoading()
wx.showToast({ title: '未找到设备', icon: 'none' })
}, 15000)
lefuService.onDevicesList((devices) => {
const matched = devices.find(d => d.raw.deviceId === targetDeviceId)
if (!matched) return
if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null }
lefuService.stopScan()
wx.showLoading({ title: '连接中...', mask: true })
lefuService.connect(matched.raw)
lefuService.onDeviceConnect(() => { lefuService.onDeviceConnect(() => {
wx.hideLoading() wx.hideLoading()
wx.setStorageSync('connectDeviceInfo', rawDevice) wx.setStorageSync('connectDeviceInfo', matched.raw)
}) })
lefuService.onDeviceInfo((info) => { lefuService.onDeviceInfo((info) => {
this._markConnected(info.serialNumber as string) this._markConnected(info.serialNumber as string)
}) })
})
lefuService.startScan()
}, },
fail: () => { fail: () => {
wx.hideLoading() wx.hideLoading()