[AI Generated]: fix(*): 修复蓝牙连接全链路问题,包含 bus 重复订阅、activeProtocol 空指针、stop 竞速、重连计数器跨页面共享、OTA 超时保护、硬超时改事件驱动、保活定时器泄漏及回调错误处理

This commit is contained in:
17792275749
2026-04-08 11:09:30 +08:00
parent c86f3c3d5c
commit a38db163bd
6 changed files with 219 additions and 97 deletions
+58 -12
View File
@@ -16,22 +16,46 @@ Page({
// 一次性数据初始化,订阅逻辑在 onShow 中注册
},
// 存储页面级 bus 回调引用,用于精准 unsubscribe
_pageHandlers: {
devicesModel: null,
deviceConnect: null,
devicesList: null,
},
// 注销页面级 bus 订阅
_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; }
},
// 记录 stop() 调用时间,用于 onShow 动态计算安全延迟
_stopTime: 0,
onShow() {
console.log("configureDevice onShow");
// 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
// 动态计算距上次 stop() 已过去的时间,确保间隔至少 500ms 再重新注册
const elapsed = Date.now() - (this._stopTime || 0);
const delay = Math.max(0, 500 - elapsed);
setTimeout(() => {
// 重新注册 app 级别的全局 bus 订阅
// 重新注册 app 级别的全局 bus 订阅(内部已做 unsubscribe 防重)
app.registerBusListeners();
// 页面级 bus 订阅
ppScale.plugin.bus.subscribe("devicesModel", (res) => {
// 页面级 bus 订阅(先清理旧的,防止重复叠加)
this._unsubscribePageBus();
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);
ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
this._pageHandlers.deviceConnect = (res) => {
console.log('===》deviceConnect', res);
ppScale.plugin.ScaleAction.startDataProgress(true);
@@ -78,18 +102,20 @@ Page({
this._tryProgress();
})
});
});
};
ppScale.plugin.bus.subscribe("deviceConnect", this._pageHandlers.deviceConnect);
// 如果之前已选择过设备,自动扫描并重连
if (this.data.device) {
wx.showLoading({ title: "正在重新连接...", mask: true });
ppScale.plugin.bus.subscribe("devicesList", (res) => {
this._pageHandlers.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)
@@ -153,6 +179,7 @@ Page({
},
setConfigSuccessful() {
this._stopKeepAlive();
ppScale.plugin.Blue.stop();
wx.switchTab({
url: "/pages/home/home"
@@ -160,9 +187,22 @@ Page({
},
setProgress() {
this.setData({
["progress.index"]: this.data.progress.index + 1
})
const newIndex = this.data.progress.index + 1;
this.setData({ "progress.index": newIndex });
// 进入步骤2(配网)时,等待组件渲染完成后由父页面显式触发 WiFi 列表获取
if (newIndex === 2) {
setTimeout(() => {
const comp = this.selectComponent('#configureDevice3');
if (comp) comp.initWifi();
}, 100);
}
// 进入步骤3(OTA升级)时,等待组件渲染完成后由父页面显式触发升级
if (newIndex === 3) {
setTimeout(() => {
const comp = this.selectComponent('#configureDevice4');
if (comp) comp.startOtaUpgrade();
}, 100);
}
},
// 连接完成 + mac 获取完成,双条件满足后才进入下一步
@@ -255,6 +295,8 @@ Page({
onHide() {
console.log("configureDevice onHide");
this._stopKeepAlive();
this._unsubscribePageBus();
app.unregisterBusListeners();
// 配网步骤:立即清空 WiFi 列表(stop 后数据已失效)
if (this.data.progress.index === 2) {
const comp = this.selectComponent('#configureDevice3');
@@ -271,16 +313,20 @@ Page({
app.setGlobalData('ppScale.device.connectState', null);
this._connectReady = false;
this._macReady = false;
this._stopTime = Date.now();
ppScale.plugin.Blue.stop();
},
onUnload() {
console.log("configureDevice onUnload");
this._stopKeepAlive();
this._unsubscribePageBus();
app.unregisterBusListeners();
app.resetReconnectCount();
app.setGlobalData('ppScale.device.connectState', null);
this._connectReady = false;
this._macReady = false;
this._stopTime = Date.now();
ppScale.plugin.Blue.stop();
}
})