Browse Source

fix(BleManager): 修复编解码器状态管理问题

添加等待状态标志位来正确处理编解码器开关操作,避免状态更新混乱
同时优化翻译控制器中的日志标记和历史记录判空逻辑
weicu
lxm 10 months ago
parent
commit
46e3046b51
  1. 31
      lib/data/services/ble_manager.dart
  2. 7
      lib/modules/translation/controllers/translation_controller.dart

31
lib/data/services/ble_manager.dart

@ -202,6 +202,8 @@ class BleManager extends GetxService {
final _isCodecActive = false.obs;
bool get isCodecActive => _isCodecActive.value;
RxBool get isCodecActiveRx => _isCodecActive;
bool _awaitingCodecOpen = false; //是否正在等待编码器打开的成功应答
bool _awaitingCodecClose = false; //是否正在等待编码器关闭的成功应答
// 订阅
StreamSubscription<dynamic>? _statusSubscription;
@ -267,7 +269,9 @@ class BleManager extends GetxService {
state == BleConnectionState.error) {
Logger.info('lxm--BLE设备已断开或发生错误');
_isConnected.value = false;
// 更新设备信息Add commentMore actions
_awaitingCodecOpen = false;
_awaitingCodecClose = false;
_isCodecActive.value = false;
Logger.info('lxm--断开连接时重置设备信息为0');
_updateDeviceInfoIfNeeded(
batteryLevel: 0,
@ -376,7 +380,16 @@ class BleManager extends GetxService {
print(" CONTROL_CODEC: $commandId,$data");
final bool codecSuccess = _isCodecControlSuccess(data);
_isCodecActive.value = codecSuccess; // 更新编解码器状态
if (_awaitingCodecOpen) {
//如果正在等待打开编解码器
_isCodecActive.value = codecSuccess; // 只在等待打开时更新状态
_awaitingCodecOpen = false;
} else if (_awaitingCodecClose) {
if (codecSuccess) {
_isCodecActive.value = false;
}
_awaitingCodecClose = false;
}
if (codecSuccess) {
print("lxm--编解码控制成功 (rawData 第二字节非 0)");
@ -991,19 +1004,27 @@ class BleManager extends GetxService {
/// 打开解码器
Future<bool> openDecoder() async {
try {
_awaitingCodecOpen = true;
_awaitingCodecClose = false;
_isCodecActive.value = false;
return await _bleService.openDecoder();
} catch (e) {
Logger.error('打开解码器失败: ${e.toString()}');
_awaitingCodecOpen = false;
return false;
}
}
/// 打开解码器
/// 打开A2DP解码器
Future<bool> openA2DPDecoder() async {
try {
_awaitingCodecOpen = true;
_awaitingCodecClose = false;
_isCodecActive.value = false;
return await _bleService.openA2DPDecoder();
} catch (e) {
Logger.error('打开解码器失败: ${e.toString()}');
_awaitingCodecOpen = false;
return false;
}
}
@ -1021,9 +1042,13 @@ class BleManager extends GetxService {
/// 关闭编解码器
Future<bool> closeCodec() async {
try {
_awaitingCodecOpen = false;
_awaitingCodecClose = true;
_isCodecActive.value = false;
return await _bleService.closeCodec();
} catch (e) {
Logger.error('关闭编解码器失败: ${e.toString()}');
_awaitingCodecClose = false;
return false;
}
}

7
lib/modules/translation/controllers/translation_controller.dart

@ -1008,7 +1008,7 @@ class TranslationController extends GetxController with WidgetsBindingObserver {
if (event.text.isEmpty) break;
//通话模式下:己方语言即为源语言,无需判断
detectedLanguageCode = sourceLanguageCode.value;
Logger.info("处理己方识别完成:${event.text}");
Logger.info("lxm---处理己方识别完成:${event.text}");
handleFinalResult(event.text);
break;
@ -1126,10 +1126,11 @@ class TranslationController extends GetxController with WidgetsBindingObserver {
/// 处理最终识别结果
/// [text] 识别的文本
Future<void> handleFinalResult(String text) async {
if (text.isEmpty || translationHistory.isEmpty) return;
if (text.isEmpty) return;
// 更新当前项
if (translationHistory.last.isIntermediate) {
if (translationHistory.isNotEmpty &&
translationHistory.last.isIntermediate) {
translationHistory.last.sourceText = text;
translationHistory.last.isIntermediate = false;
translationHistory.refresh();

Loading…
Cancel
Save