[AI Generated]: refactor(*): 重构蓝牙生命周期管理,修复最小化后重连与进度跳步问题
- app.js: 提取 registerBusListeners 方法,新增自动重连机制(3次上限) - configureDevice: bus 订阅迁移至 onShow,deviceConnect 按步骤区分处理逻辑 - configureDevice_2: setUserInfo 兼容 WIFISUCCESS 状态,WXML 覆盖六态 - configureDevice_3: 新增 reInitWifi 方法,最小化后重置 WiFi 列表 - replaceNetwork: 订阅迁移至 onShow,修复 selectComponent 空指针 - replaceNetwork_3: 修复 domain1 配网参数引用错误 - userInfo: 订阅迁移至 onShow,setUserInfo 兼容 WIFISUCCESS - searchDevice: 移除 onLoad 重复初始化,统一由 onShow 处理 - 所有 BLE 页面统一 onHide/onUnload 清理:resetReconnectCount + setGlobalData(null) + stop() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
6924a1ff5b
commit
81f9301c64
@@ -141,44 +141,91 @@ App({
|
||||
|
||||
_callbacks: {} // 用于存储所有 globalData 属性的监听回调
|
||||
},
|
||||
|
||||
|
||||
_reconnectCount: 0, // 自动重连已尝试次数
|
||||
_reconnectMax: 3, // 最大自动重连次数
|
||||
_reconnectTimer: null, // 重连定时器
|
||||
|
||||
initPpScale() {
|
||||
this.globalData.ppScale.plugin = requirePlugin('ppScale-plugin');
|
||||
|
||||
// 蓝牙连接状态监听
|
||||
this.globalData.ppScale.plugin.bus.subscribe("connectState", (res) => {
|
||||
console.log("app.js ===> connectState", res);
|
||||
this.setGlobalData('ppScale.device.connectState', res);
|
||||
|
||||
// 连接失败则重新连接
|
||||
if (res == this.globalData.ppScale.plugin.BLUE_STATE.CONNECTFAILED) {
|
||||
this.reconnectDevice()
|
||||
}
|
||||
wx.hideLoading();
|
||||
});
|
||||
|
||||
// 同步时间
|
||||
this.globalData.ppScale.plugin.bus.subscribe("syncDeviceTimeSuccess", (res) => {
|
||||
console.log("app.js ===> syncDeviceTimeSuccess", res);
|
||||
})
|
||||
|
||||
// 设备自动断开监听
|
||||
this.globalData.ppScale.plugin.bus.subscribe("deviceWillDisconnect", (res) => {
|
||||
console.log("app.js ===> deviceWillDisconnect", res);
|
||||
|
||||
this.reconnectDevice();
|
||||
})
|
||||
},
|
||||
|
||||
reconnectDevice() {
|
||||
this.globalData.ppScale.plugin.Blue.disconnect((disres) => {
|
||||
if (disres.errCode == 0) {
|
||||
this.globalData.ppScale.plugin.Blue.startBluetoothDevicesDiscovery();
|
||||
} else {
|
||||
/**
|
||||
* 注册全局 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);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 自动重连设备,最多重试 _reconnectMax 次,超过后停止,由用户手动重连
|
||||
* 流程:先断开旧连接,再重新扫描设备,由页面级 devicesList 订阅自动接管连接
|
||||
*/
|
||||
reconnectDevice() {
|
||||
if (this._reconnectCount >= this._reconnectMax) {
|
||||
console.log("app.js ===> 自动重连已达上限,等待用户手动重试");
|
||||
return;
|
||||
}
|
||||
|
||||
this._reconnectCount++;
|
||||
console.log(`app.js ===> 自动重连第 ${this._reconnectCount}/${this._reconnectMax} 次`);
|
||||
|
||||
// 延迟 2 秒后重连,避免连续冲突
|
||||
if (this._reconnectTimer) {
|
||||
clearTimeout(this._reconnectTimer);
|
||||
}
|
||||
this._reconnectTimer = setTimeout(() => {
|
||||
this.globalData.ppScale.plugin.Blue.disconnect((disres) => {
|
||||
if (disres.errCode == 0) {
|
||||
console.log("app.js ===> 断开成功,开始重新扫描设备");
|
||||
this.globalData.ppScale.plugin.Blue.startBluetoothDevicesDiscovery();
|
||||
} else {
|
||||
console.log("app.js ===> 断开失败,errCode:", disres.errCode);
|
||||
}
|
||||
});
|
||||
}, 2000);
|
||||
},
|
||||
|
||||
/**
|
||||
* 重置重连计数,用于用户手动点击重试时调用
|
||||
*/
|
||||
resetReconnectCount() {
|
||||
this._reconnectCount = 0;
|
||||
if (this._reconnectTimer) {
|
||||
clearTimeout(this._reconnectTimer);
|
||||
this._reconnectTimer = null;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 注册 globalData 某个属性的监听
|
||||
|
||||
Reference in New Issue
Block a user