[AI Generated]: refactor(*): 对齐 SDK demo 模式,移除 bus 标志位,依赖 Blue.stop() 清空订阅,onShow 直接重新 subscribe
This commit is contained in:
@@ -151,23 +151,11 @@ App({
|
|||||||
this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin');
|
this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin');
|
||||||
},
|
},
|
||||||
|
|
||||||
// 标记 app 级 bus 是否已订阅,防止重复叠加
|
|
||||||
_busSubscribed: false,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 注销全局 bus 事件监听(重置订阅标志,下次 registerBusListeners 时重新订阅)
|
|
||||||
*/
|
|
||||||
unregisterBusListeners() {
|
|
||||||
this._busSubscribed = false;
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注册全局 bus 事件监听(connectState / syncDeviceTimeSuccess / deviceWillDisconnect)
|
* 注册全局 bus 事件监听(connectState / syncDeviceTimeSuccess / deviceWillDisconnect)
|
||||||
* 通过标志位防止多次 onShow 导致回调叠加(SDK bus 不支持 unsubscribe)
|
* 依赖 Blue.stop() 清空订阅,onShow 时直接调用,无需标志位
|
||||||
*/
|
*/
|
||||||
registerBusListeners() {
|
registerBusListeners() {
|
||||||
if (this._busSubscribed) return;
|
|
||||||
this._busSubscribed = true;
|
|
||||||
const plugin = this.globalData.ppScale.plugin;
|
const plugin = this.globalData.ppScale.plugin;
|
||||||
|
|
||||||
// 蓝牙连接状态监听
|
// 蓝牙连接状态监听
|
||||||
|
|||||||
@@ -16,74 +16,6 @@ Page({
|
|||||||
// 一次性数据初始化,订阅逻辑在 onShow 中注册
|
// 一次性数据初始化,订阅逻辑在 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 动态计算安全延迟
|
// 记录 stop() 调用时间,用于 onShow 动态计算安全延迟
|
||||||
_stopTime: 0,
|
_stopTime: 0,
|
||||||
|
|
||||||
@@ -92,13 +24,58 @@ Page({
|
|||||||
const elapsed = Date.now() - (this._stopTime || 0);
|
const elapsed = Date.now() - (this._stopTime || 0);
|
||||||
const delay = Math.max(0, 500 - elapsed);
|
const delay = Math.max(0, 500 - elapsed);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// 注册 app 级别的全局 bus 订阅
|
// 注册 app 级别的全局 bus 订阅(stop() 已清空,直接重新订阅)
|
||||||
app.registerBusListeners();
|
app.registerBusListeners();
|
||||||
|
|
||||||
// 页面级 bus 订阅(标志位防重)
|
// 页面级 bus 订阅
|
||||||
this._subscribePageBus();
|
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) {
|
if (this.data.device) {
|
||||||
wx.showLoading({ title: "正在重新连接...", mask: true });
|
wx.showLoading({ title: "正在重新连接...", mask: true });
|
||||||
ppScale.plugin.bus.subscribe("devicesList", (res) => {
|
ppScale.plugin.bus.subscribe("devicesList", (res) => {
|
||||||
@@ -287,18 +264,11 @@ Page({
|
|||||||
onHide() {
|
onHide() {
|
||||||
console.log("configureDevice onHide");
|
console.log("configureDevice onHide");
|
||||||
this._stopKeepAlive();
|
this._stopKeepAlive();
|
||||||
this._unsubscribePageBus();
|
|
||||||
app.unregisterBusListeners();
|
|
||||||
// 配网步骤:立即清空 WiFi 列表(stop 后数据已失效)
|
// 配网步骤:立即清空 WiFi 列表(stop 后数据已失效)
|
||||||
if (this.data.progress.index === 2) {
|
if (this.data.progress.index === 2) {
|
||||||
const comp = this.selectComponent('#configureDevice3');
|
const comp = this.selectComponent('#configureDevice3');
|
||||||
if (comp) {
|
if (comp) {
|
||||||
comp.setData({
|
comp.setData({ progress: 0, wifiList: [], selectWifi: null, selectWifiPWD: "" });
|
||||||
progress: 0,
|
|
||||||
wifiList: [],
|
|
||||||
selectWifi: null,
|
|
||||||
selectWifiPWD: ""
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
app.resetReconnectCount();
|
app.resetReconnectCount();
|
||||||
@@ -312,8 +282,6 @@ Page({
|
|||||||
onUnload() {
|
onUnload() {
|
||||||
console.log("configureDevice onUnload");
|
console.log("configureDevice onUnload");
|
||||||
this._stopKeepAlive();
|
this._stopKeepAlive();
|
||||||
this._unsubscribePageBus();
|
|
||||||
app.unregisterBusListeners();
|
|
||||||
app.resetReconnectCount();
|
app.resetReconnectCount();
|
||||||
app.setGlobalData('ppScale.device.connectState', null);
|
app.setGlobalData('ppScale.device.connectState', null);
|
||||||
this._connectReady = false;
|
this._connectReady = false;
|
||||||
|
|||||||
@@ -18,44 +18,6 @@ Page({
|
|||||||
// 存储回调函数的引用,以便在 onUnload 时取消监听
|
// 存储回调函数的引用,以便在 onUnload 时取消监听
|
||||||
_connectStateWatcher: null,
|
_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) {
|
onLoad(options) {
|
||||||
// 一次性数据初始化
|
// 一次性数据初始化
|
||||||
this.checkBluetoothPermissionAndInit();
|
this.checkBluetoothPermissionAndInit();
|
||||||
@@ -76,13 +38,34 @@ Page({
|
|||||||
|
|
||||||
onShow() {
|
onShow() {
|
||||||
console.log("replaceNetwork onShow");
|
console.log("replaceNetwork onShow");
|
||||||
// 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// 重新注册 app 级别的全局 bus 订阅
|
// 注册 app 级别的全局 bus 订阅(stop() 已清空,直接重新订阅)
|
||||||
app.registerBusListeners();
|
app.registerBusListeners();
|
||||||
|
|
||||||
// 页面级 bus 订阅(标志位防重)
|
// 页面级 bus 订阅
|
||||||
this._subscribePageBus();
|
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)
|
// 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
|
||||||
this.checkBluetoothPermissionAndInit();
|
this.checkBluetoothPermissionAndInit();
|
||||||
@@ -301,19 +284,14 @@ Page({
|
|||||||
onHide() {
|
onHide() {
|
||||||
console.log("replaceNetwork onHide");
|
console.log("replaceNetwork onHide");
|
||||||
this._stopKeepAlive();
|
this._stopKeepAlive();
|
||||||
this._unsubscribePageBus();
|
|
||||||
app.unregisterBusListeners();
|
|
||||||
app.resetReconnectCount();
|
app.resetReconnectCount();
|
||||||
app.setGlobalData('ppScale.device.connectState', null);
|
app.setGlobalData('ppScale.device.connectState', null);
|
||||||
app.globalData.ppScale.plugin.Blue.stop();
|
app.globalData.ppScale.plugin.Blue.stop();
|
||||||
},
|
},
|
||||||
|
|
||||||
// 卸载事件监听
|
|
||||||
onUnload() {
|
onUnload() {
|
||||||
console.log("replaceNetwork onUnload");
|
console.log("replaceNetwork onUnload");
|
||||||
this._stopKeepAlive();
|
this._stopKeepAlive();
|
||||||
this._unsubscribePageBus();
|
|
||||||
app.unregisterBusListeners();
|
|
||||||
app.resetReconnectCount();
|
app.resetReconnectCount();
|
||||||
app.setGlobalData('ppScale.device.connectState', null);
|
app.setGlobalData('ppScale.device.connectState', null);
|
||||||
app.globalData.ppScale.plugin.Blue.stop();
|
app.globalData.ppScale.plugin.Blue.stop();
|
||||||
|
|||||||
Reference in New Issue
Block a user