Browse Source

feat(ota_upgrade): 添加BLE连接状态检查功能

在OTA升级模块中增加BLE设备连接状态检查,确保设备已连接才显示版本状态和更新按钮。未连接时显示未连接提示并禁用相关功能。同时修复音频接收器中注释掉的音频模式设置代码。
newdev_shunjiawei
lxm 1 year ago
parent
commit
14a96588d8
  1. 117
      lib/modules/ota_upgrade/controllers/ota_core_contropller.dart
  2. 13
      lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart
  3. 7
      lib/modules/ota_upgrade/views/ota_upgrade_view.dart
  4. 2
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/tools/SimpleAudioReceiver.kt

117
lib/modules/ota_upgrade/controllers/ota_core_contropller.dart

@ -53,6 +53,123 @@ class OtaCoreService {
// ==================== 设备检查功能 ====================
/// 检查BLE设备连接状态
/// 返回true表示设备已连接,可以进行升级
/// 返回false表示设备未连接,不能进行升级
bool checkBleConnectionStatus() {
try {
// 检查BLE管理器的连接状态
if (!_bleManager.isConnected) {
//_showBleDisconnectedDialog();
return false;
}
// 检查设备信息是否可用
final deviceInfo = _bleManager.deviceInfo.value;
if (deviceInfo == null) {
_showDeviceInfoUnavailableDialog();
return false;
}
return true;
} catch (e) {
_showBleCheckFailedSnackbar('检查BLE连接状态时发生错误:$e');
return false;
}
}
/// 显示BLE设备未连接对话框
void _showBleDisconnectedDialog() {
Get.dialog(
AlertDialog(
title: Text(
'BLE设备未连接',
style: TextStyle(
color: Colors.red[700],
fontWeight: FontWeight.w600,
),
),
content: Text(
'固件升级需要连接BLE设备\n请确保设备已正确连接后再试',
style: TextStyle(
color: Colors.red[600],
),
),
actions: [
TextButton(
onPressed: () => Get.back(),
style: TextButton.styleFrom(
foregroundColor: Colors.red[700],
),
child: const Text('确定'),
),
],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: BorderSide(
color: Colors.red.withValues(alpha: 0.3),
width: 1,
),
),
backgroundColor: Colors.red[50],
),
);
}
/// 显示设备信息不可用对话框
void _showDeviceInfoUnavailableDialog() {
Get.dialog(
AlertDialog(
title: Text(
'设备信息不可用',
style: TextStyle(
color: Colors.orange[700],
fontWeight: FontWeight.w600,
),
),
content: Text(
'无法获取设备信息\n请重新连接设备或稍后再试',
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],
),
);
}
/// 显示BLE连接检查失败提示
/// [message] 错误信息
void _showBleCheckFailedSnackbar(String message) {
Get.snackbar(
'BLE连接检查失败',
message,
snackPosition: SnackPosition.BOTTOM,
backgroundColor: Colors.red[600],
colorText: Colors.white,
icon: const Icon(
Icons.bluetooth_disabled,
color: Colors.white,
),
);
}
/// 检查设备电量是否满足升级条件
/// 返回true表示电量充足,可以进行升级
/// 返回false表示电量不足,不能进行升级

13
lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart

@ -27,11 +27,12 @@ class OtaUpgradeController extends GetxController {
final RxDouble downloadProgress = 0.0.obs;
final RxDouble progress = 0.0.obs;
final RxInt type = OtaCoreService.NOT_STARTED.obs;
final RxBool isUpdateAvailable = true.obs;
final RxBool isUpdateAvailable = false.obs;
final RxBool isDownloading = false.obs;
final RxBool isUpgrade = false.obs;
final RxBool isFetchingDeviceInfo = true.obs;
final RxBool isFetchingDeviceInfo = false.obs;
final RxBool showDetailedStatus = false.obs;
final RxBool isBleConnected = true.obs;
String _address = '';
String _updatePackageAddress = '';
@ -53,6 +54,14 @@ class OtaUpgradeController extends GetxController {
/// 初始化控制器
/// 设置事件监听并加载版本信息
Future<void> _initializeController() async {
// 首先检查BLE设备连接状态
if (!_coreService.checkBleConnectionStatus()) {
//isFetchingDeviceInfo.value = false;
//errorMessage.value = 'unconnected'.tr;
isBleConnected.value = false;
return;
}
_listenToOtaEvents();
await _loadVersionInfo();
}

7
lib/modules/ota_upgrade/views/ota_upgrade_view.dart

@ -397,7 +397,8 @@ class OtaUpgradeView extends GetView<OtaUpgradeController> {
),
// 版本状态指示器
SizedBox(height: 14.h),
_buildVersionStatusIndicator(isDarkMode),
if (controller.isBleConnected.value)
_buildVersionStatusIndicator(isDarkMode),
],
);
}
@ -836,7 +837,9 @@ class OtaUpgradeView extends GetView<OtaUpgradeController> {
label: Text(
controller.isUpdateAvailable.value
? 'ota_updateNow'.tr
: 'latestVersion'.tr, // 立即更新 : 已是最新版本
: controller.isBleConnected.value
? 'latestVersion'.tr // 立即更新 : 已是最新版本
: 'unconnected'.tr,
style: TextStyle(
fontSize: 16.sp,
fontWeight: FontWeight.w600,

2
local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/tools/SimpleAudioReceiver.kt

@ -236,7 +236,7 @@ class SimpleAudioReceiver(private val context: Context) {
private fun runMicrophoneCapture() {
try {
// 首先设置通话音频模式
setupCommunicationAudioMode()
//setupCommunicationAudioMode()
val supportedSampleRates = intArrayOf(16000, 8000, 11025, 22050, 44100)
val sampleRate = supportedSampleRates.firstOrNull { rate ->
val bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat)

Loading…
Cancel
Save