const app = getApp(); const ppScale = app.globalData.ppScale; Page({ data: { progress: { index: 0, data: ['配置蓝牙', '初始化用户信息', '配备网络', '完成'] }, progressNext: false, device: null }, onLoad() { // 一次性数据初始化,订阅逻辑在 onShow 中注册 }, // 记录 stop() 调用时间,用于 onShow 动态计算安全延迟 _stopTime: 0, onShow() { console.log("configureDevice onShow"); const elapsed = Date.now() - (this._stopTime || 0); const delay = Math.max(0, 500 - elapsed); setTimeout(() => { // 注册 app 级别的全局 bus 订阅(stop() 已清空,直接重新订阅) app.registerBusListeners(); // 页面级 bus 订阅 ppScale.plugin.bus.subscribe("devicesModel", (res) => { console.log("===》devicesModel", res); ppScale.device.mac = res.deviceMac; this._macReady = true; this._tryProgress(); }); ppScale.plugin.bus.subscribe("deviceConnect", (res) => { console.log('===》deviceConnect', res); ppScale.plugin.ScaleAction.startDataProgress(true); ppScale.activeProtocol = ppScale.plugin.ScaleAction.getActiveProtocol(); let currentStep = this.data.progress.index; // 步骤 1/2/3:最小化后重连,只需恢复连接状态 if (currentStep >= 1) { ppScale.activeProtocol.codeUpdateMTU((mtuRes) => { console.log("===》重连 codeUpdateMTU", mtuRes); ppScale.device.name = this.data.device.name; ppScale.device.connection = this.data.device; wx.hideLoading(); if (currentStep === 2) { const comp = this.selectComponent('#configureDevice3'); if (comp) comp.reInitWifi(); } }); return; } // 步骤 0:首次连接设备 ppScale.activeProtocol.codeUpdateMTU((mtuRes) => { console.log("===》codeUpdateMTU", mtuRes); ppScale.activeProtocol.codeSyncTime((codeSyncTime) => { console.log("===》codeSyncTime", codeSyncTime); wx.hideLoading(); ppScale.device.name = this.data.device.name; ppScale.device.connection = this.data.device; const nameArr = this.data.device.name.split("-"); ppScale.version = nameArr.length === 5 ? "domain2" : "domain1"; this._connectReady = true; this._tryProgress(); }); }); }); // 如果之前已选择过设备,自动扫描并重连 if (this.data.device) { wx.showLoading({ title: "正在重新连接...", mask: true }); ppScale.plugin.bus.subscribe("devicesList", (res) => { let fIndex = res.findIndex(item => item.deviceId === this.data.device.deviceId); if (fIndex >= 0) { ppScale.plugin.Blue.stopBluetoothDevicesDiscovery(); ppScale.plugin.Blue.createBLEConnection(res[fIndex]); } }); } // 通过完整链路重新初始化蓝牙(权限检查 → 打开适配器 → start) this.checkBluetoothPermissionAndInit(); }, delay); }, // 选择了某个设备 deviceChange(e) { let status = e.detail.status; if (status) { wx.showLoading({ title: "蓝牙配对中...", mask: true }); this.setData({ progressNext: true }) let device = e.detail.device; this.connectedDevice_(device); } }, connectedDevice(e) { app.resetReconnectCount(); wx.showLoading({ title: "正在尝试连接...", mask: true }); this.setData({ progressNext: false }) let device = e.detail.device; // 先断开旧连接再重新连接 ppScale.plugin.Blue.disconnect((disres) => { this.connectedDevice_(device); }); }, connectedDevice_(device) { if (device) { this.setData({ device: device }) ppScale.plugin.Blue.createBLEConnection(device); } }, setDeviceUserList(e) { let status = e.detail.status; if (status) { this.setProgress(); } }, setDeviceConfig(e) { let status = e.detail.status; if (status) { this.setProgress(); } }, setConfigSuccessful() { app.cleanupConnection(); wx.switchTab({ url: "/pages/home/home" }) }, setProgress() { const newIndex = this.data.progress.index + 1; this.setData({ "progress.index": newIndex }); // 进入步骤2(配网)时,等待组件渲染完成后由父页面显式触发 WiFi 列表获取 if (newIndex === 2) { setTimeout(() => { const comp = this.selectComponent('#configureDevice3'); if (comp) comp.initWifi(); }, 100); } // 进入步骤3(OTA升级)时,等待组件渲染完成后由父页面显式触发升级 if (newIndex === 3) { setTimeout(() => { const comp = this.selectComponent('#configureDevice4'); if (comp) comp.startOtaUpgrade(); }, 100); } }, // 连接完成 + mac 获取完成,双条件满足后才进入下一步 _connectReady: false, _macReady: false, _tryProgress() { if (this._connectReady && this._macReady && this.data.progressNext) { this._connectReady = false; this._macReady = false; this.setData({ progressNext: false }); this.setProgress(); } }, checkBluetoothPermissionAndInit() { wx.getSetting({ success: (res) => { if (res.authSetting['scope.bluetooth']) { this.openBluetoothAdapter(); } else { wx.authorize({ scope: 'scope.bluetooth', success: () => { this.openBluetoothAdapter(); }, fail: () => { wx.hideLoading(); wx.showModal({ title: '提示', content: '蓝牙权限被拒绝,无法连接设备。是否前往设置开启?', confirmText: '去开启', cancelText: '不开启', success: (modalRes) => { if (modalRes.confirm) { wx.openSetting(); } } }); } }); } }, fail: () => { wx.hideLoading(); } }); }, openBluetoothAdapter() { wx.openBluetoothAdapter({ success: () => { // 蓝牙适配器打开成功,设置设备配置后启动扫描 let setting = ppScale.device.setting; ppScale.plugin.Blue.setDeviceSetting(setting); let deviceNames = setting.map(item => item.deviceName); ppScale.plugin.Blue.start(deviceNames, false); }, fail: (err) => { wx.hideLoading(); if (err.errCode === 10001) { wx.showModal({ title: "提示", content: "请确保手机蓝牙已开启。", showCancel: false }); } } }); }, onHide() { console.log("configureDevice onHide"); app.cleanupConnection(); // 配网步骤:立即清空 WiFi 列表(stop 后数据已失效) if (this.data.progress.index === 2) { const comp = this.selectComponent('#configureDevice3'); if (comp) { comp.setData({ progress: 0, wifiList: [], selectWifi: null, selectWifiPWD: "" }); } } this._connectReady = false; this._macReady = false; this._stopTime = Date.now(); }, onUnload() { console.log("configureDevice onUnload"); app.cleanupConnection(); this._connectReady = false; this._macReady = false; this._stopTime = Date.now(); } })