[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
@@ -1,4 +1,5 @@
|
||||
const ppScale = getApp().globalData.ppScale;
|
||||
const app = getApp();
|
||||
const ppScale = app.globalData.ppScale;
|
||||
import $ from "../../utils/request";
|
||||
|
||||
Page({
|
||||
@@ -13,113 +14,163 @@ Page({
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
ppScale.plugin.bus.subscribe("deviceInfo", (res) => {
|
||||
console.log("===》deviceInfo", res);
|
||||
ppScale.device.mac = res.serialNumber;
|
||||
});
|
||||
// 一次性数据初始化,订阅逻辑在 onShow 中注册
|
||||
},
|
||||
|
||||
ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
||||
console.log('===》deviceConnect', res);
|
||||
onShow() {
|
||||
console.log("configureDevice onShow");
|
||||
// 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
|
||||
setTimeout(() => {
|
||||
// 重新注册 app 级别的全局 bus 订阅
|
||||
app.registerBusListeners();
|
||||
|
||||
ppScale.plugin.ScaleAction.startDataProgress(true);
|
||||
ppScale.activeProtocol = ppScale.plugin.ScaleAction.getActiveProtocol();
|
||||
// 页面级 bus 订阅
|
||||
ppScale.plugin.bus.subscribe("deviceInfo", (res) => {
|
||||
console.log("===》deviceInfo", res);
|
||||
ppScale.device.mac = res.serialNumber;
|
||||
});
|
||||
|
||||
ppScale.activeProtocol.codeUpdateMTU((res) => {
|
||||
console.log("===》codeUpdateMTU", res);
|
||||
ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
||||
console.log('===》deviceConnect', res);
|
||||
|
||||
ppScale.activeProtocol.codeFetchBindingState((res) => {
|
||||
console.log("===》codeFetchBindingState", res);
|
||||
ppScale.plugin.ScaleAction.startDataProgress(true);
|
||||
ppScale.activeProtocol = ppScale.plugin.ScaleAction.getActiveProtocol();
|
||||
|
||||
ppScale.activeProtocol.codeSyncTime((codeSyncTime) => {
|
||||
console.log("===》codeSyncTime", codeSyncTime)
|
||||
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();
|
||||
if (res === 1) {
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: '当前设备已被绑定,你确定要覆盖绑定吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
wx.showLoading({
|
||||
title: "正在初始化设备...",
|
||||
mask: true
|
||||
});
|
||||
ppScale.activeProtocol.codeClearDeviceData("00", (res) => {
|
||||
console.log("deviceInfo === codeClearDeviceData", res);
|
||||
wx.hideLoading();
|
||||
|
||||
if (res === 0) {
|
||||
let scaleDeviceId = ppScale.device.mac.replace(/:/g, '');
|
||||
let params = {
|
||||
equipmentCode: scaleDeviceId,
|
||||
};
|
||||
$.ajax("weighingScale/del/device", params, "POST", true, "正在初始化设备...").then(res => {
|
||||
// 配网步骤:重新获取 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.codeFetchBindingState((res) => {
|
||||
console.log("===》codeFetchBindingState", res);
|
||||
|
||||
ppScale.activeProtocol.codeSyncTime((codeSyncTime) => {
|
||||
console.log("===》codeSyncTime", codeSyncTime)
|
||||
|
||||
wx.hideLoading();
|
||||
if (res === 1) {
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: '当前设备已被绑定,你确定要覆盖绑定吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
wx.showLoading({
|
||||
title: "正在初始化设备...",
|
||||
mask: true
|
||||
});
|
||||
ppScale.activeProtocol.codeClearDeviceData("00", (res) => {
|
||||
console.log("deviceInfo === codeClearDeviceData", res);
|
||||
wx.hideLoading();
|
||||
|
||||
if (res === 0) {
|
||||
let scaleDeviceId = ppScale.device.mac.replace(/:/g, '');
|
||||
let params = {
|
||||
equipmentCode: scaleDeviceId,
|
||||
};
|
||||
$.ajax("weighingScale/del/device", params, "POST", true, "正在初始化设备...").then(res => {
|
||||
ppScale.plugin.Blue.stop();
|
||||
wx.showToast({
|
||||
icon: "none",
|
||||
title: "设备初始化成功,请重新绑定。",
|
||||
})
|
||||
setTimeout(() => {
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
})
|
||||
}, 1500);
|
||||
});
|
||||
} else {
|
||||
ppScale.plugin.Blue.stop();
|
||||
wx.showToast({
|
||||
icon: "none",
|
||||
title: "设备初始化成功,请重新绑定。",
|
||||
title: "设备初始化失败。",
|
||||
})
|
||||
// device.list = [];
|
||||
// device.mac = null;
|
||||
setTimeout(() => {
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
})
|
||||
}, 1500);
|
||||
});
|
||||
} else {
|
||||
ppScale.plugin.Blue.stop();
|
||||
wx.showToast({
|
||||
icon: "none",
|
||||
title: "设备初始化失败。",
|
||||
})
|
||||
}
|
||||
})
|
||||
} else if (res.cancel) {
|
||||
// device.mac = null;
|
||||
ppScale.plugin.Blue.stop();
|
||||
}
|
||||
})
|
||||
} else if (res.cancel) {
|
||||
ppScale.plugin.Blue.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
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";
|
||||
if (this.data.progressNext) {
|
||||
let scaleDeviceId = ppScale.device.mac.replace(/:/g, '');
|
||||
let params = {
|
||||
sn: scaleDeviceId,
|
||||
};
|
||||
$.ajax("weighingScale/getUserInfoBySn", params, "GET", true, "正在同步用户信息...").then(res => {
|
||||
if (res.result) {
|
||||
wx.setStorageSync("userInfo", res.result);
|
||||
this.setProgress();
|
||||
} else {
|
||||
wx.removeStorageSync("userInfo");
|
||||
})
|
||||
} else {
|
||||
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";
|
||||
if (this.data.progressNext) {
|
||||
// 立即重置,防止最小化后重连时重复推进进度
|
||||
this.setData({ progressNext: false });
|
||||
let scaleDeviceId = ppScale.device.mac.replace(/:/g, '');
|
||||
let params = {
|
||||
sn: scaleDeviceId,
|
||||
};
|
||||
$.ajax("weighingScale/getUserInfoBySn", params, "GET", true, "正在同步用户信息...").then(res => {
|
||||
if (res.result) {
|
||||
wx.setStorageSync("userInfo", res.result);
|
||||
this.setProgress();
|
||||
} else {
|
||||
wx.removeStorageSync("userInfo");
|
||||
wx.showToast({
|
||||
title: "暂无用户信息,请手动补充",
|
||||
icon: "none"
|
||||
})
|
||||
setTimeout(() => {
|
||||
this.setProgress();
|
||||
}, 1500)
|
||||
}
|
||||
}).catch(() => {
|
||||
wx.showToast({
|
||||
title: "暂无用户信息,请手动补充",
|
||||
title: "用户信息同步失败,请手动补充",
|
||||
icon: "none"
|
||||
})
|
||||
setTimeout(() => {
|
||||
this.setProgress();
|
||||
}, 1500)
|
||||
}
|
||||
}).catch(() => {
|
||||
wx.showToast({
|
||||
title: "用户信息同步失败,请手动补充",
|
||||
icon: "none"
|
||||
})
|
||||
setTimeout(() => {
|
||||
this.setProgress();
|
||||
}, 1500)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 如果之前已选择过设备,自动扫描并重连
|
||||
if (this.data.device) {
|
||||
wx.showLoading({ title: "正在重新连接...", mask: true });
|
||||
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]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 通过完整链路重新初始化蓝牙(权限检查 → 打开适配器 → start)
|
||||
this.checkBluetoothPermissionAndInit();
|
||||
}, 300);
|
||||
},
|
||||
|
||||
// 选择了某个设备
|
||||
@@ -139,7 +190,7 @@ Page({
|
||||
},
|
||||
|
||||
connectedDevice(e) {
|
||||
ppScale.plugin.Blue.stop();
|
||||
app.resetReconnectCount();
|
||||
wx.showLoading({
|
||||
title: "正在尝试连接...",
|
||||
mask: true
|
||||
@@ -148,7 +199,10 @@ Page({
|
||||
progressNext: false
|
||||
})
|
||||
let device = e.detail.device;
|
||||
this.connectedDevice_(device);
|
||||
// 先断开旧连接再重新连接
|
||||
ppScale.plugin.Blue.disconnect((disres) => {
|
||||
this.connectedDevice_(device);
|
||||
});
|
||||
},
|
||||
|
||||
connectedDevice_(device) {
|
||||
@@ -161,9 +215,6 @@ Page({
|
||||
},
|
||||
|
||||
setDeviceUserInfo(e) {
|
||||
this.setData({
|
||||
progressNext: true
|
||||
})
|
||||
let status = e.detail.status;
|
||||
if (status) {
|
||||
this.setProgress();
|
||||
@@ -190,7 +241,73 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
checkBluetoothPermissionAndInit() {
|
||||
wx.getSetting({
|
||||
success: (res) => {
|
||||
if (res.authSetting['scope.bluetooth']) {
|
||||
this.openBluetoothAdapter();
|
||||
} else {
|
||||
wx.authorize({
|
||||
scope: 'scope.bluetooth',
|
||||
success: () => {
|
||||
this.openBluetoothAdapter();
|
||||
},
|
||||
fail: () => {
|
||||
wx.hideLoading();
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: '蓝牙权限被拒绝,无法连接设备。是否前往设置开启?',
|
||||
confirmText: '去开启',
|
||||
cancelText: '不开启',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
wx.openSetting();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: () => {
|
||||
wx.hideLoading();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
openBluetoothAdapter() {
|
||||
wx.openBluetoothAdapter({
|
||||
success: () => {
|
||||
// 蓝牙适配器打开成功,设置设备配置后启动扫描
|
||||
let setting = ppScale.device.setting;
|
||||
ppScale.plugin.Blue.setDeviceSetting(setting);
|
||||
let deviceNames = setting.map(item => item.deviceName);
|
||||
ppScale.plugin.Blue.start(deviceNames, false);
|
||||
},
|
||||
fail: (err) => {
|
||||
wx.hideLoading();
|
||||
if (err.errCode === 10001) {
|
||||
wx.showModal({
|
||||
title: "提示",
|
||||
content: "请确保手机蓝牙已开启。",
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
onHide() {
|
||||
console.log("configureDevice onHide");
|
||||
app.resetReconnectCount();
|
||||
app.setGlobalData('ppScale.device.connectState', null);
|
||||
ppScale.plugin.Blue.stop();
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
console.log("configureDevice onUnload");
|
||||
app.resetReconnectCount();
|
||||
app.setGlobalData('ppScale.device.connectState', null);
|
||||
ppScale.plugin.Blue.stop();
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user