diff --git a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart index e87191f33..f21bccf07 100644 --- a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart @@ -271,6 +271,12 @@ class OtaUpgradeController extends GetxController { Get.back(); Future.delayed(Duration(milliseconds: 300)); + + // 检查设备电量 + if (!_checkBatteryLevel()) { + return; // 电量不足,不执行升级 + } + startDownload(); }, child: Text('更新'), @@ -399,6 +405,60 @@ class OtaUpgradeController extends GetxController { // isUpgrade.value = false; } + // 检查设备电量是否满足升级条件 + bool _checkBatteryLevel() { + try { + final deviceInfo = _bleManager.deviceInfo.value; + + if (deviceInfo == null) { + Get.snackbar( + '电量检查失败', + '无法获取设备信息,请检查设备连接', + snackPosition: SnackPosition.BOTTOM, + backgroundColor: Colors.orange, + colorText: Colors.white, + ); + return false; + } + + final leftBattery = deviceInfo.leftEarBattery; + final rightBattery = deviceInfo.rightEarBattery; + + // 检查左右耳电量是否都大于30% + if (leftBattery <= 30 || rightBattery <= 30) { + String message = '设备电量不足,无法进行固件升级\n'; + message += '升级要求:左右耳电量均需大于30%\n'; + message += '当前电量:左耳 ${leftBattery}%,右耳 ${rightBattery}%\n'; + message += '请充电后再试'; + + Get.dialog( + AlertDialog( + title: Text('电量不足'), + content: Text(message), + actions: [ + TextButton( + onPressed: () => Get.back(), + child: Text('确定'), + ), + ], + ), + ); + return false; + } + + return true; + } catch (e) { + Get.snackbar( + '电量检查失败', + '检查设备电量时发生错误:$e', + snackPosition: SnackPosition.BOTTOM, + backgroundColor: Colors.red, + colorText: Colors.white, + ); + return false; + } + } + void _listenToEvents() { Ota.instance.eventStream.listen((event) { if (event.event == 'onProgress' && diff --git a/lib/modules/ota_upgrade/views/ota_upgrade_view.dart b/lib/modules/ota_upgrade/views/ota_upgrade_view.dart index 8beb61aa1..49659084d 100644 --- a/lib/modules/ota_upgrade/views/ota_upgrade_view.dart +++ b/lib/modules/ota_upgrade/views/ota_upgrade_view.dart @@ -221,33 +221,33 @@ class OtaUpgradeView extends GetView { value: deviceInfo?.name ?? '未知设备', isDarkMode: isDarkMode, ), - SizedBox(height: 10.h), - // 电量信息区域 - Row( - children: [ - // 左耳电量 - Expanded( - child: _buildBatteryInfo( - label: '左耳电量', - batteryLevel: deviceInfo?.leftEarBattery ?? 0, - isCharging: deviceInfo?.leftEarCharging ?? false, - isDarkMode: isDarkMode, - icon: Icons.headset, - ), - ), - SizedBox(width: 10.w), - // 右耳电量 - Expanded( - child: _buildBatteryInfo( - label: '右耳电量', - batteryLevel: deviceInfo?.rightEarBattery ?? 0, - isCharging: deviceInfo?.rightEarCharging ?? false, - isDarkMode: isDarkMode, - icon: Icons.headset, - ), - ), - ], - ), + // SizedBox(height: 10.h), + // // 电量信息区域 + // Row( + // children: [ + // // 左耳电量 + // Expanded( + // child: _buildBatteryInfo( + // label: '左耳电量', + // batteryLevel: deviceInfo?.leftEarBattery ?? 0, + // isCharging: deviceInfo?.leftEarCharging ?? false, + // isDarkMode: isDarkMode, + // icon: Icons.headset, + // ), + // ), + // SizedBox(width: 10.w), + // // 右耳电量 + // Expanded( + // child: _buildBatteryInfo( + // label: '右耳电量', + // batteryLevel: deviceInfo?.rightEarBattery ?? 0, + // isCharging: deviceInfo?.rightEarCharging ?? false, + // isDarkMode: isDarkMode, + // icon: Icons.headset, + // ), + // ), + // ], + // ), ], ), );