diff --git a/app.js b/app.js index a3f45ae..5f9a4d9 100644 --- a/app.js +++ b/app.js @@ -151,23 +151,11 @@ App({ this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin'); }, - // 标记 app 级 bus 是否已订阅,防止重复叠加 - _busSubscribed: false, - - /** - * 注销全局 bus 事件监听(重置订阅标志,下次 registerBusListeners 时重新订阅) - */ - unregisterBusListeners() { - this._busSubscribed = false; - }, - /** * 注册全局 bus 事件监听(connectState / syncDeviceTimeSuccess / deviceWillDisconnect) - * 通过标志位防止多次 onShow 导致回调叠加(SDK bus 不支持 unsubscribe) + * 依赖 Blue.stop() 清空订阅,onShow 时直接调用,无需标志位 */ registerBusListeners() { - if (this._busSubscribed) return; - this._busSubscribed = true; const plugin = this.globalData.ppScale.plugin; // 蓝牙连接状态监听 diff --git a/pages/configureDevice/configureDevice.js b/pages/configureDevice/configureDevice.js index edd04af..af32245 100644 --- a/pages/configureDevice/configureDevice.js +++ b/pages/configureDevice/configureDevice.js @@ -16,74 +16,6 @@ Page({ // 一次性数据初始化,订阅逻辑在 onShow 中注册 }, - // 标志位:防止页面级 bus 重复订阅(SDK bus 不支持 unsubscribe) - _pageBusSubscribed: false, - - // 注册页面级 bus 订阅(通过标志位防重) - _subscribePageBus() { - if (this._pageBusSubscribed) return; - this._pageBusSubscribed = true; - - 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(); - this._startKeepAlive(); - - // 配网步骤:重新获取 WiFi 列表 - 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(); - this._startKeepAlive(); - 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(); - }); - }); - }); - }, - - // 重置页面级 bus 订阅标志(页面离开时调用,下次 onShow 重新订阅) - _unsubscribePageBus() { - this._pageBusSubscribed = false; - }, - // 记录 stop() 调用时间,用于 onShow 动态计算安全延迟 _stopTime: 0, @@ -92,13 +24,58 @@ Page({ const elapsed = Date.now() - (this._stopTime || 0); const delay = Math.max(0, 500 - elapsed); setTimeout(() => { - // 注册 app 级别的全局 bus 订阅 + // 注册 app 级别的全局 bus 订阅(stop() 已清空,直接重新订阅) app.registerBusListeners(); - // 页面级 bus 订阅(标志位防重) - this._subscribePageBus(); + // 页面级 bus 订阅 + ppScale.plugin.bus.subscribe("devicesModel", (res) => { + console.log("===》devicesModel", res); + ppScale.device.mac = res.deviceMac; + this._macReady = true; + this._tryProgress(); + }); - // devicesList 每次 onShow 都需要重新订阅(重连场景) + 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(); + this._startKeepAlive(); + 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(); + this._startKeepAlive(); + 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) => { @@ -287,18 +264,11 @@ Page({ onHide() { console.log("configureDevice onHide"); this._stopKeepAlive(); - this._unsubscribePageBus(); - app.unregisterBusListeners(); // 配网步骤:立即清空 WiFi 列表(stop 后数据已失效) if (this.data.progress.index === 2) { const comp = this.selectComponent('#configureDevice3'); if (comp) { - comp.setData({ - progress: 0, - wifiList: [], - selectWifi: null, - selectWifiPWD: "" - }); + comp.setData({ progress: 0, wifiList: [], selectWifi: null, selectWifiPWD: "" }); } } app.resetReconnectCount(); @@ -312,8 +282,6 @@ Page({ onUnload() { console.log("configureDevice onUnload"); this._stopKeepAlive(); - this._unsubscribePageBus(); - app.unregisterBusListeners(); app.resetReconnectCount(); app.setGlobalData('ppScale.device.connectState', null); this._connectReady = false; diff --git a/pages/replaceNetwork/replaceNetwork.js b/pages/replaceNetwork/replaceNetwork.js index 735b78d..88ce7d9 100644 --- a/pages/replaceNetwork/replaceNetwork.js +++ b/pages/replaceNetwork/replaceNetwork.js @@ -18,44 +18,6 @@ Page({ // 存储回调函数的引用,以便在 onUnload 时取消监听 _connectStateWatcher: null, - // 标志位:防止页面级 bus 重复订阅(SDK bus 不支持 unsubscribe) - _pageBusSubscribed: false, - - // 注册页面级 bus 订阅(通过标志位防重) - _subscribePageBus() { - if (this._pageBusSubscribed) return; - this._pageBusSubscribed = true; - - app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => { - console.log("replaceNetwork ===》devicesModel", res); - app.globalData.ppScale.device.mac = res.deviceMac; - }); - - app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => { - console.log('replaceNetwork ===》deviceConnect', res); - app.globalData.ppScale.plugin.ScaleAction.startDataProgress(true); - app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol(); - app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => { - console.log("replaceNetwork ===》codeUpdateMTU", res); - app.globalData.ppScale.device.name = this.data.device.name; - app.globalData.ppScale.device.connection = this.data.device; - const nameArr = this.data.device.name.split("-"); - app.globalData.ppScale.version = nameArr.length === 5 ? "domain2" : "domain1"; - wx.hideLoading(); - this._startKeepAlive(); - if (this.data.progress === 0) { - const comp = this.selectComponent('#replaceNetwork1Component'); - if (comp) comp.getWiFiList(); - } - }); - }); - }, - - // 重置订阅标志,下次 onShow 时重新订阅 - _unsubscribePageBus() { - this._pageBusSubscribed = false; - }, - onLoad(options) { // 一次性数据初始化 this.checkBluetoothPermissionAndInit(); @@ -76,13 +38,34 @@ Page({ onShow() { console.log("replaceNetwork onShow"); - // 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo) setTimeout(() => { - // 重新注册 app 级别的全局 bus 订阅 + // 注册 app 级别的全局 bus 订阅(stop() 已清空,直接重新订阅) app.registerBusListeners(); - // 页面级 bus 订阅(标志位防重) - this._subscribePageBus(); + // 页面级 bus 订阅 + app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => { + console.log("replaceNetwork ===》devicesModel", res); + app.globalData.ppScale.device.mac = res.deviceMac; + }); + + app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => { + console.log('replaceNetwork ===》deviceConnect', res); + app.globalData.ppScale.plugin.ScaleAction.startDataProgress(true); + app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol(); + app.globalData.ppScale.activeProtocol.codeUpdateMTU((mtuRes) => { + console.log("replaceNetwork ===》codeUpdateMTU", mtuRes); + app.globalData.ppScale.device.name = this.data.device.name; + app.globalData.ppScale.device.connection = this.data.device; + const nameArr = this.data.device.name.split("-"); + app.globalData.ppScale.version = nameArr.length === 5 ? "domain2" : "domain1"; + wx.hideLoading(); + this._startKeepAlive(); + if (this.data.progress === 0) { + const comp = this.selectComponent('#replaceNetwork1Component'); + if (comp) comp.getWiFiList(); + } + }); + }); // 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start) this.checkBluetoothPermissionAndInit(); @@ -301,19 +284,14 @@ Page({ onHide() { console.log("replaceNetwork onHide"); this._stopKeepAlive(); - this._unsubscribePageBus(); - app.unregisterBusListeners(); app.resetReconnectCount(); app.setGlobalData('ppScale.device.connectState', null); app.globalData.ppScale.plugin.Blue.stop(); }, - // 卸载事件监听 onUnload() { console.log("replaceNetwork onUnload"); this._stopKeepAlive(); - this._unsubscribePageBus(); - app.unregisterBusListeners(); app.resetReconnectCount(); app.setGlobalData('ppScale.device.connectState', null); app.globalData.ppScale.plugin.Blue.stop();