From cf629495e2985106b0fe0d4f7fbdd0a285dbedd5 Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Wed, 23 Jul 2025 22:06:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BF=BB=E8=AF=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/data/services/ble_manager.dart | 3 +- .../Sources/azure_speech/AzureAsrHelper.swift | 147 +++++++++++++----- 2 files changed, 109 insertions(+), 41 deletions(-) diff --git a/lib/data/services/ble_manager.dart b/lib/data/services/ble_manager.dart index 96bea2fc6..b55927d5a 100644 --- a/lib/data/services/ble_manager.dart +++ b/lib/data/services/ble_manager.dart @@ -562,7 +562,8 @@ class BleManager extends GetxService { 'volcanoAppId': '2316081933', 'volcanoToken': 'mcJTh6OelBqU_L78lgGd77Nyrc_-5-En', 'serverurl': serverurl, - 'usertoken': jsonString + 'usertoken': jsonString, + 'supportedLanguages': ['zh-CN'], }; // 记录日志(不记录敏感信息) diff --git a/local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureAsrHelper.swift b/local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureAsrHelper.swift index 9425fa62d..e45b51003 100644 --- a/local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureAsrHelper.swift +++ b/local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureAsrHelper.swift @@ -272,7 +272,15 @@ public class AzureAsrHelper: NSObject { // 创建识别器 if isAutoDetectLanguage { - let autoDetectConfig = try SPXAutoDetectSourceLanguageConfiguration(supportedLanguages) + // 使用更精确的语言配置方式 + var sourceLanguageConfigs: [SPXSourceLanguageConfiguration] = [] + for language in supportedLanguages { + if let config = try? SPXSourceLanguageConfiguration(language) { + sourceLanguageConfigs.append(config) + } + } + + let autoDetectConfig = try SPXAutoDetectSourceLanguageConfiguration(sourceLanguageConfigurations: sourceLanguageConfigs) recognizer = try SPXSpeechRecognizer( speechConfiguration: speechConfig!, autoDetectSourceLanguageConfiguration: autoDetectConfig, @@ -336,10 +344,31 @@ public class AzureAsrHelper: NSObject { recognizer.addRecognizingEventHandler { [weak self] (_, event) in guard let self = self else { return } let result = event.result - let detectedLanguage = self.detectLanguage(from: result) - print("识别中事件: \(detectedLanguage)") - DispatchQueue.main.async { - callback.onRecognizing(result.text ?? "", detectedLanguage) + print("识别中文本: \(result.text)") + // 优化:只处理非空结果 + if !(result.text?.isEmpty ?? true) { + let detectedLanguage: String + if self.isAutoDetectLanguage { + do { + let langResult = try SPXAutoDetectSourceLanguageResult(result) + let rawLanguage = langResult.language + print("[DEBUG] 原始检测语言: \(rawLanguage ?? "nil"), 文本: \(result.text ?? "nil"), 支持的语言: \(self.supportedLanguages)") + + // 应用启发式语言检测纠正 + detectedLanguage = self.correctLanguageDetection(rawLanguage: rawLanguage ?? "", text: result.text ?? "") + print("[DEBUG] 纠正后语言: \(detectedLanguage)") + } catch { + print("[DEBUG] 语言检测异常: \(error.localizedDescription)") + detectedLanguage = "" + } + } else { + detectedLanguage = self.currentLanguage + print("[DEBUG] 使用当前语言: \(detectedLanguage)") + } + print("识别中事件: \(detectedLanguage)") + DispatchQueue.main.async { + callback.onRecognizing(result.text ?? "", detectedLanguage) + } } } @@ -347,8 +376,27 @@ public class AzureAsrHelper: NSObject { recognizer.addRecognizedEventHandler { [weak self] (_, event) in guard let self = self else { return } let result = event.result - if result.reason == .recognizedSpeech { - let detectedLanguage = self.detectLanguage(from: result) + print("识别完成文本: \(result.text)") + if result.reason == .recognizedSpeech && !(result.text?.isEmpty ?? true) { + let detectedLanguage: String + if self.isAutoDetectLanguage { + do { + let langResult = try SPXAutoDetectSourceLanguageResult(result) + let rawLanguage = langResult.language + print("[DEBUG] 识别完成 - 原始检测语言: \(rawLanguage ?? "nil"), 文本: \(result.text ?? "nil"), 支持的语言: \(self.supportedLanguages)") + + // 应用启发式语言检测纠正 + let correctedLanguage = self.correctLanguageDetection(rawLanguage: rawLanguage ?? "", text: result.text ?? "") + detectedLanguage = correctedLanguage.isEmpty ? (self.supportedLanguages.first ?? self.currentLanguage) : correctedLanguage + print("[DEBUG] 识别完成 - 纠正后语言: \(detectedLanguage)") + } catch { + print("[DEBUG] 识别完成 - 语言检测异常: \(error.localizedDescription)") + detectedLanguage = self.supportedLanguages.first ?? self.currentLanguage + } + } else { + detectedLanguage = self.currentLanguage + print("[DEBUG] 识别完成 - 使用当前语言: \(detectedLanguage)") + } print("识别完成事件: \(detectedLanguage)") DispatchQueue.main.async { callback.onResult(result.text ?? "", detectedLanguage) @@ -387,38 +435,6 @@ public class AzureAsrHelper: NSObject { return true } -// 提取的辅助方法 -private func detectLanguage(from result: SPXSpeechRecognitionResult) -> String { - if isAutoDetectLanguage { - let langResult = try SPXAutoDetectSourceLanguageResult(result) - return langResult.language ?? "" - } else { - return currentLanguage - } -} - /** - * 获取检测到的语言 - */ - private func getDetectedLanguage(from result: SPXSpeechRecognitionResult) -> String { - if !isAutoDetectLanguage { - return "" - } - - // 尝试从属性中获取语言 - if let properties = result.properties, - let language = properties.getPropertyByName("SpeechServiceResponse_RecognitionLanguage") { - return language - } - - // 尝试另一种方式获取语言 - do { - let langResult = try SPXAutoDetectSourceLanguageResult(result) - print("获取检测到的语言") - return langResult.language ?? "" - } catch { - return "" - } - } /** * 禁用蓝牙音频功能,切换回正常音频模式 @@ -853,6 +869,57 @@ private func detectLanguage(from result: SPXSpeechRecognitionResult) -> String { } } } + + /** + * 启发式语言检测纠正方法 + * 基于文本内容特征来纠正Azure语音服务的语言检测结果 + */ + private func correctLanguageDetection(rawLanguage: String, text: String) -> String { + // 如果原始检测为空,返回空 + if rawLanguage.isEmpty || text.isEmpty { + return rawLanguage + } + + let trimmedText = text.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() + + // 英文特征检测 + let englishPattern = "^[a-z\\s\\.,!?;:'-]+$" + let englishRegex = try? NSRegularExpression(pattern: englishPattern, options: .caseInsensitive) + let englishMatches = englishRegex?.numberOfMatches(in: trimmedText, options: [], range: NSRange(location: 0, length: trimmedText.count)) ?? 0 + + // 中文特征检测 + let chinesePattern = "[\\u4e00-\\u9fff]" + let chineseRegex = try? NSRegularExpression(pattern: chinesePattern, options: []) + let chineseMatches = chineseRegex?.numberOfMatches(in: trimmedText, options: [], range: NSRange(location: 0, length: trimmedText.count)) ?? 0 + + // 常见英文单词列表 + let commonEnglishWords = ["hello", "hi", "please", "thank", "you", "yes", "no", "good", "morning", "afternoon", "evening", "night", "how", "are", "what", "where", "when", "why", "who", "can", "could", "would", "should", "will", "the", "and", "or", "but", "if", "then", "this", "that", "these", "those"] + + let hasCommonEnglishWords = commonEnglishWords.contains { word in + trimmedText.contains(word) + } + + print("[DEBUG] 语言纠正分析 - 文本: '\(trimmedText)', 英文匹配: \(englishMatches > 0), 中文字符: \(chineseMatches), 常见英文词: \(hasCommonEnglishWords), 原始语言: \(rawLanguage)") + + // 纠正逻辑 + if rawLanguage == "zh-CN" { + // 如果Azure检测为中文,但文本明显是英文特征 + if (englishMatches > 0 && chineseMatches == 0) || hasCommonEnglishWords { + print("[DEBUG] 纠正: \(rawLanguage) -> en-US (检测到英文特征)") + return "en-US" + } + } else if rawLanguage == "en-US" { + // 如果Azure检测为英文,但包含中文字符 + if chineseMatches > 0 { + print("[DEBUG] 纠正: \(rawLanguage) -> zh-CN (检测到中文字符)") + return "zh-CN" + } + } + + // 如果没有明确的纠正理由,保持原始检测结果 + return rawLanguage + } + // MARK: - 回调函数 /** * 连续识别回调接口 @@ -899,4 +966,4 @@ private func detectLanguage(from result: SPXSpeechRecognitionResult) -> String { */ func onError(_ error: String) } -} +} From 693571cda8a1f8f5dcbaa47f9c9d44652c545255 Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Wed, 23 Jul 2025 20:04:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=E7=89=88=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/pairing_controller.dart | 28 +++++++++++++------ .../Sources/ble_service/BleService.swift | 7 ++++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/modules/pairing/controllers/pairing_controller.dart b/lib/modules/pairing/controllers/pairing_controller.dart index af7e399f8..5992d98cf 100644 --- a/lib/modules/pairing/controllers/pairing_controller.dart +++ b/lib/modules/pairing/controllers/pairing_controller.dart @@ -660,14 +660,26 @@ class PairingController extends GetxController { String? uuids = deviceInfo["id"]; _verifiedDeviceAddress = uuids; print('找到匹配设备,获取到 uuids: $uuids'); - // 检查是否已经绑定 - // if (User.instance.earphonelicense != "" && - // User.instance.earphonemac == device.address) { - // // 已经绑定 - // return 10; - // } else { - return 1; - // } + //检查是否已经绑定 + final existingDevice = User.instance.devices.firstWhere( + (d) => + d.devicetype == 2 && + d.license != "" && + d.devicemac == device.address, + orElse: () => UserDevice( + id: -1, + uid: '', + devicetype: -1, + devicename: '', + devicemac: '', + license: ''), + ); + if (existingDevice.id != -1) { + // 已经绑定 + return 10; + } else { + return 1; + } } } else if (Platform.isAndroid) { // Android设备,提取15-24字节的授权码 diff --git a/local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift b/local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift index c54bacde2..6647a5e4a 100644 --- a/local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift +++ b/local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift @@ -229,7 +229,12 @@ private var cmdReplyType: UInt8 = 0 isScanning = true - // 移除服务UUID过滤,扫描所有设备 + // // 移除服务UUID过滤,扫描所有设备 + // manager.scanForPeripherals(withServices: nil, options: [ + // CBCentralManagerScanOptionAllowDuplicatesKey: true // 允许重复以获取更新的广播数据 + // ]) + let serviceUUIDs = [BleConst.SERVICE_UUID, BleConst.SERVICE_UUID1] + manager.scanForPeripherals(withServices: nil, options: [ CBCentralManagerScanOptionAllowDuplicatesKey: true // 允许重复以获取更新的广播数据 ])