[AI Generated]: refactor(*): 对齐 SDK demo 模式,移除 bus 标志位,依赖 Blue.stop() 清空订阅,onShow 直接重新 subscribe
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user