[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
+98 -54
View File
@@ -6,7 +6,7 @@ App({
globalData: {
// wx.request 请求的服务地址
// wxRequestUrl: "https://device.shuziweidao.com/gateway/",
wxRequestUrl: "http://192.168.1.209:8889/",
wxRequestUrl: "http://192.168.1.207:8889/",
// 秤的所有信息
ppScale: {
@@ -14,8 +14,8 @@ App({
plugin: null,
activeProtocol: null,
domain1: "http://device.shuziweidao.com:80/gateway", // 老版本 没有鉴权
domain2: "http://device.shuziweidao.com:80/weight", // 新版本 有鉴权
domain1: "http://192.168.1.207:8889", // 老版本 没有鉴权
domain2: "http://192.168.1.207:8889/weight", // 新版本 有鉴权
wifi: {
ssid: "",
password: ""
@@ -136,75 +136,118 @@ App({
mac: null, // 选中的设备mac地址/SN码
connection: null, // 默认选中连接的设备
connectState: null, // 当前连接设备的状态
},
reconnect: {
count: 0, // 当前已重连次数
max: 3, // 最大重连次数
timer: null // 重连定时器
}
},
_callbacks: {} // 用于存储所有 globalData 属性的监听回调
},
_reconnectCount: 0, // 自动重连已尝试次数
_reconnectMax: 3, // 最大自动重连次数
_reconnectTimer: null, // 重连定时器
_reconnectCount: 0, // 已废弃,保留兼容,实际使用 globalData.ppScale.reconnect.count
_reconnectMax: 3, // 已废弃,保留兼容,实际使用 globalData.ppScale.reconnect.max
_reconnectTimer: null, // 已废弃,保留兼容,实际使用 globalData.ppScale.reconnect.timer
initPpScale() {
this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin');
},
/**
* 注册全局 bus 事件监听(connectState / syncDeviceTimeSuccess / deviceWillDisconnect
* stop() 会销毁所有订阅,页面在 onShow 中需重新调用此方法
*/
registerBusListeners() {
const plugin = this.globalData.ppScale.plugin;
// 蓝牙连接状态监听
plugin.bus.subscribe("connectState", (res) => {
console.log("app.js ===> connectState", res);
this.setGlobalData('ppScale.device.connectState', res);
// 连接成功,重置重连计数
if (res == plugin.BLUE_STATE.CONNECTSUCCESS) {
this._reconnectCount = 0;
}
// 连接失败,自动重连(最多重试3次)
if (res == plugin.BLUE_STATE.CONNECTFAILED) {
this.reconnectDevice();
}
wx.hideLoading();
});
// 同步时间
plugin.bus.subscribe("syncDeviceTimeSuccess", (res) => {
console.log("app.js ===> syncDeviceTimeSuccess", res);
});
// 设备自动断开监听
plugin.bus.subscribe("deviceWillDisconnect", (res) => {
console.log("app.js ===> deviceWillDisconnect", res);
this.setGlobalData('ppScale.device.connectState', plugin.BLUE_STATE.CONNECTFAILED);
});
// 存储 app 级 bus 回调引用,用于精准 unsubscribe
_busCallbacks: {
connectState: null,
syncDeviceTimeSuccess: null,
deviceWillDisconnect: null,
},
/**
* 自动重连设备,最多重试 _reconnectMax 次,超过后停止,由用户手动重连
* 流程:先断开旧连接,再重新扫描设备,由页面级 devicesList 订阅自动接管连接
* 注销全局 bus 事件监听,防止重复订阅叠加
*/
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;
}
},
/**
* 注册全局 bus 事件监听(connectState / syncDeviceTimeSuccess / deviceWillDisconnect
* 调用前先 unsubscribe 旧的,防止多次 onShow 导致回调叠加
*/
registerBusListeners() {
this.unregisterBusListeners();
const plugin = this.globalData.ppScale.plugin;
// 蓝牙连接状态监听
this._busCallbacks.connectState = (res) => {
console.log("app.js ===> connectState", res);
this.setGlobalData('ppScale.device.connectState', res);
if (res == plugin.BLUE_STATE.CONNECTSUCCESS) {
this.globalData.ppScale.reconnect.count = 0;
}
if (res == plugin.BLUE_STATE.CONNECTFAILED) {
this.reconnectDevice();
}
wx.hideLoading();
};
// 同步时间
this._busCallbacks.syncDeviceTimeSuccess = (res) => {
console.log("app.js ===> syncDeviceTimeSuccess", res);
};
// 设备自动断开监听
this._busCallbacks.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);
},
/**
* 自动重连设备,最多重试 max 次,超过后停止,由用户手动重连
* 只在配置页(configureDevice)活跃时执行,防止跨页面干扰
*/
reconnectDevice() {
if (this._reconnectCount >= this._reconnectMax) {
const reconnect = this.globalData.ppScale.reconnect;
// 仅在配置页活跃时执行重连
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
if (!currentPage || !currentPage.route || !currentPage.route.includes('configureDevice')) {
console.log("app.js ===> 非配置页,跳过自动重连");
return;
}
if (reconnect.count >= reconnect.max) {
console.log("app.js ===> 自动重连已达上限,等待用户手动重试");
return;
}
this._reconnectCount++;
console.log(`app.js ===> 自动重连第 ${this._reconnectCount}/${this._reconnectMax}`);
reconnect.count++;
console.log(`app.js ===> 自动重连第 ${reconnect.count}/${reconnect.max}`);
// 延迟 2 秒后重连,避免连续冲突
if (this._reconnectTimer) {
clearTimeout(this._reconnectTimer);
if (reconnect.timer) {
clearTimeout(reconnect.timer);
}
this._reconnectTimer = setTimeout(() => {
reconnect.timer = setTimeout(() => {
this.globalData.ppScale.plugin.Blue.disconnect((disres) => {
if (disres.errCode == 0) {
console.log("app.js ===> 断开成功,开始重新扫描设备");
@@ -217,13 +260,14 @@ App({
},
/**
* 重置重连计数,用于用户手动点击重试时调用
* 重置重连计数,用于用户手动点击重试或页面离开时调用
*/
resetReconnectCount() {
this._reconnectCount = 0;
if (this._reconnectTimer) {
clearTimeout(this._reconnectTimer);
this._reconnectTimer = null;
const reconnect = this.globalData.ppScale.reconnect;
reconnect.count = 0;
if (reconnect.timer) {
clearTimeout(reconnect.timer);
reconnect.timer = null;
}
},