diff --git a/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs b/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs index 614860c..2837542 100644 --- a/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs +++ b/Assets/Scripts/UI/Pages/HomePage/HomePageCtrl.cs @@ -41,6 +41,7 @@ namespace Kill.UI.Pages bool hasWifi = false; bool isPollingOnline = false; bool stopPolling = false; + bool isAppForeground = true; // 是否处于前台:仅前台进行在线状态轮询 // UI 组件引用 public HomePageDeviceState deviceState; @@ -117,8 +118,13 @@ namespace Kill.UI.Pages } void OnApplicationPause(bool pause) { + isAppForeground = !pause; LockButtonPlane.SetActive(true); } + void OnApplicationFocus(bool hasFocus) + { + isAppForeground = hasFocus; + } void OnDestroy() { StopDeviceOnlineStatusPolling(); @@ -2021,8 +2027,15 @@ namespace Kill.UI.Pages while (!stopPolling && selectedDevice != null) { - // 每 5s 检测一次设备 WiFi 在线状态 - await System.Threading.Tasks.Task.Delay(5000); + // 每 3s 检测一次设备 WiFi 在线状态 + await System.Threading.Tasks.Task.Delay(3000); + + if (stopPolling || selectedDevice == null) + break; + + // 仅前台进行轮询:后台时挂起等待,切回前台后立即执行一次 + while (!stopPolling && !isAppForeground) + await System.Threading.Tasks.Task.Delay(500); if (stopPolling || selectedDevice == null) break; @@ -2084,6 +2097,23 @@ namespace Kill.UI.Pages deviceState?.UpdateDeviceState(hasBluetooth, hasWifi); }); } + + // 在线状态处理后拉取一次设备配置,与本地快照比对,有变化则刷新主页UI + DeviceConfig configBefore = DataManager.Instance.deviceConfig?.Clone(); + await DataManager.Instance.GetDeviceConfig(selectedDevice.ble_mac); + if (stopPolling) break; + + var changedFields = DataManager.Instance.deviceConfig?.GetChangedFields(configBefore); + if (changedFields != null && changedFields.Count > 0) + { + Debug.Log($"[HomePageCtrl] 检测到设备配置变化: {string.Join(",", changedFields.Keys)},刷新主页UI"); + pendingUIUpdates.Add(() => + { + // await 期间蓝牙可能已连接,避免用 WiFi 数据覆盖 BLE 数据 + if (hasBluetooth) return; + InitDeviceUIFromConfig(); + }); + } } isPollingOnline = false;