[AI Generated]: fix(*): 修复 SDK bus 不支持 unsubscribe 导致的崩溃,改用标志位防重复订阅;修复 OTA 回调注释逻辑
This commit is contained in:
@@ -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);
|
||||
},
|
||||
|
||||
// 选择了某个设备
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
});
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user