From 5cdef5423bf0a57ad2daceebbf07252a37805305 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Wed, 28 Jan 2026 10:04:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(agent=5Fservice):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=AF=AD=E9=9F=B3=E8=AF=86=E5=88=AB=E5=90=AF=E5=8A=A8=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E6=B7=BB=E5=8A=A0=E8=B0=83=E8=AF=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整初始化检查顺序,避免在未初始化时执行蓝牙操作 - 添加 ble_wakeup 模式连续触发保护,防止重复启动 - 增加关键节点调试日志,便于问题排查 - 移除部分冗余日志输出 --- .../agent_service/AgentServiceImpl.swift | 29 ++++++++++++++----- .../Sources/ble_service/BleService.swift | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift index 6b6e90a2b..a3bfb676b 100644 --- a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift +++ b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift @@ -442,7 +442,7 @@ class AgentServiceImpl: NSObject { self.idleTimer = nil } guard self.isRecognizing else { return } - os_log("启动空闲检测,超时时间: %.0f秒,模式: %{public}@", + os_log("liwei--------启动空闲检测,超时时间: %.0f秒,模式: %{public}@", log: self.logger, type: .info, idleSeconds, @@ -453,7 +453,7 @@ class AgentServiceImpl: NSObject { guard let self = self else { return } DispatchQueue.main.async { [weak self] in guard let self = self else { return } - os_log("空闲检测触发自动停止,模式: %{public}@", + os_log("liwei--------空闲检测触发自动停止,模式: %{public}@", log: self.logger, type: .info, self.currentRecognitionMode) @@ -490,6 +490,17 @@ class AgentServiceImpl: NSObject { } } func startRecognition(useBle: Bool = false, mode: String = "normal") -> Bool { + if !isInitialized { + sendError("服务未初始化", code: "NOT_INITIALIZED") + return false + } + + if useBle, mode == "ble_wakeup", currentRecognitionMode == "ble_wakeup", (isRecognizing || isStartingRecognition) { + os_log("liwei-------- ble_wakeup 连续触发,识别中/启动中,忽略重复 startRecognition", log: logger, type: .info) + audioPlayer?.playStartSound() + return true + } + // 接收到唤醒信号,打开编码器 (设备侧) print("ai启动语音\(useBle))") var openResult = false // 添加openResult变量定义 @@ -498,10 +509,6 @@ class AgentServiceImpl: NSObject { // 捕获openEncoder的返回值 openResult = BleService.shared.openEncoder() // 正确捕获返回值 } - if !isInitialized { - sendError("服务未初始化", code: "NOT_INITIALIZED") - return false - } if !useBle { guard AVAudioSession.sharedInstance().recordPermission == .granted else { @@ -509,6 +516,7 @@ class AgentServiceImpl: NSObject { return false } } + lastRecognitionMode = currentRecognitionMode stopRecognition() if isAiStreaming { @@ -528,13 +536,13 @@ class AgentServiceImpl: NSObject { // 设置当前识别模式 currentRecognitionMode = mode - os_log("liwei--------------- 设置语音识别模式: %{public}@", log: logger, type: .info, mode) + let audioSourceType: AzureAsrHelper.AudioSourceType = useBle ? .external : .microphone // 新增:标记为"启动中",用于允许 stop 在启动未完成时也能生效 isStartingRecognition = true - print("ai启动语音=\(audioSourceType)=\(useBle)") + print("ai启动语音=\(audioSourceType)=\(useBle)") guard let success = azureAsrHelper?.startContinuousRecognition( mod:self.currentRecognitionMode, audioSourceType: audioSourceType, @@ -543,11 +551,13 @@ class AgentServiceImpl: NSObject { // 新增:启动失败时复位"启动中"状态 isStartingRecognition = false sendError("启动语音识别失败", code: "RECOGNITION_START_ERROR") + os_log("liwei--------------- 启动语音识别失败: %{public}@", log: logger, type: .info, mode) return false } if(useBle){ isKeepResult = true } + os_log("liwei--------------- 启动语音识别: %{public}@", log: logger, type: .info, mode) // 根据模式启动相应的空闲检测(此处 guard isRecognizing,会在 onSessionStarted 中再次启动) startIdleCheckForMode(mode: mode) @@ -2125,10 +2135,12 @@ class AudioPlayer { } func playStartSound() { + os_log("liwei 播放 playStartSound", log: self.logger, type: .debug) playSound(named: "start", fileType: "mp3") } func playAwaitSound() { + os_log("liwei 播放 playAwaitSound", log: self.logger, type: .debug) // 若已处于等待音效播放状态,则直接返回,避免频繁启停 if isAwaitSoundActive { return } @@ -2146,6 +2158,7 @@ class AudioPlayer { } func playStopSound() { + os_log("liwei 播放 playStopSound", log: self.logger, type: .debug) playSound(named: "stop", fileType: "mp3") } 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 1ec345635..30670648c 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 @@ -1008,7 +1008,7 @@ private var cmdReplyType: UInt8 = 0 // 记录响应详情 var hexString = data.map { String(format: "0x%02X", $0) }.joined(separator: ", ") - os_log("liwei---- 收到设备响应: %@", log: logger, type: .debug, hexString) + // os_log("liwei---- 收到设备响应: %@", log: logger, type: .debug, hexString) // 检查数据长度(与 Android 统一失败判定保持一致) if data.count != Int(length) + 4 { // 帧头 + 命令 + 长度 + 数据 + CRC