[AI Generated]: feat(equipment): 连接与自动重连改为扫描匹配 deviceId 后再建立连接
This commit is contained in:
@@ -93,10 +93,11 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
/** 检查缓存设备:已连接则直接标记,未连接则扫描自动连接 */
|
||||
/** 检查缓存设备:已连接则直接标记,未连接则扫描匹配 deviceId 后自动连接 */
|
||||
_checkAndAutoConnect() {
|
||||
const cachedRaw = wx.getStorageSync('connectDeviceInfo') as RawDevice | null
|
||||
if (!cachedRaw) return
|
||||
const targetDeviceId = cachedRaw?.deviceId as string | undefined
|
||||
if (!targetDeviceId) return
|
||||
|
||||
// 已连接且 deviceInfo 就绪,直接标记列表
|
||||
if (lefuService.isConnected && lefuService.deviceInfo?.serialNumber) {
|
||||
@@ -104,19 +105,34 @@ Page({
|
||||
return
|
||||
}
|
||||
|
||||
// 未连接,直接用缓存对象连,不走扫描
|
||||
let scanTimer: number | null = null
|
||||
|
||||
wx.showLoading({ title: '连接中...', mask: true })
|
||||
wx.openBluetoothAdapter({
|
||||
success: () => {
|
||||
lefuService.connect(cachedRaw)
|
||||
lefuService.onDeviceConnect(() => {
|
||||
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(() => {
|
||||
wx.hideLoading()
|
||||
wx.setStorageSync('connectDeviceInfo', matched.raw)
|
||||
})
|
||||
lefuService.onDeviceInfo((info) => {
|
||||
this._markConnected(info.serialNumber as string)
|
||||
})
|
||||
})
|
||||
lefuService.onDeviceInfo((info) => {
|
||||
this._markConnected(info.serialNumber as string)
|
||||
})
|
||||
lefuService.startScan()
|
||||
},
|
||||
fail: () => {
|
||||
if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null }
|
||||
wx.hideLoading()
|
||||
},
|
||||
})
|
||||
@@ -131,32 +147,46 @@ Page({
|
||||
this.setData({ deviceList: list })
|
||||
},
|
||||
|
||||
/** 点击「连接」:断开当前、直接用 connectDeviceInfo 连接指定设备 */
|
||||
/** 点击「连接」:扫描周边设备,找到 deviceId 匹配的设备后连接 */
|
||||
onTapConnect(e: WechatMiniprogram.TouchEvent) {
|
||||
const connectDeviceInfoStr = e.currentTarget.dataset.connectDeviceInfo as string
|
||||
let rawDevice: RawDevice | null = null
|
||||
let cachedDevice: RawDevice | null = null
|
||||
try {
|
||||
rawDevice = JSON.parse(connectDeviceInfoStr || '{}')
|
||||
cachedDevice = JSON.parse(connectDeviceInfoStr || '{}')
|
||||
} catch {}
|
||||
if (!rawDevice) return
|
||||
const targetDeviceId = cachedDevice?.deviceId as string | undefined
|
||||
if (!targetDeviceId) return
|
||||
|
||||
if (lefuService.isConnected) {
|
||||
lefuService.disconnect()
|
||||
}
|
||||
const list = this.data.deviceList.map(item => ({ ...item, connected: false }))
|
||||
this.setData({ deviceList: list })
|
||||
if (lefuService.isConnected) lefuService.disconnect()
|
||||
this.setData({ deviceList: this.data.deviceList.map(item => ({ ...item, connected: false })) })
|
||||
|
||||
wx.showLoading({ title: '连接中...', mask: true })
|
||||
let scanTimer: number | null = null
|
||||
|
||||
wx.showLoading({ title: '扫描中...', mask: true })
|
||||
wx.openBluetoothAdapter({
|
||||
success: () => {
|
||||
lefuService.connect(rawDevice!)
|
||||
lefuService.onDeviceConnect(() => {
|
||||
scanTimer = setTimeout(() => {
|
||||
lefuService.stopScan()
|
||||
wx.hideLoading()
|
||||
wx.setStorageSync('connectDeviceInfo', rawDevice)
|
||||
})
|
||||
lefuService.onDeviceInfo((info) => {
|
||||
this._markConnected(info.serialNumber as string)
|
||||
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(() => {
|
||||
wx.hideLoading()
|
||||
wx.setStorageSync('connectDeviceInfo', matched.raw)
|
||||
})
|
||||
lefuService.onDeviceInfo((info) => {
|
||||
this._markConnected(info.serialNumber as string)
|
||||
})
|
||||
})
|
||||
lefuService.startScan()
|
||||
},
|
||||
fail: () => {
|
||||
wx.hideLoading()
|
||||
|
||||
Reference in New Issue
Block a user