diff --git a/lib/data/services/ble_manager.dart b/lib/data/services/ble_manager.dart index 43179eb5c..ca7cbd4e3 100644 --- a/lib/data/services/ble_manager.dart +++ b/lib/data/services/ble_manager.dart @@ -159,7 +159,7 @@ class BleManager extends GetxService { final state = event['state'] as int; final oldState = _connectionState.value; _connectionState.value = state; - + Logger.d('BleManager', '🔄 BLE状态更新: $oldState → $state'); Logger.d('BleManager', '📡 通知监听器状态变化: ${_connectionState.value}'); @@ -418,7 +418,8 @@ class BleManager extends GetxService { } else { Logger.warning('电池信息获取指令发送失败'); } - + // 添加延迟 + await Future.delayed(const Duration(seconds: 1)); // 获取设备版本信息 final versionResult = await _bleService.getDeviceVersion(); if (versionResult) { @@ -426,7 +427,8 @@ class BleManager extends GetxService { } else { Logger.warning('版本信息获取指令发送失败'); } - + // 添加延迟 + await Future.delayed(const Duration(seconds: 1)); // 获取产品id final productIdResult = await _bleService.getProductId(); if (productIdResult) { @@ -655,7 +657,7 @@ class BleManager extends GetxService { } // ==================== 配对相关方法 ==================== - + /// 检查设备是否支持Companion Device Manager Future isCompanionSupported() async { try { diff --git a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart index 4e60d798d..6a41a2b8b 100644 --- a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart @@ -20,21 +20,39 @@ class OtaUpgradeController extends GetxController { static const int CONNECTION_DISCONNECTION = 5; // 连接断开 static const int CONNECTION_SUCCESS = 6; // 连接成功 static const int DOWNLOADING_UP_FILES = 7; // 下载升级文件 + +// 错误信息,用于存储错误提示 final RxString errorMessage = ''.obs; - final RxBool isFetchingDeviceInfo = false.obs; + +// 最新版本号 final RxString latestVersion = ''.obs; + +// 当前设备固件版本号 final RxString currentVersion = ''.obs; + +// 更新包地址,用于下载更新文件 String updatepackageaddress = ''; + +// 下载进度,范围为 0.0 到 1.0 final RxDouble downloadProgress = 0.0.obs; +// 是否有可用更新的状态 final isUpdateAvailable = true.obs; - //int currentProgress = 0; // 当前进度值,范围0-100 + +// 当前升级进度,范围为 0.0 到 1.0 final RxDouble progress = 0.0.obs; + +// OTA 单例实例,用于处理升级相关逻辑 final jlota = Ota.instance; +// 是否正在下载更新包的状态 final RxBool isDownloading = false.obs; - // 蓝牙服务 + +// 蓝牙管理服务,用于获取设备信息和通信 final _bleManager = Get.find(); + final maxRetryAttempts = 3; // 最大重试次数 + int currentRetryCount = 0; + final RxBool isFetchingDeviceInfo = true.obs; @override void onInit() { super.onInit(); @@ -92,33 +110,38 @@ class OtaUpgradeController extends GetxController { Future _getCurrentVersion() async { try { + isFetchingDeviceInfo.value = true; + errorMessage.value = ''; // 添加获取设备信息的重试机制 DeviceInfo? deviceInfo = _bleManager.deviceInfo.value; - // 第一次检查设备信息 - if (deviceInfo == null) { - // 显示加载提示 - isFetchingDeviceInfo.value = true; + // // 第一次检查设备信息 + // if (deviceInfo == null) { + // // 尝试重新获取设备信息 + // await _bleManager.fetchDeviceInfo(); + // } - // 尝试重新获取设备信息 - await _bleManager.fetchDeviceInfo(); - - // 等待后再次获取设备信息 + while (currentRetryCount < maxRetryAttempts) { deviceInfo = _bleManager.deviceInfo.value; - isFetchingDeviceInfo.value = false; - // 仍然为空则抛出异常 - if (deviceInfo == null) { - throw const FormatException('无法获取设备信息,请检查设备连接'); + // 检查信息有效性 + if (deviceInfo != null && + deviceInfo.firmwareVersion.isNotEmpty && + deviceInfo.firmwareProductId.isNotEmpty) { + break; } - } - // 检查固件信息有效性 - if (deviceInfo.firmwareVersion.isEmpty || + // 获取设备信息 + await _bleManager.fetchDeviceInfo(); + await Future.delayed(const Duration(seconds: 5)); + currentRetryCount++; + } + // 最终验证 + if (deviceInfo == null || + deviceInfo.firmwareVersion.isEmpty || deviceInfo.firmwareProductId.isEmpty) { - throw const FormatException('设备固件信息不完整'); + throw "获取设备信息失败,请检查连接"; } - // 正常流程 currentVersion.value = deviceInfo.firmwareVersion; print('getequipmentinfo,id: ${deviceInfo.firmwareProductId}'); @@ -134,7 +157,8 @@ class OtaUpgradeController extends GetxController { final resp = OtaConfigResp.fromJson(response); print('getequipmentinfo,resp.systemversion: ${resp.systemversion}'); - latestVersion.value = "1"; + latestVersion.value = resp.systemversion!; + updatepackageaddress = resp.updatepackageaddress!; // 版本检查 diff --git a/lib/modules/ota_upgrade/views/ota_upgrade_view.dart b/lib/modules/ota_upgrade/views/ota_upgrade_view.dart index 1173a130d..fe85296fc 100644 --- a/lib/modules/ota_upgrade/views/ota_upgrade_view.dart +++ b/lib/modules/ota_upgrade/views/ota_upgrade_view.dart @@ -182,7 +182,7 @@ class OtaUpgradeView extends GetView { isDarkMode ? Colors.blue[300] : Colors.blue), ), SizedBox(height: 20), - Text('正在连接设备...', + Text('正在获取设备信息...', style: TextStyle(color: isDarkMode ? Colors.grey[300] : Colors.grey)) ],