增加蓝牙自动搜索

This commit is contained in:
2026-01-21 19:29:00 +08:00
parent 8d244cb316
commit 8cceee566d
3 changed files with 208 additions and 40 deletions

View File

@@ -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/';

View File

@@ -17,6 +17,31 @@
</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: 设备测试 -->
<view wx:if="{{TabCur==1}}" class="content">
@@ -31,8 +56,16 @@
<view class="grid-and-actions">
<view class="grid">
<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>
<text class="label">{{item.label}}</text>
</view>
</view>
@@ -157,7 +190,7 @@
<view class="log-input-row">
<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>
<!-- <scroll-view class="log-scroll" scroll-y="true">
@@ -199,23 +232,7 @@
<!-- Tab: 设备升级 -->
<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="toolbar">