增加蓝牙自动搜索
This commit is contained in:
@@ -53,6 +53,8 @@ Page({
|
||||
|
||||
disconnectAllDevices() {
|
||||
const list = this.data.deviceList.map(d => ({ ...d, connected: false }))
|
||||
// 按 signal 从高到低排序,避免空值影响
|
||||
list.sort((a, b) => (b.signal || 0) - (a.signal || 0))
|
||||
this.setData({ deviceList: list })
|
||||
},
|
||||
|
||||
@@ -89,16 +91,12 @@ Page({
|
||||
searchBluetooth() {
|
||||
const filterPrefix = this.data.activeTab
|
||||
|
||||
wx.showLoading({
|
||||
title: '搜索中...',
|
||||
mask: true
|
||||
})
|
||||
|
||||
// 先断开当前连接设备(如果有)
|
||||
this.disconnectCurrentDevice()
|
||||
// this.disconnectCurrentDevice()
|
||||
|
||||
// 清空旧列表并启动搜索
|
||||
this.setData({ deviceList: [] })
|
||||
// // 清空旧列表并启动搜索
|
||||
// this.setData({ deviceList: [] })
|
||||
|
||||
this.ensureBluetoothReady()
|
||||
.then(() => this.startBluetoothDevicesDiscovery(filterPrefix))
|
||||
@@ -173,7 +171,6 @@ Page({
|
||||
return
|
||||
}
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: '搜索失败', icon: 'none' })
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -188,7 +185,6 @@ Page({
|
||||
try { this.appendLog && this.appendLog('CFG', 'adapter already discovering, attach listener') } catch (e) {}
|
||||
this.setupDeviceFoundListener(prefix)
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: '正在搜索中', icon: 'none' })
|
||||
return
|
||||
}
|
||||
doStart()
|
||||
@@ -220,6 +216,26 @@ Page({
|
||||
this._deviceFoundHandler = null
|
||||
},
|
||||
|
||||
// 启动/停止定时扫描(每3秒一次)
|
||||
startScanTimer() {
|
||||
if (this._scanTimer) return
|
||||
try { this.appendLog && this.appendLog('CFG', 'startScanTimer') } catch (e) {}
|
||||
this._scanTimer = setInterval(() => {
|
||||
try {
|
||||
// 触发一次搜索,内部有防抖保护
|
||||
this.searchBluetooth()
|
||||
} catch (e) { /* ignore */ }
|
||||
}, 3000)
|
||||
},
|
||||
|
||||
stopScanTimer() {
|
||||
if (this._scanTimer) {
|
||||
clearInterval(this._scanTimer)
|
||||
this._scanTimer = null
|
||||
try { this.appendLog && this.appendLog('CFG', 'stopScanTimer') } catch (e) {}
|
||||
}
|
||||
},
|
||||
|
||||
handleDeviceFound(devices, prefix) {
|
||||
const list = [...this.data.deviceList]
|
||||
devices.forEach((dev) => {
|
||||
@@ -244,7 +260,12 @@ Page({
|
||||
}
|
||||
|
||||
if (existsIndex >= 0) {
|
||||
list[existsIndex] = { ...list[existsIndex], ...mapped }
|
||||
// 设备已存在时仅更新信号值与 RSSI,避免覆盖其它已保存字段
|
||||
list[existsIndex] = {
|
||||
...list[existsIndex],
|
||||
signal,
|
||||
RSSI: rssi
|
||||
}
|
||||
} else {
|
||||
list.push(mapped)
|
||||
}
|
||||
@@ -260,7 +281,7 @@ Page({
|
||||
console.info('[BLE] stop scan, found events:', this._foundCount || 0, 'list size:', this.data.deviceList.length)
|
||||
wx.hideLoading()
|
||||
const count = this.data.deviceList.length
|
||||
wx.showToast({ title: `发现${count}个设备`, icon: 'success', duration: 1500 })
|
||||
// 取消自动显示搜索完成提示,避免打扰
|
||||
}
|
||||
})
|
||||
},
|
||||
@@ -268,6 +289,8 @@ Page({
|
||||
onUnload() {
|
||||
// 页面卸载时清理蓝牙扫描与监听
|
||||
// this.teardownDeviceFoundListener()
|
||||
// 页面卸载时停止定时扫描
|
||||
try { this.stopScanTimer && this.stopScanTimer() } catch (e) {}
|
||||
if (typeof wx.stopBluetoothDevicesDiscovery === 'function') {
|
||||
wx.stopBluetoothDevicesDiscovery({ complete: () => {} })
|
||||
}
|
||||
@@ -351,7 +374,7 @@ Page({
|
||||
wx.createBLEConnection({
|
||||
deviceId: device.id,
|
||||
success: () => {
|
||||
const list = this.data.deviceList.map((d, i) => ({ ...d, connected: i === index }))
|
||||
const list = this.data.deviceList.map((d) => ({ ...d, connected: d.id === device.id }))
|
||||
this.setData({ deviceList: list, currentDeviceId: device.id })
|
||||
// 设置MTU为256,提升传输效率(若支持)
|
||||
if (typeof wx.setBLEMTU === 'function') {
|
||||
@@ -372,7 +395,7 @@ Page({
|
||||
const isAlreadyConnected = errmsg.indexOf('already connect') >= 0 || errmsg.indexOf('already connected') >= 0 || (err && (err.errCode === -1 || err.errno === 1509007))
|
||||
if (isAlreadyConnected) {
|
||||
try { this.appendLog && this.appendLog('CFG', 'createBLEConnection: already connected, treating as connected') } catch (e) {}
|
||||
const list = this.data.deviceList.map((d, i) => ({ ...d, connected: i === index }))
|
||||
const list = this.data.deviceList.map((d) => ({ ...d, connected: d.id === device.id }))
|
||||
this.setData({ deviceList: list, currentDeviceId: device.id })
|
||||
// 继续发现服务与特征以恢复页面状态
|
||||
try { this.discoverBleChannels(device) } catch (e) {}
|
||||
@@ -599,8 +622,8 @@ Page({
|
||||
onShow() {
|
||||
// 返回到此页面时,清理导航标记
|
||||
if (this._navigatingToB13) {
|
||||
// 已从 B13 返回:清理导航标记但仍继续执行恢复流程,确保已连接设备状态可被恢复并展示
|
||||
this._navigatingToB13 = false
|
||||
return
|
||||
}
|
||||
|
||||
try { this.appendLog && this.appendLog('CFG', 'onShow: resume, ensuring adapter and forcing discovery') } catch (e) {}
|
||||
@@ -665,17 +688,28 @@ Page({
|
||||
.catch((err) => {
|
||||
try { this.appendLog && this.appendLog('WARN', 'onShow ensureBluetoothReady failed') } catch (e) {}
|
||||
})
|
||||
// 进入页面时启动定时扫描(每3秒一次)
|
||||
try { this.startScanTimer && this.startScanTimer() } catch (e) {}
|
||||
},
|
||||
|
||||
onHide() {
|
||||
// 如果正在导航到 B13 页面,跳过 onHide 的断开/重置流程,保留连接
|
||||
// 如果正在导航到 B13 页面,保留连接会话,但应停止扫描/发现以节省资源
|
||||
if (this._navigatingToB13) {
|
||||
try { this.appendLog && this.appendLog('CFG', 'onHide skipped due to navigation to B13') } catch (e) {}
|
||||
// 清理标记,后续返回时 onShow 会再次执行
|
||||
this._navigatingToB13 = false
|
||||
try { this.appendLog && this.appendLog('CFG', 'onHide during navigation to B13: stop scan but keep connection') } catch (e) {}
|
||||
// 停止定时扫描
|
||||
try { this.stopScanTimer && this.stopScanTimer() } catch (e) {}
|
||||
// 停止发现(但不断开已连接设备),随后直接返回以保留连接状态
|
||||
try {
|
||||
if (typeof wx.stopBluetoothDevicesDiscovery === 'function') {
|
||||
wx.stopBluetoothDevicesDiscovery({ complete: () => { try { this.appendLog && this.appendLog('CFG', 'stopBluetoothDevicesDiscovery onHide (navigating to B13)') } catch (e) {} } })
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
return
|
||||
}
|
||||
|
||||
// 非导航到 B13 的离开:停止定时扫描并断开连接、关闭适配器以重置状态
|
||||
try { this.stopScanTimer && this.stopScanTimer() } catch (e) {}
|
||||
|
||||
try {
|
||||
// 停止发现,避免后台扫描
|
||||
if (typeof wx.stopBluetoothDevicesDiscovery === 'function') {
|
||||
|
||||
Reference in New Issue
Block a user