diff --git a/lib/modules/home/controllers/home_controller.dart b/lib/modules/home/controllers/home_controller.dart index d47400f2d..aa4f7f57c 100644 --- a/lib/modules/home/controllers/home_controller.dart +++ b/lib/modules/home/controllers/home_controller.dart @@ -1,7 +1,9 @@ +import 'dart:async'; + +import 'package:device_jieli/device_jieli.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; import 'package:agent_service/agent_service.dart'; -// 移除了dart:async导入,因为不再使用Timer import '../../../data/services/novice_guide_service.dart'; import '../../../routes/app_routes.dart'; import '../../../core/utils/logger.dart'; @@ -68,6 +70,13 @@ class HomeController extends GetxController { Worker? _classicBluetoothWorker; Worker? _bleConnectionWorker; + /// 全局 5s 刷新定时器。声明为 static 是为了让它跨 HomeController 实例存活: + /// `Get.offAllNamed(Routes.home)` 把旧 controller 干掉、新 controller 起来时, + /// 这个定时器始终是同一个,永远在跑,直接调当前注册的 HomeController 的 + /// `refreshDeviceConnection`(=点刷新按钮的逻辑)。 + static Timer? _staticPollTimer; + static const Duration _pollInterval = Duration(seconds: 5); + final _bluetoothManager = Get.find(); final _bleManager = Get.find(); @@ -101,13 +110,30 @@ class HomeController extends GetxController { _deviceInfoWorker?.dispose(); _classicBluetoothWorker?.dispose(); _bleConnectionWorker?.dispose(); - // 移除了定时器停止逻辑 super.onClose(); } void _initWithCachedInfo() { _setupStateListeners(); _checkConnectionStatus(); + _ensureStaticPollTimer(); + } + + /// 启动静态 5s 刷新定时器。已经在跑就什么都不做。 + /// 定时器一旦起来就永远不取消(HomeController 只是 lazyPut,控制器实例可能反复 + /// 销毁/重建,但定时器跨实例存活),每次 fire 都去当前注册的 HomeController + /// 调 refreshDeviceConnection——和用户点刷新按钮完全等价。 + static void _ensureStaticPollTimer() { + if (_staticPollTimer != null && _staticPollTimer!.isActive) return; + _staticPollTimer = Timer.periodic(_pollInterval, (_) { + try { + if (Get.isRegistered()) { + Get.find().refreshDeviceConnection(); + } + } catch (_) { + // 忽略:home 还没注册或者刚销毁中间窗口期 + } + }); } void _setupStateListeners() { @@ -236,18 +262,18 @@ class HomeController extends GetxController { Future _checkConnectionStatus() async { try { - final currentBleState = await _bleManager.getConnectionState(); - print('fdp--_checkConnectionStatus--currentBleState: $currentBleState'); - bool hasClassicBluetooth = currentDevice != null; - bool hasBleConnection = currentBleState == 2; - bool wasConnected = _isFullyConnected.value; - bool isNowConnected = hasClassicBluetooth && hasBleConnection; - - //bool isGuideCompleted = _storage.read('home_guide') ?? false; - - if (isNowConnected && currentDevice == null) { - isNowConnected = false; + // 直接问杰理 SDK 当前已连接设备 + JieliDevice? sdkDevice; + try { + sdkDevice = await Jielihome.instance.connectedDevice(); + } catch (e) { + Logger.w(_tag, '查询杰理已连接设备失败: $e'); + sdkDevice = null; } + final hasBleConnection = + sdkDevice != null && sdkDevice.address.isNotEmpty; + bool wasConnected = _isFullyConnected.value; + bool isNowConnected = hasBleConnection; if (_isFullyConnected.value != isNowConnected) { _isFullyConnected.value = isNowConnected;