[AI Generated]: refactor(*): 重构蓝牙生命周期管理,修复最小化后重连与进度跳步问题

- app.js: 提取 registerBusListeners 方法,新增自动重连机制(3次上限)
- configureDevice: bus 订阅迁移至 onShow,deviceConnect 按步骤区分处理逻辑
- configureDevice_2: setUserInfo 兼容 WIFISUCCESS 状态,WXML 覆盖六态
- configureDevice_3: 新增 reInitWifi 方法,最小化后重置 WiFi 列表
- replaceNetwork: 订阅迁移至 onShow,修复 selectComponent 空指针
- replaceNetwork_3: 修复 domain1 配网参数引用错误
- userInfo: 订阅迁移至 onShow,setUserInfo 兼容 WIFISUCCESS
- searchDevice: 移除 onLoad 重复初始化,统一由 onShow 处理
- 所有 BLE 页面统一 onHide/onUnload 清理:resetReconnectCount + setGlobalData(null) + stop()

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-03-09 17:34:08 +08:00
co-authored by Claude Opus 4.6
parent 6924a1ff5b
commit 81f9301c64
12 changed files with 487 additions and 192 deletions
+63 -22
View File
@@ -19,20 +19,44 @@ Page({
_connectStateWatcher: null,
onLoad(options) {
// 一次性数据初始化
this.checkBluetoothPermissionAndInit();
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
console.log("searchDevice ===》devicesModel", res);
app.globalData.ppScale.device.mac = res.deviceMac;
console.log(`[replaceNetwork] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
this.setData({
BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
connectState: app.globalData.ppScale.device.connectState
})
this._connectStateWatcher = (newValue, oldValue) => {
console.log(`[replaceNetwork] connectState 变化: ${oldValue} -> ${newValue}`);
this.setData({
connectState: newValue || ''
});
};
app.watch('ppScale.device.connectState', this._connectStateWatcher);
},
onShow() {
console.log("replaceNetwork onShow");
// 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
setTimeout(() => {
// 重新注册 app 级别的全局 bus 订阅
app.registerBusListeners();
// 页面级 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('connectedDevice ===》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("connectedDevice ===》codeUpdateMTU", res);
console.log("replaceNetwork ===》codeUpdateMTU", res);
app.globalData.ppScale.device.name = this.data.device.name;
app.globalData.ppScale.device.connection = this.data.device;
@@ -40,23 +64,19 @@ Page({
app.globalData.ppScale.version = nameArr.length === 5 ? "domain2" : "domain1";
wx.hideLoading();
this.selectComponent('#replaceNetwork1Component').getWiFiList();
// 仅在 WiFi 列表步骤时获取列表(组件只在 progress === 0 时渲染)
if (this.data.progress === 0) {
const comp = this.selectComponent('#replaceNetwork1Component');
if (comp) {
comp.getWiFiList();
}
}
});
});
});
console.log(`[Component] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
this.setData({
BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
connectState: app.globalData.ppScale.device.connectState
})
this._connectStateWatcher = (newValue, oldValue) => {
console.log(`[Component] connectState 变化: ${oldValue} -> ${newValue}`);
this.setData({
connectState: newValue || ''
});
};
app.watch('ppScale.device.connectState', this._connectStateWatcher);
// 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
this.checkBluetoothPermissionAndInit();
}, 300);
},
checkBluetoothPermissionAndInit() {
@@ -201,11 +221,22 @@ Page({
})
},
// 点击设备重连
// 手动重连设备
connectedDevice() {
app.resetReconnectCount();
let device = this.data.device;
if(device) {
ppScale.plugin.Blue.createBLEConnection(device);
wx.showLoading({
title: "正在重新连接设备...",
mask: true
});
app.globalData.ppScale.plugin.Blue.disconnect((disres) => {
if (disres.errCode == 0) {
app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
} else {
app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
}
});
}
},
@@ -243,9 +274,19 @@ Page({
})
}
},
onHide() {
console.log("replaceNetwork onHide");
app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null);
app.globalData.ppScale.plugin.Blue.stop();
},
// 卸载事件监听
onUnload() {
console.log("replaceNetwork onUnload");
app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null);
app.globalData.ppScale.plugin.Blue.stop();
if (this._connectStateWatcher) {
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);