[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:
co-authored by
Claude Opus 4.6
parent
6924a1ff5b
commit
81f9301c64
+59
-29
@@ -37,37 +37,16 @@ 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;
|
||||
|
||||
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
||||
console.log('connectedDevice ===》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);
|
||||
|
||||
|
||||
app.globalData.ppScale.device.name = this.data.device.name;
|
||||
app.globalData.ppScale.device.connection = this.data.device;
|
||||
|
||||
wx.hideLoading();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`[Component] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
|
||||
console.log(`[userInfo] 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}`);
|
||||
console.log(`[userInfo] connectState 变化: ${oldValue} -> ${newValue}`);
|
||||
this.setData({
|
||||
connectState: newValue || ''
|
||||
});
|
||||
@@ -77,7 +56,7 @@ Page({
|
||||
let userInfo = wx.getStorageSync("userInfo") || null;
|
||||
if(userInfo) {
|
||||
let [year, month, day] = userInfo.birthday.split("-");
|
||||
|
||||
|
||||
this.setData({
|
||||
userId: userInfo.userId,
|
||||
realname: userInfo.realname,
|
||||
@@ -101,6 +80,40 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
onShow() {
|
||||
console.log("userInfo onShow");
|
||||
// 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
|
||||
setTimeout(() => {
|
||||
// 重新注册 app 级别的全局 bus 订阅
|
||||
app.registerBusListeners();
|
||||
|
||||
// 页面级 bus 订阅
|
||||
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
|
||||
console.log("userInfo ===》devicesModel", res);
|
||||
app.globalData.ppScale.device.mac = res.deviceMac;
|
||||
});
|
||||
|
||||
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
||||
console.log('userInfo ===》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("userInfo ===》codeUpdateMTU", res);
|
||||
|
||||
app.globalData.ppScale.device.name = this.data.device.name;
|
||||
app.globalData.ppScale.device.connection = this.data.device;
|
||||
|
||||
wx.hideLoading();
|
||||
});
|
||||
});
|
||||
|
||||
// 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
|
||||
this.checkBluetoothPermissionAndInit();
|
||||
}, 300);
|
||||
},
|
||||
|
||||
// 姓名输入
|
||||
realnameInput(e) {
|
||||
let realname = e.detail.value.replace(/\s+/g, '');
|
||||
@@ -156,7 +169,7 @@ Page({
|
||||
},
|
||||
|
||||
setUserInfo() {
|
||||
if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS) {
|
||||
if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS || this.data.connectState == this.data.BLUE_STATE.WIFISUCCESS) {
|
||||
let realname = this.data.realname;
|
||||
let sex = this.data.sex;
|
||||
let birthday = this.data.birthday;
|
||||
@@ -414,19 +427,36 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
// 点击设备重连
|
||||
// 手动重连设备
|
||||
connectedDevice() {
|
||||
app.resetReconnectCount();
|
||||
let device = this.data.device;
|
||||
if(device) {
|
||||
wx.showLoading({
|
||||
wx.showLoading({
|
||||
title: "正在重新连接设备...",
|
||||
mask: true
|
||||
});
|
||||
app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
onHide() {
|
||||
console.log("userInfo onHide");
|
||||
app.resetReconnectCount();
|
||||
app.setGlobalData('ppScale.device.connectState', null);
|
||||
app.globalData.ppScale.plugin.Blue.stop();
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
console.log("userInfo 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);
|
||||
|
||||
Reference in New Issue
Block a user