[AI Generated]: fix(*): 修复 SDK bus 不支持 unsubscribe 导致的崩溃,改用标志位防重复订阅;修复 OTA 回调注释逻辑

This commit is contained in:
17792275749
2026-04-08 14:00:02 +08:00
parent 228c68abca
commit 8e1be13602
4 changed files with 159 additions and 195 deletions
+70 -78
View File
@@ -16,20 +16,72 @@ Page({
// 一次性数据初始化,订阅逻辑在 onShow 中注册
},
// 存储页面级 bus 回调引用,用于精准 unsubscribe
_pageHandlers: {
devicesModel: null,
deviceConnect: null,
devicesList: null,
// 标志位:防止页面级 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 订阅
// 重置页面级 bus 订阅标志(页面离开时调用,下次 onShow 重新订阅)
_unsubscribePageBus() {
const bus = ppScale.plugin.bus;
const h = this._pageHandlers;
if (h.devicesModel) { bus.unsubscribe('devicesModel', h.devicesModel); h.devicesModel = null; }
if (h.deviceConnect) { bus.unsubscribe('deviceConnect', h.deviceConnect); h.deviceConnect = null; }
if (h.devicesList) { bus.unsubscribe('devicesList', h.devicesList); h.devicesList = null; }
this._pageBusSubscribed = false;
},
// 记录 stop() 调用时间,用于 onShow 动态计算安全延迟
@@ -37,90 +89,30 @@ Page({
onShow() {
console.log("configureDevice onShow");
// 动态计算距上次 stop() 已过去的时间,确保间隔至少 500ms 再重新注册
const elapsed = Date.now() - (this._stopTime || 0);
const delay = Math.max(0, 500 - elapsed);
setTimeout(() => {
// 重新注册 app 级别的全局 bus 订阅(内部已做 unsubscribe 防重)
// 注册 app 级别的全局 bus 订阅
app.registerBusListeners();
// 页面级 bus 订阅(先清理旧的,防止重复叠加
this._unsubscribePageBus();
// 页面级 bus 订阅(标志位防重
this._subscribePageBus();
this._pageHandlers.devicesModel = (res) => {
console.log("===》devicesModel", res);
ppScale.device.mac = res.deviceMac;
this._macReady = true;
this._tryProgress();
};
ppScale.plugin.bus.subscribe("devicesModel", this._pageHandlers.devicesModel);
this._pageHandlers.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((res) => {
console.log("===》重连 codeUpdateMTU", res);
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((res) => {
console.log("===》codeUpdateMTU", res);
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();
})
});
};
ppScale.plugin.bus.subscribe("deviceConnect", this._pageHandlers.deviceConnect);
// 如果之前已选择过设备,自动扫描并重连
// devicesList 每次 onShow 都需要重新订阅(重连场景)
if (this.data.device) {
wx.showLoading({ title: "正在重新连接...", mask: true });
this._pageHandlers.devicesList = (res) => {
ppScale.plugin.bus.subscribe("devicesList", (res) => {
let fIndex = res.findIndex(item => item.deviceId === this.data.device.deviceId);
if (fIndex >= 0) {
ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
ppScale.plugin.Blue.createBLEConnection(res[fIndex]);
}
};
ppScale.plugin.bus.subscribe("devicesList", this._pageHandlers.devicesList);
});
}
// 通过完整链路重新初始化蓝牙(权限检查 → 打开适配器 → start)
this.checkBluetoothPermissionAndInit();
}, 300);
}, delay);
},
// 选择了某个设备