diff --git a/lib/data/models/appconfig_model.dart b/lib/data/models/appconfig_model.dart index 945103b42..7f7b6bd59 100644 --- a/lib/data/models/appconfig_model.dart +++ b/lib/data/models/appconfig_model.dart @@ -1,4 +1,5 @@ import 'package:json_annotation/json_annotation.dart'; +import 'package:get/get.dart'; part 'appconfig_model.g.dart'; // 生成的文件名 @@ -136,3 +137,24 @@ enum DeviceType { /// 设备类型对应的整数值 final int value; } + +// 为 DeviceType 添加扩展方法 +extension DeviceTypeExtension on DeviceType { + /// 获取设备类型的显示名称 + /// @Description 根据设备类型返回对应的翻译键 + String get displayName { + switch (this) { + case DeviceType.unknown: + return "deviceTypeUnknown".tr; // 未知设备 + case DeviceType.classicBluetoothHeadset: + return "deviceTypeClassicBluetoothHeadset".tr; // 经典蓝牙耳机 + case DeviceType.bleBluetoothDoubleHeadset: + case DeviceType.bleBluetoothSingleHeadset: + return "deviceTypeBleBluetoothHeadset".tr; // BLE蓝牙耳机(单端和双端都使用同一个翻译键) + case DeviceType.smartVoiceRecorder: + return "deviceTypeSmartVoiceRecorder".tr; // 智能录音笔 + case DeviceType.smartEye: + return "deviceTypeSmartEye".tr; // 智能眼镜 + } + } +} diff --git a/lib/modules/home/controllers/home_controller.dart b/lib/modules/home/controllers/home_controller.dart index a7b702576..7d8aa47af 100644 --- a/lib/modules/home/controllers/home_controller.dart +++ b/lib/modules/home/controllers/home_controller.dart @@ -393,7 +393,9 @@ class HomeController extends GetxController { /// 判断当前设备是否为单端耳机 bool isSingleHeadset() { - return getCurrentDeviceType() == DeviceType.bleBluetoothSingleHeadset; + return getCurrentDeviceType() == DeviceType.bleBluetoothSingleHeadset || + getCurrentDeviceType() == DeviceType.smartVoiceRecorder || + getCurrentDeviceType() == DeviceType.smartEye; } String getVersionText() { diff --git a/lib/modules/home/views/home_view.dart b/lib/modules/home/views/home_view.dart index e656f0b0c..5e5a72be4 100644 --- a/lib/modules/home/views/home_view.dart +++ b/lib/modules/home/views/home_view.dart @@ -6,6 +6,7 @@ import '../../../core/widgets/intro_overlay_builder.dart'; import '../controllers/home_controller.dart'; import '../../../routes/app_routes.dart'; import '../../../core/widgets/common_bottom_nav.dart'; +import '../../../data/models/appconfig_model.dart'; // 呼吸灯动画组件 class AnimatedPulsing extends StatefulWidget { @@ -533,7 +534,9 @@ class HomeView extends GetView { // 单端耳机只显示一个电池信息(优先显示左耳,如果左耳电量为0则显示右耳) if (isSingleHeadset) ...[ _buildCompactBatteryItem( - label: 'deviceTypeBleBluetoothHeadset'.tr, + label: controller + .getCurrentDeviceType() + .displayName, // 根据设备类型动态显示文本 batteryLevel: (leftBattery > 0 || rightBattery == 0) ? leftBattery : rightBattery, diff --git a/lib/modules/ota_upgrade/controllers/ota_core_contropller.dart b/lib/modules/ota_upgrade/controllers/ota_core_contropller.dart index 03ec1be87..cd05687f0 100644 --- a/lib/modules/ota_upgrade/controllers/ota_core_contropller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_core_contropller.dart @@ -185,22 +185,155 @@ class OtaCoreService { return false; } + // 获取设备类型以确定检查策略 + String deviceName = getDeviceName(); + DBProduct? product = AppConfig.getproduct(deviceName); + if (product == null) { + // 如果根据设备名称找不到产品,尝试使用默认产品名称 + product = AppConfig.getproduct("DeapSound"); + if (product == null) { + _showBatteryCheckFailedSnackbar('无法获取产品信息,设备名称: $deviceName'); + return false; + } + } + final leftBattery = deviceInfo.leftEarBattery; final rightBattery = deviceInfo.rightEarBattery; - // 检查左右耳电量是否都大于20% - if (leftBattery <= 20 || rightBattery <= 20) { - _showLowBatteryDialog(leftBattery, rightBattery); - return false; + // 根据设备类型进行不同的电量检查策略 + if (product.devicetype == DeviceType.bleBluetoothSingleHeadset.value || + product.devicetype == DeviceType.smartEye.value || + product.devicetype == DeviceType.smartVoiceRecorder.value) { + // 单端耳机:只需要检查有效的那一端电量 + return _checkSingleHeadsetBattery(leftBattery, rightBattery); + } else if (product.devicetype == + DeviceType.bleBluetoothDoubleHeadset.value) { + // 双端耳机:需要检查左右两端电量 + return _checkDoubleHeadsetBattery(leftBattery, rightBattery); + } else { + // 其他设备类型,使用原有逻辑(双端检查) + return _checkDoubleHeadsetBattery(leftBattery, rightBattery); } - - return true; } catch (e) { _showBatteryCheckFailedSnackbar('检查设备电量时发生错误:$e'); return false; } } + /// 检查单端耳机电量 + /// [leftBattery] 左耳电量 + /// [rightBattery] 右耳电量 + /// 返回true表示电量充足 + bool _checkSingleHeadsetBattery(int leftBattery, int rightBattery) { + // 单端耳机逻辑:优先检查左耳,如果左耳无效(<=0或127)则检查右耳 + int activeBattery; + String activeEar; + + if (leftBattery > 0 && leftBattery != 127) { + // 左耳有效,使用左耳电量 + activeBattery = leftBattery; + activeEar = '左耳'; + } else if (rightBattery > 0 && rightBattery != 127) { + // 左耳无效,使用右耳电量 + activeBattery = rightBattery; + activeEar = '右耳'; + } else { + // 两边都无效 + _showBatteryCheckFailedSnackbar('无法获取有效的电量信息'); + return false; + } + + // 检查有效端的电量是否大于20% + if (activeBattery <= 20) { + _showSingleHeadsetLowBatteryDialog(activeEar, activeBattery); + return false; + } + + return true; + } + + /// 检查双端耳机电量 + /// [leftBattery] 左耳电量 + /// [rightBattery] 右耳电量 + /// 返回true表示电量充足 + bool _checkDoubleHeadsetBattery(int leftBattery, int rightBattery) { + // 双端耳机逻辑:需要检查左右两端电量是否都大于20% + + // 检查电量数据有效性 + bool leftValid = leftBattery > 0 && leftBattery != 127; + bool rightValid = rightBattery > 0 && rightBattery != 127; + + if (!leftValid || !rightValid) { + _showBatteryCheckFailedSnackbar('无法获取有效的电量信息'); + return false; + } + + // 如果只有一端有效,按单端处理 + // if (!leftValid || !rightValid) { + // String activeEar = leftValid ? '左耳' : '右耳'; + // int activeBattery = leftValid ? leftBattery : rightBattery; + + // if (activeBattery <= 20) { + // _showSingleHeadsetLowBatteryDialog(activeEar, activeBattery); + // return false; + // } + // return true; + // } + + // 两端都有效,检查是否都大于20% + if (leftBattery <= 20 || rightBattery <= 20) { + _showLowBatteryDialog(leftBattery, rightBattery); + return false; + } + + return true; + } + + /// 显示单端耳机电量不足对话框 + /// [earSide] 耳机端(左耳/右耳) + /// [batteryLevel] 电量百分比 + void _showSingleHeadsetLowBatteryDialog(String earSide, int batteryLevel) { + String message = '设备电量不足,无法进行固件升级\n'; + message += '升级要求:设备电量需大于20%\n'; + message += '当前电量:$earSide ${batteryLevel}%\n'; + message += '请充电后再试'; + + Get.dialog( + AlertDialog( + title: Text( + '电量不足', + style: TextStyle( + color: Colors.orange[700], + fontWeight: FontWeight.w600, + ), + ), + content: Text( + message, + style: TextStyle( + color: Colors.orange[600], + ), + ), + actions: [ + TextButton( + onPressed: () => Get.back(), + style: TextButton.styleFrom( + foregroundColor: Colors.orange[700], + ), + child: const Text('确定'), + ), + ], + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + side: BorderSide( + color: Colors.orange.withValues(alpha: 0.3), + width: 1, + ), + ), + backgroundColor: Colors.orange[50], + ), + ); + } + /// 显示电量不足对话框 /// [leftBattery] 左耳电量 /// [rightBattery] 右耳电量 diff --git a/lib/modules/pairing/controllers/pairing_controller.dart b/lib/modules/pairing/controllers/pairing_controller.dart index 51387abe8..3fb4e4ceb 100644 --- a/lib/modules/pairing/controllers/pairing_controller.dart +++ b/lib/modules/pairing/controllers/pairing_controller.dart @@ -817,7 +817,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { if (Platform.isAndroid) { final existingDevice = User.instance.devices.firstWhere( (d) => - d.devicetype == 2 && + d.devicetype != 1 && d.devicename.contains(deviceName) && d.devicemac == device.address, orElse: () => UserDevice( @@ -879,7 +879,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { //检查是否已经绑定 final existingDevice = User.instance.devices.firstWhere( (d) => - d.devicetype == 2 && + d.devicetype != 1 && d.license != "" && d.devicemac == device.address, orElse: () => UserDevice( @@ -913,7 +913,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { //检查是否已经绑定 final existingDevice = User.instance.devices.firstWhere( (d) => - d.devicetype == 2 && + d.devicetype != 1 && d.license != "" && d.devicemac == device.address, orElse: () => UserDevice(