[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
+41 -59
View File
@@ -18,20 +18,42 @@ Page({
// 存储回调函数的引用,以便在 onUnload 时取消监听
_connectStateWatcher: null,
// 存储页面级 bus 回调引用,用于精准 unsubscribe
_pageHandlers: {
devicesModel: null,
deviceConnect: null,
devicesList: 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();
}
});
});
},
// 注销页面级 bus 订阅
// 重置订阅标志,下次 onShow 时重新订阅
_unsubscribePageBus() {
const bus = app.globalData.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;
},
onLoad(options) {
@@ -59,41 +81,8 @@ Page({
// 重新注册 app 级别的全局 bus 订阅
app.registerBusListeners();
// 页面级 bus 订阅(先清理旧的,防止重复叠加
this._unsubscribePageBus();
this._pageHandlers.devicesModel = (res) => {
console.log("replaceNetwork ===》devicesModel", res);
app.globalData.ppScale.device.mac = res.deviceMac;
};
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", this._pageHandlers.devicesModel);
this._pageHandlers.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();
// 仅在 WiFi 列表步骤时获取列表(组件只在 progress === 0 时渲染)
if (this.data.progress === 0) {
const comp = this.selectComponent('#replaceNetwork1Component');
if (comp) {
comp.getWiFiList();
}
}
});
};
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", this._pageHandlers.deviceConnect);
// 页面级 bus 订阅(标志位防重
this._subscribePageBus();
// 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
this.checkBluetoothPermissionAndInit();
@@ -223,23 +212,16 @@ Page({
});
app.globalData.ppScale.plugin.Blue.setDeviceSetting([seviceList]);
app.globalData.ppScale.plugin.Blue.start(res.result[0].type, false);
this._pageHandlers.devicesList = (res_) => {
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => {
console.log("searchDevice ===> devicesList", res_);
let fIndex = res_.findIndex(item => item.deviceId === res.result[0].deviceId);
if (fIndex >= 0) {
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
wx.showLoading({
title: "正在连接设备...",
mask: true
});
this.setData({
device: res_[fIndex]
})
app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[fIndex]);
}
};
app.globalData.ppScale.plugin.bus.subscribe("devicesList", this._pageHandlers.devicesList);
wx.showLoading({ title: "正在连接设备...", mask: true });
this.setData({ device: res_[fIndex] });
app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[fIndex]);
}
});
})
},