[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
+13 -34
View File
@@ -151,44 +151,27 @@ App({
this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin');
},
// 存储 app 级 bus 回调引用,用于精准 unsubscribe
_busCallbacks: {
connectState: null,
syncDeviceTimeSuccess: null,
deviceWillDisconnect: null,
},
// 标记 app 级 bus 是否已订阅,防止重复叠加
_busSubscribed: false,
/**
* 注销全局 bus 事件监听,防止重复订阅叠加
* 注销全局 bus 事件监听(重置订阅标志,下次 registerBusListeners 时重新订阅)
*/
unregisterBusListeners() {
const plugin = this.globalData.ppScale.plugin;
if (!plugin) return;
const cbs = this._busCallbacks;
if (cbs.connectState) {
plugin.bus.unsubscribe('connectState', cbs.connectState);
cbs.connectState = null;
}
if (cbs.syncDeviceTimeSuccess) {
plugin.bus.unsubscribe('syncDeviceTimeSuccess', cbs.syncDeviceTimeSuccess);
cbs.syncDeviceTimeSuccess = null;
}
if (cbs.deviceWillDisconnect) {
plugin.bus.unsubscribe('deviceWillDisconnect', cbs.deviceWillDisconnect);
cbs.deviceWillDisconnect = null;
}
this._busSubscribed = false;
},
/**
* 注册全局 bus 事件监听(connectState / syncDeviceTimeSuccess / deviceWillDisconnect
* 调用前先 unsubscribe 旧的,防止多次 onShow 导致回调叠加
* 通过标志位防止多次 onShow 导致回调叠加(SDK bus 不支持 unsubscribe
*/
registerBusListeners() {
this.unregisterBusListeners();
if (this._busSubscribed) return;
this._busSubscribed = true;
const plugin = this.globalData.ppScale.plugin;
// 蓝牙连接状态监听
this._busCallbacks.connectState = (res) => {
plugin.bus.subscribe('connectState', (res) => {
console.log("app.js ===> connectState", res);
this.setGlobalData('ppScale.device.connectState', res);
if (res == plugin.BLUE_STATE.CONNECTSUCCESS) {
@@ -198,22 +181,18 @@ App({
this.reconnectDevice();
}
wx.hideLoading();
};
});
// 同步时间
this._busCallbacks.syncDeviceTimeSuccess = (res) => {
plugin.bus.subscribe('syncDeviceTimeSuccess', (res) => {
console.log("app.js ===> syncDeviceTimeSuccess", res);
};
});
// 设备自动断开监听
this._busCallbacks.deviceWillDisconnect = (res) => {
plugin.bus.subscribe('deviceWillDisconnect', (res) => {
console.log("app.js ===> deviceWillDisconnect", res);
this.setGlobalData('ppScale.device.connectState', plugin.BLUE_STATE.CONNECTFAILED);
};
plugin.bus.subscribe('connectState', this._busCallbacks.connectState);
plugin.bus.subscribe('syncDeviceTimeSuccess', this._busCallbacks.syncDeviceTimeSuccess);
plugin.bus.subscribe('deviceWillDisconnect', this._busCallbacks.deviceWillDisconnect);
});
},
/**
@@ -1,8 +1,5 @@
const app = getApp();
// OTA 超时时间:1 分钟
const OTA_TIMEOUT_MS = 1 * 60 * 1000;
Component({
data: {
otaProgress: 0,
@@ -59,29 +56,43 @@ Component({
this._otaTimer = setTimeout(() => {
console.log('OTA 升级超时');
this.handleOtaFailed('升级超时,设备即将重启');
}, OTA_TIMEOUT_MS);
}, 30000);
// 回调参数 { progress, isFailed }
activeProtocol.codeOtaUpdate((res) => {
// 回调参数:数字(指令状态码)或对象 { progress, isFailed }
activeProtocol.codeOtaUpdate((res, status) => {
console.log('OTA 升级回调:', status);
console.log('OTA 升级回调:', res);
// 已完成(成功或失败)则忽略后续回调
if (this._otaCompleted) return;
if (res && res.progress !== undefined) {
this.setData({
otaProgress: res.progress,
otaMessage: `升级进度: ${res.progress}%`
});
}
if (res && res.isFailed === false) {
this._clearOtaTimer();
this.handleOtaSuccess();
} else if (res && res.isFailed === true) {
this._clearOtaTimer();
this.handleOtaFailed();
}
// // 已完成(成功或失败)则忽略后续回调
// if (this._otaCompleted) return;
//
// // SDK 返回数字时:1 表示指令已下发、设备开始通过 WiFi 升级并重启
// if (typeof res === 'number') {
// if (res === 1) {
// this._clearOtaTimer();
// this.handleOtaSuccess();
// } else {
// this._clearOtaTimer();
// this.handleOtaFailed(`OTA 指令失败,错误码:${res}`);
// }
// return;
// }
//
// // SDK 返回对象时:{ progress, isFailed }
// if (res && res.progress !== undefined) {
// this.setData({
// otaProgress: res.progress,
// otaMessage: `升级进度: ${res.progress}%`
// });
// }
//
// if (res && res.isFailed === false) {
// this._clearOtaTimer();
// this.handleOtaSuccess();
// } else if (res && res.isFailed === true) {
// this._clearOtaTimer();
// this.handleOtaFailed();
// }
});
},
@@ -93,7 +104,7 @@ Component({
this.setData({
otaProgress: 100,
otaStatus: 'success',
otaMessage: '升级成功,设备即将重启'
otaMessage: '升级指令已下发,设备正在重启...'
});
wx.showToast({ title: 'OTA 升级成功', icon: 'success', duration: 2000 });
setTimeout(() => {
+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);
},
// 选择了某个设备
+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]);
}
});
})
},