From 8cceee566d4afd7d5e69073ae5948b7fbebb8f35 Mon Sep 17 00:00:00 2001
From: chenzhihao <1798906853@qq.com>
Date: Wed, 21 Jan 2026 19:29:00 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=93=9D=E7=89=99=E8=87=AA?=
=?UTF-8?q?=E5=8A=A8=E6=90=9C=E7=B4=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../BluetoothDebugging/B13page/B13page.js | 123 +++++++++++++++++-
.../BluetoothDebugging/B13page/B13page.wxml | 55 +++++---
.../BluetoothDebugging/BluetoothDebugging.js | 70 +++++++---
3 files changed, 208 insertions(+), 40 deletions(-)
diff --git a/pages/basics/BluetoothDebugging/B13page/B13page.js b/pages/basics/BluetoothDebugging/B13page/B13page.js
index f4fe7fc..9169852 100644
--- a/pages/basics/BluetoothDebugging/B13page/B13page.js
+++ b/pages/basics/BluetoothDebugging/B13page/B13page.js
@@ -19,6 +19,10 @@ Page({
bleMac: '00:00:00:00:00:00',
bleAMC: '-',
bleVersion: '-',
+ // 设备信号/RSSI/连接状态显示
+ bleSignal: '-',
+ bleRSSI: '-',
+ isConnected: false,
openDelay: 20,
bathDelay: 20,
logs: [],
@@ -141,6 +145,8 @@ Page({
const txCharId = options && options.txCharId ? decodeURIComponent(options.txCharId) : ''
const rxCharId = options && options.rxCharId ? decodeURIComponent(options.rxCharId) : ''
this.setData({ DevName: devName, bleName: devName, bleMac, deviceId, serviceId, txCharId, rxCharId })
+ // 尝试更新设备连接/信号信息
+ try { this.updateDeviceInfo && this.updateDeviceInfo() } catch (e) {}
// 初始化 picker 选项(触发/释放分别根据各自单位生成)
this.updateDoorTriggerOptions(this.data.doorTriggerUnitIndex || 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) {
const i = Number(idx || 0)
return i === 0 ? 1 : (i === 1 ? 2 : 3)
@@ -263,10 +321,19 @@ Page({
})
}, 250)
} catch (e) { /* ignore */ }
+ try { this.updateDeviceInfo && this.updateDeviceInfo() } catch (e) {}
},
onUnload() {
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)
this.setData({ TabCur: id })
// 切换到蓝牙升级标签时,主动读取蓝牙版本号并保存到 bleVersion
- if (id === 2) {
- this.onSendReadVersion()
- }
+ // if (id === 1) {
+
+ // }
// 切换到设备升级标签(data-id === 3)时,默认发送一包 OTA 升级命令
if (id === 3) {
// 给用户一点缓冲时间再发送(确保 UI 切换完成)
@@ -998,6 +1065,7 @@ Page({
// 开启订阅(若已传入rx特征),保证能接收数据
this.enableNotify()
this.setupBleListener()
+ this.onSendReadVersion()
this.sendRadarStatusCommand(true)
},
@@ -1234,6 +1302,55 @@ Page({
}
// 注册系统 BLE 通知回调
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() {
const url = 'https://www.baidu.com/';
diff --git a/pages/basics/BluetoothDebugging/B13page/B13page.wxml b/pages/basics/BluetoothDebugging/B13page/B13page.wxml
index dc1b8f4..32d86c2 100644
--- a/pages/basics/BluetoothDebugging/B13page/B13page.wxml
+++ b/pages/basics/BluetoothDebugging/B13page/B13page.wxml
@@ -17,6 +17,31 @@
+
+
+
+
+ 蓝牙名称:
+ {{bleName || '-'}}
+
+
+ MAC:
+ {{bleMac || '-'}}
+ 版本:
+ {{bleVersion || '-'}}
+
+
+ 信号:
+ {{bleSignal}}
+ RSSI:
+ {{bleRSSI}}
+ 状态:
+ {{isConnected ? '已连接' : '未连接'}}
+
+
+ 读取蓝牙信息
+
+
@@ -31,8 +56,16 @@
+
+
+ {{
+ item.key === 'door' ? (item.triggered ? '门磁(开门)' : '门磁(关门)') :
+ item.key === 'bath' ? (item.triggered ? '卫浴(触发)' : '卫浴(释放)') :
+ item.key === 'bed' ? (item.triggered ? '卧室(触发)' : '卧室(释放)') :
+ item.key === 'hall' ? (item.triggered ? '走廊(触发)' : '走廊(释放)') : item.label
+ }}
+
- {{item.label}}
@@ -157,7 +190,7 @@
-
+
-
-
-
-
- 蓝牙名称:
- {{bleName || '-'}}
-
-
- MAC:
- {{bleMac || '-'}}
- 版本:
- {{bleVersion || '-'}}
-
-
- 读取蓝牙信息
-
-
+
diff --git a/pages/basics/BluetoothDebugging/BluetoothDebugging.js b/pages/basics/BluetoothDebugging/BluetoothDebugging.js
index ad77d04..6738152 100644
--- a/pages/basics/BluetoothDebugging/BluetoothDebugging.js
+++ b/pages/basics/BluetoothDebugging/BluetoothDebugging.js
@@ -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') {