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 @@