Browse Source

feat(设备检测): 改进设备类型检测和电量检查逻辑

扩展单端耳机检测范围,新增智能录音笔和智能眼镜类型
重构电量检查逻辑,根据设备类型采用不同策略
为单端耳机添加专门的电池显示和低电量提示
newdev_shunjiawei
lxm 1 year ago
parent
commit
070ff450a1
  1. 4
      lib/modules/home/controllers/home_controller.dart
  2. 3
      lib/modules/home/views/home_view.dart
  3. 145
      lib/modules/ota_upgrade/controllers/ota_core_contropller.dart
  4. 6
      lib/modules/pairing/controllers/pairing_controller.dart

4
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() {

3
lib/modules/home/views/home_view.dart

@ -533,7 +533,8 @@ class HomeView extends GetView<HomeController> {
// 单端耳机只显示一个电池信息(优先显示左耳,如果左耳电量为0则显示右耳)
if (isSingleHeadset) ...[
_buildCompactBatteryItem(
label: 'deviceTypeBleBluetoothHeadset'.tr,
//label: 'deviceTypeBleBluetoothHeadset'.tr,
label: '',
batteryLevel: (leftBattery > 0 || rightBattery == 0)
? leftBattery
: rightBattery,

145
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] 右耳电量

6
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(

Loading…
Cancel
Save