From 0c74bf77aa09f6375a44b90a9b82d5079619728d Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Mon, 13 Oct 2025 19:31:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dios=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E4=B8=8D=E4=BA=86=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/pairing_controller.dart | 36 ++++++++++--------- .../agent_service/AgentService.kt | 2 +- .../agent_service/AgentServiceImpl.swift | 5 +-- .../yunqiinnovation/ble_service/BleService.kt | 15 +++++++- .../Sources/ble_service/BleService.swift | 20 ++++++++++- 5 files changed, 57 insertions(+), 21 deletions(-) diff --git a/lib/modules/pairing/controllers/pairing_controller.dart b/lib/modules/pairing/controllers/pairing_controller.dart index 5f548f303..ba9ed71f4 100644 --- a/lib/modules/pairing/controllers/pairing_controller.dart +++ b/lib/modules/pairing/controllers/pairing_controller.dart @@ -672,9 +672,10 @@ class PairingController extends GetxController with WidgetsBindingObserver { _verifiedDeviceAddress?.toUpperCase()) { // 已有关联,检查BLE连接状态 Logger.i(_tag, '设备已关联,检查BLE连接状态'); + print('fdp--connectble--pairedMacAddress: $pairedMacAddress'); final currentBleState = await _bleManager.getConnectionState(); Logger.i(_tag, '验证时BLE状态: $currentBleState'); - + print('fdp--connectble--currentBleState: $currentBleState'); if (currentBleState == 2) { // BleConnectionState.connected Logger.i(_tag, 'BLE已连接,直接完成配对'); @@ -701,21 +702,24 @@ class PairingController extends GetxController with WidgetsBindingObserver { } // Logger.i(_tag, '开始BLE设备关联: $_verifiedDeviceName:$_verifiedDeviceAddress'); + if (Platform.isIOS) { + // 调用BLE管理器的关联方法 + final success = await _bleManager.startDeviceAssociation( + deviceNamePattern: _verifiedDeviceName!, + macAddress: _verifiedDeviceAddress!, + ); - // // 调用BLE管理器的关联方法 - // final success = await _bleManager.startDeviceAssociation( - // deviceNamePattern: _verifiedDeviceName!, - // macAddress: _verifiedDeviceAddress!, - // ); - - // if (success) { - // Logger.i(_tag, 'BLE设备关联成功'); - // 关联成功后,转到等待BLE连接状态 - _transition(PairingState.waitingBle); - // } else { - // _setError(ErrorType.associationFailed, - // 'bleDeviceAssociationFailed'.tr); //BLE设备关联失败 - // } + if (success) { + Logger.i(_tag, 'BLE设备关联成功'); + //关联成功后,转到等待BLE连接状态 + _transition(PairingState.waitingBle); + } else { + _setError(ErrorType.associationFailed, + 'bleDeviceAssociationFailed'.tr); //BLE设备关联失败 + } + } else { + _transition(PairingState.waitingBle); + } } catch (e) { Logger.e(_tag, 'BLE设备关联异常: $e'); _setError(ErrorType.associationFailed, @@ -803,7 +807,7 @@ class PairingController extends GetxController with WidgetsBindingObserver { } } else { //未绑定设备 - _bleManager.clearPairedDevices(); + _bleManager.clearPairedDevices(); Logger.i(_tag, '验证时BLE状态: $currentBleState'); if (currentBleState == 2) { //已连接 清理 diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt index 21aaba574..2ecdc7b54 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt @@ -396,7 +396,7 @@ object AgentService : CoroutineScope { } context.sendBroadcast(intent) if(currentRecognitionMode == "ble_wakeup"&&_isInterrupt.get()){ - BleService.openEncoder() + BleService.openB1Encoder() } if(isplaymusic){ isplaymusic = false 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 4ea14e8be..62777f006 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 @@ -1667,9 +1667,10 @@ class ChatApiStreamCallback: StreamCallback { // 停止等待音效 agentService.audioPlayer?.stopAwaitSound() } + os_log("onComplete agentService.isInterrupt:%{public}@", log: agentService.logger, type: .info, String(agentService.isInterrupt)) if(agentService.isInterrupt){ BleService.shared.closeCodec() - } + } if speakResponse && reply && broadcast && sessionid == agentService.currsessionId{ agentService.azureTtsHelper?.flushStream(sessionid:sessionid) @@ -1835,7 +1836,7 @@ extension AgentServiceImpl: TtsEventListener { case .playbackCompleted: restartIdleCheck() if(isInterrupt){ - BleService.shared.openEncoder() + BleService.shared.openB1Encoder() } sendEvent(name: "playback_completed", data: ["status": "playback_completed"]) MusicService.shared.restoreVolume() diff --git a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt index cf8cbb834..4752ba199 100644 --- a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt +++ b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt @@ -1287,7 +1287,6 @@ object BleService { Log.i(TAG, "A2DP播放...") return commandSender.sendCommand(BleConst.CMD_PLAY_A2DP.toByte()) } - /** * 打开编码器 * @param channelMode 声道模式 @@ -1296,6 +1295,20 @@ object BleService { fun openEncoder(channelMode: Int = BleConst.AUDIO_CHANNEL_STEREO): Boolean { Log.i(TAG, "打开编码器...") startOpusStreamDecoding(false, 1, 16000, 40) + // val cmdData = byteArrayOf( + // BleConst.CODEC_CONTROL_ENCODE_ON.toByte(), + // channelMode.toByte() + // ) + // return commandSender.sendCommand(BleConst.CMD_CONTROL_CODEC.toByte(), cmdData) + } + /** + * 打开编码器 + * @param channelMode 声道模式 + * @return 是否成功发送命令 + */ + fun openB1Encoder(channelMode: Int = BleConst.AUDIO_CHANNEL_STEREO): Boolean { + Log.i(TAG, "打开B1编码器...") + startOpusStreamDecoding(false, 1, 16000, 40) val cmdData = byteArrayOf( BleConst.CODEC_CONTROL_ENCODE_ON.toByte(), channelMode.toByte() 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 da86e4dca..46dd97445 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 @@ -635,7 +635,25 @@ private var cmdReplyType: UInt8 = 0 //return result return true } - + /// 打开B1编码器 + public func openB1Encoder() -> Bool { + os_log("🎙️ 准备打开编码(设备侧)...", log: logger, type: .info) + // 确保Opus解码器处于活跃状态(如果不是则启动) + if opusProcessor?.isDecoding() != true { + startOpusDecoding() + } + let paramData = Data([BleConst.CODEC_CONTROL_ENCODE_ON, BleConst.AUDIO_CHANNEL_RIGHT]) + let result = sendCommand(BleConst.CMD_CONTROL_CODEC, data: paramData) + + if result { + os_log("✅ 编码器命令发送成功", log: logger, type: .info) + } else { + os_log("❌ 编码器命令发送失败", log: logger, type: .error) + } + + return result + + } /// 打开解码器 func openDecoder() -> Bool { os_log("打开解码...", log: logger, type: .info)