diff --git a/app.js b/app.js
index ceb6977..d382745 100644
--- a/app.js
+++ b/app.js
@@ -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 某个属性的监听
diff --git a/components/configureDevice_2/configureDevice_2.js b/components/configureDevice_2/configureDevice_2.js
index 34676ca..e2468f5 100644
--- a/components/configureDevice_2/configureDevice_2.js
+++ b/components/configureDevice_2/configureDevice_2.js
@@ -88,8 +88,9 @@ Component({
// 组件的方法
methods: {
- // 二次连接设备
+ // 手动重连设备
connectedDevice() {
+ app.resetReconnectCount();
this.triggerEvent('connectedDeviceEvent', {
device: this.data.device
});
@@ -154,7 +155,7 @@ Component({
// console.log("deviceInfo === codeClearDeviceData", res);
// })
- if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS) {
+ if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS || this.data.connectState == this.data.BLUE_STATE.WIFISUCCESS) {
if(this.data.userId) {
app.globalData.ppScale.activeProtocol.dataSyncUserInfo({
userID: this.data.userId,
diff --git a/components/configureDevice_2/configureDevice_2.wxml b/components/configureDevice_2/configureDevice_2.wxml
index a949cbb..5cfb5d8 100644
--- a/components/configureDevice_2/configureDevice_2.wxml
+++ b/components/configureDevice_2/configureDevice_2.wxml
@@ -5,14 +5,30 @@
型号:{{device.name}}
-
+
连接成功
-
+
+
+ 正在搜索...
+
+
+
+ 蓝牙就绪
+
+
+
+ 蓝牙不可用
+
+
连接失败,点击重试
+
+
+ 未连接
+
diff --git a/components/configureDevice_3/configureDevice_3.js b/components/configureDevice_3/configureDevice_3.js
index aa7d5d9..a2a82dc 100644
--- a/components/configureDevice_3/configureDevice_3.js
+++ b/components/configureDevice_3/configureDevice_3.js
@@ -37,6 +37,17 @@ Component({
})
},
+ // 最小化后重连,重置所有状态并重新获取 WiFi 列表
+ reInitWifi() {
+ this.setData({
+ progress: 0,
+ wifiList: [],
+ selectWifi: null,
+ selectWifiPWD: ""
+ });
+ this.initWifi();
+ },
+
selectDevice(e) {
this.setData({
progress: 1,
diff --git a/components/replaceNetwork_3/replaceNetwork_3.js b/components/replaceNetwork_3/replaceNetwork_3.js
index 52b8979..3a75349 100644
--- a/components/replaceNetwork_3/replaceNetwork_3.js
+++ b/components/replaceNetwork_3/replaceNetwork_3.js
@@ -36,11 +36,11 @@ Component({
if (version === 'domain1') {
app.globalData.ppScale.activeProtocol.dataConfigNetWork({
domain: app.globalData.ppScale.domain1,
- ssid: this.data.selectWifi.ssid,
- password: this.data.selectWifiPWD
+ ssid: ssid,
+ password: password
}, (res) => {
console.log("setNetwork.js dataConfigNetWork 1", res);
-
+
this.netWorkCallBack(res);
})
}
@@ -53,7 +53,7 @@ Component({
userPassword: "3acebb95eb49577e9c2a2082589b9bd6"
}, (res) => {
console.log("setNetwork.js dataConfigUserNetWork 2", res);
-
+
this.netWorkCallBack(res);
})
}
@@ -61,14 +61,14 @@ Component({
netWorkCallBack(res) {
if(res === 23) {
- app.globalData.ppScale.wifi.ssid = ssid;
- app.globalData.ppScale.wifi.password = password;
+ app.globalData.ppScale.wifi.ssid = this.properties.ssid;
+ app.globalData.ppScale.wifi.password = this.properties.password;
let scaleDeviceId = app.globalData.ppScale.device.mac.replace(/:/g, '');
if(scaleDeviceId) {
let params = {
equipmentCode: scaleDeviceId,
- wifiName: ssid
+ wifiName: this.properties.ssid
};
$.ajax("weighingScale/edit/device", params, "POST").then((res) => {
this.triggerEvent('wifiEvent', {
diff --git a/pages/configureDevice/configureDevice.js b/pages/configureDevice/configureDevice.js
index e0e8213..a491707 100644
--- a/pages/configureDevice/configureDevice.js
+++ b/pages/configureDevice/configureDevice.js
@@ -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();
}
})
\ No newline at end of file
diff --git a/pages/configureDevice/configureDevice.wxml b/pages/configureDevice/configureDevice.wxml
index 830b596..8add779 100644
--- a/pages/configureDevice/configureDevice.wxml
+++ b/pages/configureDevice/configureDevice.wxml
@@ -18,7 +18,7 @@
-
+
diff --git a/pages/replaceNetwork/replaceNetwork.js b/pages/replaceNetwork/replaceNetwork.js
index ee3d59e..d2d13ae 100644
--- a/pages/replaceNetwork/replaceNetwork.js
+++ b/pages/replaceNetwork/replaceNetwork.js
@@ -19,20 +19,44 @@ Page({
_connectStateWatcher: null,
onLoad(options) {
+ // 一次性数据初始化
this.checkBluetoothPermissionAndInit();
- app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
- console.log("searchDevice ===》devicesModel", res);
- app.globalData.ppScale.device.mac = res.deviceMac;
+ console.log(`[replaceNetwork] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
+ this.setData({
+ BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
+ connectState: app.globalData.ppScale.device.connectState
+ })
+ this._connectStateWatcher = (newValue, oldValue) => {
+ console.log(`[replaceNetwork] connectState 变化: ${oldValue} -> ${newValue}`);
+ this.setData({
+ connectState: newValue || ''
+ });
+ };
+ app.watch('ppScale.device.connectState', this._connectStateWatcher);
+ },
+
+ onShow() {
+ console.log("replaceNetwork onShow");
+ // 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
+ setTimeout(() => {
+ // 重新注册 app 级别的全局 bus 订阅
+ app.registerBusListeners();
+
+ // 页面级 bus 订阅
+ 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('connectedDevice ===》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("connectedDevice ===》codeUpdateMTU", res);
+ console.log("replaceNetwork ===》codeUpdateMTU", res);
app.globalData.ppScale.device.name = this.data.device.name;
app.globalData.ppScale.device.connection = this.data.device;
@@ -40,23 +64,19 @@ Page({
app.globalData.ppScale.version = nameArr.length === 5 ? "domain2" : "domain1";
wx.hideLoading();
- this.selectComponent('#replaceNetwork1Component').getWiFiList();
+ // 仅在 WiFi 列表步骤时获取列表(组件只在 progress === 0 时渲染)
+ if (this.data.progress === 0) {
+ const comp = this.selectComponent('#replaceNetwork1Component');
+ if (comp) {
+ comp.getWiFiList();
+ }
+ }
});
});
- });
- console.log(`[Component] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
- this.setData({
- BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
- connectState: app.globalData.ppScale.device.connectState
- })
- this._connectStateWatcher = (newValue, oldValue) => {
- console.log(`[Component] connectState 变化: ${oldValue} -> ${newValue}`);
- this.setData({
- connectState: newValue || ''
- });
- };
- app.watch('ppScale.device.connectState', this._connectStateWatcher);
+ // 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
+ this.checkBluetoothPermissionAndInit();
+ }, 300);
},
checkBluetoothPermissionAndInit() {
@@ -201,11 +221,22 @@ Page({
})
},
- // 点击设备重连
+ // 手动重连设备
connectedDevice() {
+ app.resetReconnectCount();
let device = this.data.device;
if(device) {
- ppScale.plugin.Blue.createBLEConnection(device);
+ wx.showLoading({
+ title: "正在重新连接设备...",
+ mask: true
+ });
+ app.globalData.ppScale.plugin.Blue.disconnect((disres) => {
+ if (disres.errCode == 0) {
+ app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
+ } else {
+ app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
+ }
+ });
}
},
@@ -243,9 +274,19 @@ Page({
})
}
},
-
+
+ onHide() {
+ console.log("replaceNetwork onHide");
+ app.resetReconnectCount();
+ app.setGlobalData('ppScale.device.connectState', null);
+ app.globalData.ppScale.plugin.Blue.stop();
+ },
+
// 卸载事件监听
onUnload() {
+ console.log("replaceNetwork onUnload");
+ app.resetReconnectCount();
+ app.setGlobalData('ppScale.device.connectState', null);
app.globalData.ppScale.plugin.Blue.stop();
if (this._connectStateWatcher) {
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);
diff --git a/pages/replaceNetwork/replaceNetwork.wxml b/pages/replaceNetwork/replaceNetwork.wxml
index dd610ce..13d1525 100644
--- a/pages/replaceNetwork/replaceNetwork.wxml
+++ b/pages/replaceNetwork/replaceNetwork.wxml
@@ -5,14 +5,30 @@
型号:{{device.name}}
-
+
连接成功
-
+
+
+ 正在搜索...
+
+
+
+ 蓝牙就绪
+
+
+
+ 蓝牙不可用
+
+
连接失败,点击重试
+
+
+ 未连接
+
diff --git a/pages/searchDevice/searchDevice.js b/pages/searchDevice/searchDevice.js
index b6e7d0e..178e00f 100644
--- a/pages/searchDevice/searchDevice.js
+++ b/pages/searchDevice/searchDevice.js
@@ -9,7 +9,7 @@ Page({
},
onLoad(options) {
- this.checkBluetoothPermissionAndInit();
+ // 一次性数据初始化(蓝牙初始化在 onShow 中统一处理)
},
onShow() {
@@ -243,8 +243,8 @@ Page({
onUnload() {
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
- this.setData({
- isShowingModal: false
+ this.setData({
+ isShowingModal: false
});
}
})
\ No newline at end of file
diff --git a/pages/userInfo/userInfo.js b/pages/userInfo/userInfo.js
index 1b42953..e0599f2 100644
--- a/pages/userInfo/userInfo.js
+++ b/pages/userInfo/userInfo.js
@@ -37,37 +37,16 @@ Page({
_connectStateWatcher: null,
onLoad(options) {
+ // 一次性数据初始化
this.checkBluetoothPermissionAndInit();
-
- app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
- console.log("searchDevice ===》devicesModel", res);
- app.globalData.ppScale.device.mac = res.deviceMac;
- app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
- console.log('connectedDevice ===》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("connectedDevice ===》codeUpdateMTU", res);
-
-
- app.globalData.ppScale.device.name = this.data.device.name;
- app.globalData.ppScale.device.connection = this.data.device;
-
- wx.hideLoading();
- });
- });
- });
-
- console.log(`[Component] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
+ console.log(`[userInfo] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
this.setData({
BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
connectState: app.globalData.ppScale.device.connectState
})
this._connectStateWatcher = (newValue, oldValue) => {
- console.log(`[Component] connectState 变化: ${oldValue} -> ${newValue}`);
+ console.log(`[userInfo] connectState 变化: ${oldValue} -> ${newValue}`);
this.setData({
connectState: newValue || ''
});
@@ -77,7 +56,7 @@ Page({
let userInfo = wx.getStorageSync("userInfo") || null;
if(userInfo) {
let [year, month, day] = userInfo.birthday.split("-");
-
+
this.setData({
userId: userInfo.userId,
realname: userInfo.realname,
@@ -101,6 +80,40 @@ Page({
}
},
+ onShow() {
+ console.log("userInfo onShow");
+ // 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
+ setTimeout(() => {
+ // 重新注册 app 级别的全局 bus 订阅
+ app.registerBusListeners();
+
+ // 页面级 bus 订阅
+ app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
+ console.log("userInfo ===》devicesModel", res);
+ app.globalData.ppScale.device.mac = res.deviceMac;
+ });
+
+ app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
+ console.log('userInfo ===》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("userInfo ===》codeUpdateMTU", res);
+
+ app.globalData.ppScale.device.name = this.data.device.name;
+ app.globalData.ppScale.device.connection = this.data.device;
+
+ wx.hideLoading();
+ });
+ });
+
+ // 通过完整链路重新初始化蓝牙(权限检查 → 打开蓝牙 → 获取设备 → start)
+ this.checkBluetoothPermissionAndInit();
+ }, 300);
+ },
+
// 姓名输入
realnameInput(e) {
let realname = e.detail.value.replace(/\s+/g, '');
@@ -156,7 +169,7 @@ Page({
},
setUserInfo() {
- if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS) {
+ if(this.data.connectState == this.data.BLUE_STATE.CONNECTSUCCESS || this.data.connectState == this.data.BLUE_STATE.WIFISUCCESS) {
let realname = this.data.realname;
let sex = this.data.sex;
let birthday = this.data.birthday;
@@ -414,19 +427,36 @@ Page({
})
},
- // 点击设备重连
+ // 手动重连设备
connectedDevice() {
+ app.resetReconnectCount();
let device = this.data.device;
if(device) {
- wx.showLoading({
+ wx.showLoading({
title: "正在重新连接设备...",
mask: true
});
- app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
+ app.globalData.ppScale.plugin.Blue.disconnect((disres) => {
+ if (disres.errCode == 0) {
+ app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
+ } else {
+ app.globalData.ppScale.plugin.Blue.createBLEConnection(device);
+ }
+ });
}
},
+ onHide() {
+ console.log("userInfo onHide");
+ app.resetReconnectCount();
+ app.setGlobalData('ppScale.device.connectState', null);
+ app.globalData.ppScale.plugin.Blue.stop();
+ },
+
onUnload() {
+ console.log("userInfo onUnload");
+ app.resetReconnectCount();
+ app.setGlobalData('ppScale.device.connectState', null);
app.globalData.ppScale.plugin.Blue.stop();
if (this._connectStateWatcher) {
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);
diff --git a/pages/userInfo/userInfo.wxml b/pages/userInfo/userInfo.wxml
index 5081f5a..cc062d7 100644
--- a/pages/userInfo/userInfo.wxml
+++ b/pages/userInfo/userInfo.wxml
@@ -5,14 +5,30 @@
型号:{{device.name}}
-
+
连接成功
-
+
+
+ 正在搜索...
+
+
+
+ 蓝牙就绪
+
+
+
+ 蓝牙不可用
+
+
连接失败,点击重试
+
+
+ 未连接
+