From 070ff450a100428917403d929fc22bb82cf506ee Mon Sep 17 00:00:00 2001 From: lxm <1274844347@qq.com> Date: Thu, 9 Oct 2025 19:49:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=AE=BE=E5=A4=87=E6=A3=80=E6=B5=8B):=20?= =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=AE=BE=E5=A4=87=E7=B1=BB=E5=9E=8B=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=92=8C=E7=94=B5=E9=87=8F=E6=A3=80=E6=9F=A5=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 扩展单端耳机检测范围,新增智能录音笔和智能眼镜类型 重构电量检查逻辑,根据设备类型采用不同策略 为单端耳机添加专门的电池显示和低电量提示 --- .../home/controllers/home_controller.dart | 4 +- lib/modules/home/views/home_view.dart | 3 +- .../controllers/ota_core_contropller.dart | 145 +++++++++++++++++- .../controllers/pairing_controller.dart | 6 +- 4 files changed, 147 insertions(+), 11 deletions(-) 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..fccea3b5e 100644 --- a/lib/modules/home/views/home_view.dart +++ b/lib/modules/home/views/home_view.dart @@ -533,7 +533,8 @@ class HomeView extends GetView { // 单端耳机只显示一个电池信息(优先显示左耳,如果左耳电量为0则显示右耳) if (isSingleHeadset) ...[ _buildCompactBatteryItem( - label: 'deviceTypeBleBluetoothHeadset'.tr, + //label: 'deviceTypeBleBluetoothHeadset'.tr, + label: '', 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(