From 731646db6ee5c72a4ac5af530de51da96ef1bc56 Mon Sep 17 00:00:00 2001 From: wolfplus Date: Wed, 4 Jun 2025 20:07:12 +0100 Subject: [PATCH] add --- .../controllers/pairing_controller.dart | 26 +++++--- .../agent_service/AgentService.kt | 2 +- .../azure_speech/AzureAsrHelper.kt | 8 ++- .../azure_speech/AzureTtsHelper.kt | 3 +- .../ble_service/BleCompanionManager.kt | 63 ++++++++++++++----- 5 files changed, 72 insertions(+), 30 deletions(-) diff --git a/lib/modules/pairing/controllers/pairing_controller.dart b/lib/modules/pairing/controllers/pairing_controller.dart index 774c73ade..eaccfe86a 100644 --- a/lib/modules/pairing/controllers/pairing_controller.dart +++ b/lib/modules/pairing/controllers/pairing_controller.dart @@ -358,7 +358,22 @@ class PairingController extends GetxController { ); if (success) { - Logger.i(_tag, 'BLE配对启动成功,等待连接完成...'); + Logger.i(_tag, 'BLE配对启动成功,立即注册后台扫描'); + + // 配对启动成功后立即注册后台扫描 + try { + // Android平台注册后台扫描 + if (Platform.isAndroid) { + await BleManager.to.registerBackgroundScan( + macAddress: _currentClassicDevice!.address); + Logger.i(_tag, '已注册后台扫描'); + } + } catch (e) { + Logger.e(_tag, '注册后台扫描失败: $e'); + // 注册失败不影响配对流程继续 + } + + Logger.i(_tag, '等待BLE连接完成...'); // 配对结果将通过BLE状态监听回调处理 } else { Logger.e(_tag, 'BLE配对启动失败'); @@ -427,15 +442,6 @@ class PairingController extends GetxController { statusDescription.value = '配对成功!'; try { - if (_currentClassicDevice != null) { - // Android平台注册后台扫描 - if (Platform.isAndroid) { - await BleManager.to.registerBackgroundScan( - macAddress: _currentClassicDevice!.address); - Logger.i(_tag, '已注册后台扫描'); - } - } - // 启动BLE后台服务 await BleManager.to.startAgentService(); Logger.i(_tag, 'BLE后台服务已启动'); 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 5b6023e3e..94eae0890 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 @@ -635,7 +635,7 @@ object AgentService : CoroutineScope { // 先添加系统提示词 if (systemPrompt.isNotEmpty()) { messagesWithSystemPrompt.put(openAIService.createSystemMessage(systemPrompt)) - Log.d(TAG, "系统提示词: ${systemPrompt}") + // Log.d(TAG, "系统提示词: ${systemPrompt}") } // 再添加历史消息 diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt index 4ed987ea4..31a09c506 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt @@ -122,7 +122,13 @@ class AzureAsrHelper(private val context: Context) { // 设置指定的识别语言 speechRecognitionLanguage = currentLanguage } - // SetProperty(PropertyId.Speech_SegmentationStrategy, "Semantic"); + + // 设置静音超时时间(毫秒) + setProperty("SpeechServiceConnection_EndSilenceTimeoutMs", "800") + setProperty("Speech_SegmentationSilenceTimeoutMs", "800") + + // 设置分段策略为时间模式 + setProperty("Speech_SegmentationStrategy", "Time") } // 创建识别器 diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt index 72f2a2d89..7add03335 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt @@ -460,8 +460,9 @@ class AzureTtsHelper(private val context: Context) : ITtsService { if (!isInitialized) return false try { - streamBuffer.clear() synthesizer?.StopSpeakingAsync() + streamBuffer.clear() + isSpeaking = false return true } catch (e: Exception) { diff --git a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleCompanionManager.kt b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleCompanionManager.kt index 9cb132e04..ecf154a17 100644 --- a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleCompanionManager.kt +++ b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleCompanionManager.kt @@ -43,10 +43,12 @@ class BleCompanionManager(private val context: Context) { // 用于验证的临时MAC地址 private var pendingMacAddress: String? = null - // 获取CompanionDeviceManager服务 - private val companionDeviceManager: CompanionDeviceManager? by lazy { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager + /** + * 获取CompanionDeviceManager服务(使用Activity上下文) + */ + private fun getCompanionDeviceManager(activity: Activity): CompanionDeviceManager? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + activity.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager } else { null } @@ -56,7 +58,7 @@ class BleCompanionManager(private val context: Context) { * 检查是否支持CompanionDeviceManager */ fun isCompanionDeviceManagerSupported(): Boolean { - return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && companionDeviceManager != null + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O } /** @@ -96,6 +98,24 @@ class BleCompanionManager(private val context: Context) { */ @RequiresApi(Build.VERSION_CODES.O) fun getAssociationIds(): List { + // 使用默认上下文获取CompanionDeviceManager + val companionDeviceManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager + } else { + null + } + return getAssociationIds(companionDeviceManager) + } + + /** + * 获取所有关联ID(使用指定的CompanionDeviceManager) + */ + @RequiresApi(Build.VERSION_CODES.O) + private fun getAssociationIds(companionDeviceManager: CompanionDeviceManager?): List { + if (companionDeviceManager == null) { + return emptyList() + } + // 注意: allAssociations 仅在API 33 (Android 13)及以上版本可用 // 在早期版本中,使用associations (返回的是String数组) return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { @@ -115,7 +135,7 @@ class BleCompanionManager(private val context: Context) { } } else { // API 26-32 使用 associations 方法 - companionDeviceManager?.associations?.toList() ?: emptyList() + companionDeviceManager.associations?.toList() ?: emptyList() } } @@ -134,7 +154,9 @@ class BleCompanionManager(private val context: Context) { serviceUuid: String? = null, macAddress: String? = null ): Boolean { - if (!isCompanionDeviceManagerSupported()) { + // 使用Activity上下文获取CompanionDeviceManager + val companionDeviceManager = getCompanionDeviceManager(activity) + if (companionDeviceManager == null) { Log.e(TAG, "设备不支持CompanionDeviceManager") return false } @@ -196,7 +218,7 @@ class BleCompanionManager(private val context: Context) { .build() // 发起关联请求 - companionDeviceManager?.associate( + companionDeviceManager.associate( associationRequest, object : CompanionDeviceManager.Callback() { override fun onDeviceFound(intentSender: IntentSender) { @@ -328,12 +350,12 @@ class BleCompanionManager(private val context: Context) { pendingMacAddress = null // 🎯 立即发起BLE连接 - try { - Log.i(TAG, "配对成功,立即发起BLE连接: $deviceMac") - BleService.connect(deviceMac) - } catch (e: Exception) { - Log.e(TAG, "配对成功后发起BLE连接失败: ${e.message}", e) - } + // try { + // Log.i(TAG, "配对成功,立即发起BLE连接: $deviceMac") + // BleService.connect(deviceMac) + // } catch (e: Exception) { + // Log.e(TAG, "配对成功后发起BLE连接失败: ${e.message}", e) + // } return true } else { @@ -447,19 +469,26 @@ class BleCompanionManager(private val context: Context) { @RequiresApi(Build.VERSION_CODES.O) fun clearAllAssociations() { try { + // 获取CompanionDeviceManager + val companionDeviceManager = context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager + if (companionDeviceManager == null) { + Log.e(TAG, "无法获取CompanionDeviceManager") + return + } + // 清除本地保存的MAC地址 clearPairedMacAddress() // 获取所有关联ID - val associationIds = getAssociationIds() + val associationIds = getAssociationIds(companionDeviceManager) // 遍历解除关联 for (id in associationIds) { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - companionDeviceManager?.disassociate(id) + companionDeviceManager.disassociate(id) } else { - companionDeviceManager?.disassociate(id) + companionDeviceManager.disassociate(id) } Log.i(TAG, "已解除设备关联: $id") } catch (e: Exception) {