增加蓝牙自动搜索
This commit is contained in:
@@ -19,6 +19,10 @@ Page({
|
|||||||
bleMac: '00:00:00:00:00:00',
|
bleMac: '00:00:00:00:00:00',
|
||||||
bleAMC: '-',
|
bleAMC: '-',
|
||||||
bleVersion: '-',
|
bleVersion: '-',
|
||||||
|
// 设备信号/RSSI/连接状态显示
|
||||||
|
bleSignal: '-',
|
||||||
|
bleRSSI: '-',
|
||||||
|
isConnected: false,
|
||||||
openDelay: 20,
|
openDelay: 20,
|
||||||
bathDelay: 20,
|
bathDelay: 20,
|
||||||
logs: [],
|
logs: [],
|
||||||
@@ -141,6 +145,8 @@ Page({
|
|||||||
const txCharId = options && options.txCharId ? decodeURIComponent(options.txCharId) : ''
|
const txCharId = options && options.txCharId ? decodeURIComponent(options.txCharId) : ''
|
||||||
const rxCharId = options && options.rxCharId ? decodeURIComponent(options.rxCharId) : ''
|
const rxCharId = options && options.rxCharId ? decodeURIComponent(options.rxCharId) : ''
|
||||||
this.setData({ DevName: devName, bleName: devName, bleMac, deviceId, serviceId, txCharId, rxCharId })
|
this.setData({ DevName: devName, bleName: devName, bleMac, deviceId, serviceId, txCharId, rxCharId })
|
||||||
|
// 尝试更新设备连接/信号信息
|
||||||
|
try { this.updateDeviceInfo && this.updateDeviceInfo() } catch (e) {}
|
||||||
// 初始化 picker 选项(触发/释放分别根据各自单位生成)
|
// 初始化 picker 选项(触发/释放分别根据各自单位生成)
|
||||||
this.updateDoorTriggerOptions(this.data.doorTriggerUnitIndex || 0)
|
this.updateDoorTriggerOptions(this.data.doorTriggerUnitIndex || 0)
|
||||||
this.updateDoorReleaseOptions(this.data.doorReleaseUnitIndex || 0)
|
this.updateDoorReleaseOptions(this.data.doorReleaseUnitIndex || 0)
|
||||||
@@ -159,6 +165,58 @@ Page({
|
|||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 更新当前设备的 RSSI / signal / 连接状态(基于 deviceId)
|
||||||
|
updateDeviceInfo() {
|
||||||
|
const deviceId = this.data.deviceId
|
||||||
|
if (!deviceId) {
|
||||||
|
this.setData({ bleSignal: '-', bleRSSI: '-', isConnected: false })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const setUnknown = () => this.setData({ bleSignal: '-', bleRSSI: '-', isConnected: false })
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 优先使用 getConnectedBluetoothDevices 查询当前已连接设备
|
||||||
|
const svc = this.data.serviceId
|
||||||
|
if (typeof wx.getConnectedBluetoothDevices === 'function' && svc) {
|
||||||
|
wx.getConnectedBluetoothDevices({ services: [svc], success: (res) => {
|
||||||
|
const devices = (res && res.devices) || []
|
||||||
|
const found = devices.find(d => d.deviceId === deviceId)
|
||||||
|
if (found) {
|
||||||
|
const rssi = (typeof found.RSSI === 'number') ? found.RSSI : (found.RSSI ? Number(found.RSSI) : 0)
|
||||||
|
const sigText = isNaN(rssi) ? '-' : `${rssi} dBm`
|
||||||
|
this.setData({ bleRSSI: isNaN(rssi) ? '-' : rssi, bleSignal: sigText, isConnected: true })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 未连接
|
||||||
|
setUnknown()
|
||||||
|
}, fail: () => setUnknown() })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 兜底使用 getBluetoothDevices 查询缓存设备
|
||||||
|
if (typeof wx.getBluetoothDevices === 'function') {
|
||||||
|
wx.getBluetoothDevices({ success: (res) => {
|
||||||
|
const devices = (res && res.devices) || []
|
||||||
|
const found = devices.find(d => d.deviceId === deviceId)
|
||||||
|
if (found) {
|
||||||
|
const rssi = (typeof found.RSSI === 'number') ? found.RSSI : (found.RSSI ? Number(found.RSSI) : 0)
|
||||||
|
const sigText = isNaN(rssi) ? '-' : `${rssi} dBm`
|
||||||
|
// connected 字段在缓存中可能为 true/false
|
||||||
|
this.setData({ bleRSSI: isNaN(rssi) ? '-' : rssi, bleSignal: sigText, isConnected: !!found.connected })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setUnknown()
|
||||||
|
}, fail: () => setUnknown() })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setUnknown()
|
||||||
|
} catch (e) {
|
||||||
|
setUnknown()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
unitIndexToProtocolValue(idx) {
|
unitIndexToProtocolValue(idx) {
|
||||||
const i = Number(idx || 0)
|
const i = Number(idx || 0)
|
||||||
return i === 0 ? 1 : (i === 1 ? 2 : 3)
|
return i === 0 ? 1 : (i === 1 ? 2 : 3)
|
||||||
@@ -263,10 +321,19 @@ Page({
|
|||||||
})
|
})
|
||||||
}, 250)
|
}, 250)
|
||||||
} catch (e) { /* ignore */ }
|
} catch (e) { /* ignore */ }
|
||||||
|
try { this.updateDeviceInfo && this.updateDeviceInfo() } catch (e) {}
|
||||||
},
|
},
|
||||||
|
|
||||||
onUnload() {
|
onUnload() {
|
||||||
this.teardownBleListener()
|
this.teardownBleListener()
|
||||||
|
// 移除连接状态监听并清理重连计时器
|
||||||
|
try {
|
||||||
|
if (this._onBleConnChange && typeof wx.offBLEConnectionStateChange === 'function') {
|
||||||
|
wx.offBLEConnectionStateChange(this._onBleConnChange)
|
||||||
|
}
|
||||||
|
} catch (e) { /* ignore */ }
|
||||||
|
this._onBleConnChange = null
|
||||||
|
try { this.stopReconnectTimer && this.stopReconnectTimer() } catch (e) {}
|
||||||
},
|
},
|
||||||
|
|
||||||
// 顶部标签切换
|
// 顶部标签切换
|
||||||
@@ -274,9 +341,9 @@ Page({
|
|||||||
const id = Number(e.currentTarget.dataset.id || 1)
|
const id = Number(e.currentTarget.dataset.id || 1)
|
||||||
this.setData({ TabCur: id })
|
this.setData({ TabCur: id })
|
||||||
// 切换到蓝牙升级标签时,主动读取蓝牙版本号并保存到 bleVersion
|
// 切换到蓝牙升级标签时,主动读取蓝牙版本号并保存到 bleVersion
|
||||||
if (id === 2) {
|
// if (id === 1) {
|
||||||
this.onSendReadVersion()
|
|
||||||
}
|
// }
|
||||||
// 切换到设备升级标签(data-id === 3)时,默认发送一包 OTA 升级命令
|
// 切换到设备升级标签(data-id === 3)时,默认发送一包 OTA 升级命令
|
||||||
if (id === 3) {
|
if (id === 3) {
|
||||||
// 给用户一点缓冲时间再发送(确保 UI 切换完成)
|
// 给用户一点缓冲时间再发送(确保 UI 切换完成)
|
||||||
@@ -998,6 +1065,7 @@ Page({
|
|||||||
// 开启订阅(若已传入rx特征),保证能接收数据
|
// 开启订阅(若已传入rx特征),保证能接收数据
|
||||||
this.enableNotify()
|
this.enableNotify()
|
||||||
this.setupBleListener()
|
this.setupBleListener()
|
||||||
|
this.onSendReadVersion()
|
||||||
this.sendRadarStatusCommand(true)
|
this.sendRadarStatusCommand(true)
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -1234,6 +1302,55 @@ Page({
|
|||||||
}
|
}
|
||||||
// 注册系统 BLE 通知回调
|
// 注册系统 BLE 通知回调
|
||||||
wx.onBLECharacteristicValueChange(this._onBleChange)
|
wx.onBLECharacteristicValueChange(this._onBleChange)
|
||||||
|
// 注册 BLE 连接状态监听(兼容基础库)
|
||||||
|
if (typeof wx.onBLEConnectionStateChange === 'function') {
|
||||||
|
this._onBleConnChange = (res) => {
|
||||||
|
try {
|
||||||
|
const devId = res && res.deviceId
|
||||||
|
if (!devId || devId !== this.data.deviceId) return
|
||||||
|
const connected = !!res.connected
|
||||||
|
if (connected) {
|
||||||
|
// 连接成功,停止重连计时并启用发送按钮
|
||||||
|
this.setData({ isConnected: true })
|
||||||
|
try { this.stopReconnectTimer && this.stopReconnectTimer() } catch (e) {}
|
||||||
|
try { this.ensureBleChannels(() => {}) } catch (e) {}
|
||||||
|
} else {
|
||||||
|
// 断开,禁用发送并启动重连计时
|
||||||
|
this.setData({ isConnected: false })
|
||||||
|
try { this.startReconnectTimer && this.startReconnectTimer() } catch (e) {}
|
||||||
|
}
|
||||||
|
} catch (e) { /* ignore */ }
|
||||||
|
}
|
||||||
|
try { wx.onBLEConnectionStateChange(this._onBleConnChange) } catch (e) { /* ignore */ }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 启动/停止重连计时器(每3秒尝试重连),不在 onHide 时自动销毁
|
||||||
|
startReconnectTimer() {
|
||||||
|
if (this._reconnectTimer) return
|
||||||
|
const deviceId = this.data.deviceId
|
||||||
|
if (!deviceId) return
|
||||||
|
this._reconnectTimer = setInterval(() => {
|
||||||
|
try {
|
||||||
|
if (typeof wx.createBLEConnection === 'function') {
|
||||||
|
wx.createBLEConnection({ deviceId, success: () => {
|
||||||
|
// createBLEConnection 成功即认为已连接
|
||||||
|
this.setData({ isConnected: true })
|
||||||
|
try { this.stopReconnectTimer() } catch (e) {}
|
||||||
|
try { this.ensureBleChannels(() => {}) } catch (e) {}
|
||||||
|
}, fail: () => {
|
||||||
|
// ignore failure, will retry
|
||||||
|
} })
|
||||||
|
}
|
||||||
|
} catch (e) { /* ignore */ }
|
||||||
|
}, 3000)
|
||||||
|
},
|
||||||
|
|
||||||
|
stopReconnectTimer() {
|
||||||
|
if (this._reconnectTimer) {
|
||||||
|
clearInterval(this._reconnectTimer)
|
||||||
|
this._reconnectTimer = null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onDownloadOtaTool() {
|
onDownloadOtaTool() {
|
||||||
const url = 'https://www.baidu.com/';
|
const url = 'https://www.baidu.com/';
|
||||||
|
|||||||
@@ -17,6 +17,31 @@
|
|||||||
</view>
|
</view>
|
||||||
</scroll-view>
|
</scroll-view>
|
||||||
|
|
||||||
|
<!-- 公共设备信息栏:放在顶部标签下,所有 Tab 共用 -->
|
||||||
|
<view class="device-row common-device-row">
|
||||||
|
<view class="device-left">
|
||||||
|
<view class="device-line">
|
||||||
|
<text class="dr-label">蓝牙名称:</text>
|
||||||
|
<text class="dr-value">{{bleName || '-'}}</text>
|
||||||
|
</view>
|
||||||
|
<view class="device-line">
|
||||||
|
<text class="dr-label">MAC:</text>
|
||||||
|
<text class="dr-value">{{bleMac || '-'}}</text>
|
||||||
|
<text class="dr-label">版本:</text>
|
||||||
|
<text class="dr-value">{{bleVersion || '-'}}</text>
|
||||||
|
</view>
|
||||||
|
<view class="device-line">
|
||||||
|
<text class="dr-label">信号:</text>
|
||||||
|
<text class="dr-value">{{bleSignal}}</text>
|
||||||
|
<text class="dr-label">RSSI:</text>
|
||||||
|
<text class="dr-value">{{bleRSSI}}</text>
|
||||||
|
<text class="dr-label">状态:</text>
|
||||||
|
<text class="dr-value">{{isConnected ? '已连接' : '未连接'}}</text>
|
||||||
|
</view>
|
||||||
|
</view>
|
||||||
|
<view class="dr-btn dr-btn-view dr-btn-small" bindtap="onSendReadVersion">读取蓝牙信息</view>
|
||||||
|
</view>
|
||||||
|
|
||||||
<!-- Tab: 设备测试 -->
|
<!-- Tab: 设备测试 -->
|
||||||
<view wx:if="{{TabCur==1}}" class="content">
|
<view wx:if="{{TabCur==1}}" class="content">
|
||||||
|
|
||||||
@@ -31,8 +56,16 @@
|
|||||||
<view class="grid-and-actions">
|
<view class="grid-and-actions">
|
||||||
<view class="grid">
|
<view class="grid">
|
||||||
<view class="grid-item" wx:for="{{radarLights}}" wx:key="key">
|
<view class="grid-item" wx:for="{{radarLights}}" wx:key="key">
|
||||||
|
<!-- 文字先于指示灯显示 -->
|
||||||
|
<text class="label">
|
||||||
|
{{
|
||||||
|
item.key === 'door' ? (item.triggered ? '门磁(开门)' : '门磁(关门)') :
|
||||||
|
item.key === 'bath' ? (item.triggered ? '卫浴(触发)' : '卫浴(释放)') :
|
||||||
|
item.key === 'bed' ? (item.triggered ? '卧室(触发)' : '卧室(释放)') :
|
||||||
|
item.key === 'hall' ? (item.triggered ? '走廊(触发)' : '走廊(释放)') : item.label
|
||||||
|
}}
|
||||||
|
</text>
|
||||||
<view class="circle {{item.colorClass}}"></view>
|
<view class="circle {{item.colorClass}}"></view>
|
||||||
<text class="label">{{item.label}}</text>
|
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
@@ -157,7 +190,7 @@
|
|||||||
|
|
||||||
<view class="log-input-row">
|
<view class="log-input-row">
|
||||||
<textarea class="log-input" placeholder="输入要发送的数据" value="{{sendText}}" bindinput="onInputChange" disable-default-padding="true" />
|
<textarea class="log-input" placeholder="输入要发送的数据" value="{{sendText}}" bindinput="onInputChange" disable-default-padding="true" />
|
||||||
<button class="send-btn" size="mini" type="primary" bindtap="onSend">发送</button>
|
<button class="send-btn" size="mini" type="primary" bindtap="onSend" disabled="{{!isConnected}}">发送</button>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<!-- <scroll-view class="log-scroll" scroll-y="true">
|
<!-- <scroll-view class="log-scroll" scroll-y="true">
|
||||||
@@ -199,23 +232,7 @@
|
|||||||
|
|
||||||
<!-- Tab: 设备升级 -->
|
<!-- Tab: 设备升级 -->
|
||||||
<view wx:if="{{TabCur==2}}" class="content">
|
<view wx:if="{{TabCur==2}}" class="content">
|
||||||
<!-- 设备信息栏 -->
|
|
||||||
<view class="device-row">
|
|
||||||
<view class="device-left">
|
|
||||||
<view class="device-line">
|
|
||||||
<text class="dr-label">蓝牙名称:</text>
|
|
||||||
<text class="dr-value">{{bleName || '-'}}</text>
|
|
||||||
</view>
|
|
||||||
<view class="device-line">
|
|
||||||
<text class="dr-label">MAC:</text>
|
|
||||||
<text class="dr-value">{{bleMac || '-'}}</text>
|
|
||||||
<text class="dr-label">版本:</text>
|
|
||||||
<text class="dr-value">{{bleVersion || '-'}}</text>
|
|
||||||
</view>
|
|
||||||
</view>
|
|
||||||
<view class="dr-btn dr-btn-view dr-btn-small" bindtap="onSendReadVersion">读取蓝牙信息</view>
|
|
||||||
</view>
|
|
||||||
|
|
||||||
<!-- 功能栏(保留导出/导入) -->
|
<!-- 功能栏(保留导出/导入) -->
|
||||||
<view class="cfg-card">
|
<view class="cfg-card">
|
||||||
<view class="toolbar">
|
<view class="toolbar">
|
||||||
|
|||||||
@@ -53,6 +53,8 @@ Page({
|
|||||||
|
|
||||||
disconnectAllDevices() {
|
disconnectAllDevices() {
|
||||||
const list = this.data.deviceList.map(d => ({ ...d, connected: false }))
|
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 })
|
this.setData({ deviceList: list })
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -89,16 +91,12 @@ Page({
|
|||||||
searchBluetooth() {
|
searchBluetooth() {
|
||||||
const filterPrefix = this.data.activeTab
|
const filterPrefix = this.data.activeTab
|
||||||
|
|
||||||
wx.showLoading({
|
|
||||||
title: '搜索中...',
|
|
||||||
mask: true
|
|
||||||
})
|
|
||||||
|
|
||||||
// 先断开当前连接设备(如果有)
|
// 先断开当前连接设备(如果有)
|
||||||
this.disconnectCurrentDevice()
|
// this.disconnectCurrentDevice()
|
||||||
|
|
||||||
// 清空旧列表并启动搜索
|
// // 清空旧列表并启动搜索
|
||||||
this.setData({ deviceList: [] })
|
// this.setData({ deviceList: [] })
|
||||||
|
|
||||||
this.ensureBluetoothReady()
|
this.ensureBluetoothReady()
|
||||||
.then(() => this.startBluetoothDevicesDiscovery(filterPrefix))
|
.then(() => this.startBluetoothDevicesDiscovery(filterPrefix))
|
||||||
@@ -173,7 +171,6 @@ Page({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
wx.hideLoading()
|
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) {}
|
try { this.appendLog && this.appendLog('CFG', 'adapter already discovering, attach listener') } catch (e) {}
|
||||||
this.setupDeviceFoundListener(prefix)
|
this.setupDeviceFoundListener(prefix)
|
||||||
wx.hideLoading()
|
wx.hideLoading()
|
||||||
wx.showToast({ title: '正在搜索中', icon: 'none' })
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
doStart()
|
doStart()
|
||||||
@@ -220,6 +216,26 @@ Page({
|
|||||||
this._deviceFoundHandler = null
|
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) {
|
handleDeviceFound(devices, prefix) {
|
||||||
const list = [...this.data.deviceList]
|
const list = [...this.data.deviceList]
|
||||||
devices.forEach((dev) => {
|
devices.forEach((dev) => {
|
||||||
@@ -244,7 +260,12 @@ Page({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (existsIndex >= 0) {
|
if (existsIndex >= 0) {
|
||||||
list[existsIndex] = { ...list[existsIndex], ...mapped }
|
// 设备已存在时仅更新信号值与 RSSI,避免覆盖其它已保存字段
|
||||||
|
list[existsIndex] = {
|
||||||
|
...list[existsIndex],
|
||||||
|
signal,
|
||||||
|
RSSI: rssi
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
list.push(mapped)
|
list.push(mapped)
|
||||||
}
|
}
|
||||||
@@ -260,7 +281,7 @@ Page({
|
|||||||
console.info('[BLE] stop scan, found events:', this._foundCount || 0, 'list size:', this.data.deviceList.length)
|
console.info('[BLE] stop scan, found events:', this._foundCount || 0, 'list size:', this.data.deviceList.length)
|
||||||
wx.hideLoading()
|
wx.hideLoading()
|
||||||
const count = this.data.deviceList.length
|
const count = this.data.deviceList.length
|
||||||
wx.showToast({ title: `发现${count}个设备`, icon: 'success', duration: 1500 })
|
// 取消自动显示搜索完成提示,避免打扰
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@@ -268,6 +289,8 @@ Page({
|
|||||||
onUnload() {
|
onUnload() {
|
||||||
// 页面卸载时清理蓝牙扫描与监听
|
// 页面卸载时清理蓝牙扫描与监听
|
||||||
// this.teardownDeviceFoundListener()
|
// this.teardownDeviceFoundListener()
|
||||||
|
// 页面卸载时停止定时扫描
|
||||||
|
try { this.stopScanTimer && this.stopScanTimer() } catch (e) {}
|
||||||
if (typeof wx.stopBluetoothDevicesDiscovery === 'function') {
|
if (typeof wx.stopBluetoothDevicesDiscovery === 'function') {
|
||||||
wx.stopBluetoothDevicesDiscovery({ complete: () => {} })
|
wx.stopBluetoothDevicesDiscovery({ complete: () => {} })
|
||||||
}
|
}
|
||||||
@@ -351,7 +374,7 @@ Page({
|
|||||||
wx.createBLEConnection({
|
wx.createBLEConnection({
|
||||||
deviceId: device.id,
|
deviceId: device.id,
|
||||||
success: () => {
|
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 })
|
this.setData({ deviceList: list, currentDeviceId: device.id })
|
||||||
// 设置MTU为256,提升传输效率(若支持)
|
// 设置MTU为256,提升传输效率(若支持)
|
||||||
if (typeof wx.setBLEMTU === 'function') {
|
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))
|
const isAlreadyConnected = errmsg.indexOf('already connect') >= 0 || errmsg.indexOf('already connected') >= 0 || (err && (err.errCode === -1 || err.errno === 1509007))
|
||||||
if (isAlreadyConnected) {
|
if (isAlreadyConnected) {
|
||||||
try { this.appendLog && this.appendLog('CFG', 'createBLEConnection: already connected, treating as connected') } catch (e) {}
|
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 })
|
this.setData({ deviceList: list, currentDeviceId: device.id })
|
||||||
// 继续发现服务与特征以恢复页面状态
|
// 继续发现服务与特征以恢复页面状态
|
||||||
try { this.discoverBleChannels(device) } catch (e) {}
|
try { this.discoverBleChannels(device) } catch (e) {}
|
||||||
@@ -599,8 +622,8 @@ Page({
|
|||||||
onShow() {
|
onShow() {
|
||||||
// 返回到此页面时,清理导航标记
|
// 返回到此页面时,清理导航标记
|
||||||
if (this._navigatingToB13) {
|
if (this._navigatingToB13) {
|
||||||
|
// 已从 B13 返回:清理导航标记但仍继续执行恢复流程,确保已连接设备状态可被恢复并展示
|
||||||
this._navigatingToB13 = false
|
this._navigatingToB13 = false
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try { this.appendLog && this.appendLog('CFG', 'onShow: resume, ensuring adapter and forcing discovery') } catch (e) {}
|
try { this.appendLog && this.appendLog('CFG', 'onShow: resume, ensuring adapter and forcing discovery') } catch (e) {}
|
||||||
@@ -665,17 +688,28 @@ Page({
|
|||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
try { this.appendLog && this.appendLog('WARN', 'onShow ensureBluetoothReady failed') } catch (e) {}
|
try { this.appendLog && this.appendLog('WARN', 'onShow ensureBluetoothReady failed') } catch (e) {}
|
||||||
})
|
})
|
||||||
|
// 进入页面时启动定时扫描(每3秒一次)
|
||||||
|
try { this.startScanTimer && this.startScanTimer() } catch (e) {}
|
||||||
},
|
},
|
||||||
|
|
||||||
onHide() {
|
onHide() {
|
||||||
// 如果正在导航到 B13 页面,跳过 onHide 的断开/重置流程,保留连接
|
// 如果正在导航到 B13 页面,保留连接会话,但应停止扫描/发现以节省资源
|
||||||
if (this._navigatingToB13) {
|
if (this._navigatingToB13) {
|
||||||
try { this.appendLog && this.appendLog('CFG', 'onHide skipped due to navigation to B13') } catch (e) {}
|
try { this.appendLog && this.appendLog('CFG', 'onHide during navigation to B13: stop scan but keep connection') } catch (e) {}
|
||||||
// 清理标记,后续返回时 onShow 会再次执行
|
// 停止定时扫描
|
||||||
this._navigatingToB13 = false
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 非导航到 B13 的离开:停止定时扫描并断开连接、关闭适配器以重置状态
|
||||||
|
try { this.stopScanTimer && this.stopScanTimer() } catch (e) {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 停止发现,避免后台扫描
|
// 停止发现,避免后台扫描
|
||||||
if (typeof wx.stopBluetoothDevicesDiscovery === 'function') {
|
if (typeof wx.stopBluetoothDevicesDiscovery === 'function') {
|
||||||
|
|||||||
Reference in New Issue
Block a user