wolfplus 1 year ago
parent
commit
731646db6e
  1. 26
      lib/modules/pairing/controllers/pairing_controller.dart
  2. 2
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  3. 8
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt
  4. 3
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureTtsHelper.kt
  5. 63
      local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleCompanionManager.kt

26
lib/modules/pairing/controllers/pairing_controller.dart

@ -358,7 +358,22 @@ class PairingController extends GetxController {
); );
if (success) { 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状态监听回调处理 // 配对结果将通过BLE状态监听回调处理
} else { } else {
Logger.e(_tag, 'BLE配对启动失败'); Logger.e(_tag, 'BLE配对启动失败');
@ -427,15 +442,6 @@ class PairingController extends GetxController {
statusDescription.value = '配对成功!'; statusDescription.value = '配对成功!';
try { try {
if (_currentClassicDevice != null) {
// Android平台注册后台扫描
if (Platform.isAndroid) {
await BleManager.to.registerBackgroundScan(
macAddress: _currentClassicDevice!.address);
Logger.i(_tag, '已注册后台扫描');
}
}
// 启动BLE后台服务 // 启动BLE后台服务
await BleManager.to.startAgentService(); await BleManager.to.startAgentService();
Logger.i(_tag, 'BLE后台服务已启动'); Logger.i(_tag, 'BLE后台服务已启动');

2
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt

@ -635,7 +635,7 @@ object AgentService : CoroutineScope {
// 先添加系统提示词 // 先添加系统提示词
if (systemPrompt.isNotEmpty()) { if (systemPrompt.isNotEmpty()) {
messagesWithSystemPrompt.put(openAIService.createSystemMessage(systemPrompt)) messagesWithSystemPrompt.put(openAIService.createSystemMessage(systemPrompt))
Log.d(TAG, "系统提示词: ${systemPrompt}") // Log.d(TAG, "系统提示词: ${systemPrompt}")
} }
// 再添加历史消息 // 再添加历史消息

8
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 speechRecognitionLanguage = currentLanguage
} }
// SetProperty(PropertyId.Speech_SegmentationStrategy, "Semantic");
// 设置静音超时时间(毫秒)
setProperty("SpeechServiceConnection_EndSilenceTimeoutMs", "800")
setProperty("Speech_SegmentationSilenceTimeoutMs", "800")
// 设置分段策略为时间模式
setProperty("Speech_SegmentationStrategy", "Time")
} }
// 创建识别器 // 创建识别器

3
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 if (!isInitialized) return false
try { try {
streamBuffer.clear()
synthesizer?.StopSpeakingAsync() synthesizer?.StopSpeakingAsync()
streamBuffer.clear()
isSpeaking = false isSpeaking = false
return true return true
} catch (e: Exception) { } catch (e: Exception) {

63
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地址 // 用于验证的临时MAC地址
private var pendingMacAddress: String? = null private var pendingMacAddress: String? = null
// 获取CompanionDeviceManager服务 /**
private val companionDeviceManager: CompanionDeviceManager? by lazy { * 获取CompanionDeviceManager服务(使用Activity上下文)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { */
context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager 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 { } else {
null null
} }
@ -56,7 +58,7 @@ class BleCompanionManager(private val context: Context) {
* 检查是否支持CompanionDeviceManager * 检查是否支持CompanionDeviceManager
*/ */
fun isCompanionDeviceManagerSupported(): Boolean { 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) @RequiresApi(Build.VERSION_CODES.O)
fun getAssociationIds(): List<String> { fun getAssociationIds(): List<String> {
// 使用默认上下文获取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<String> {
if (companionDeviceManager == null) {
return emptyList()
}
// 注意: allAssociations 仅在API 33 (Android 13)及以上版本可用 // 注意: allAssociations 仅在API 33 (Android 13)及以上版本可用
// 在早期版本中,使用associations (返回的是String数组) // 在早期版本中,使用associations (返回的是String数组)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
@ -115,7 +135,7 @@ class BleCompanionManager(private val context: Context) {
} }
} else { } else {
// API 26-32 使用 associations 方法 // 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, serviceUuid: String? = null,
macAddress: String? = null macAddress: String? = null
): Boolean { ): Boolean {
if (!isCompanionDeviceManagerSupported()) { // 使用Activity上下文获取CompanionDeviceManager
val companionDeviceManager = getCompanionDeviceManager(activity)
if (companionDeviceManager == null) {
Log.e(TAG, "设备不支持CompanionDeviceManager") Log.e(TAG, "设备不支持CompanionDeviceManager")
return false return false
} }
@ -196,7 +218,7 @@ class BleCompanionManager(private val context: Context) {
.build() .build()
// 发起关联请求 // 发起关联请求
companionDeviceManager?.associate( companionDeviceManager.associate(
associationRequest, associationRequest,
object : CompanionDeviceManager.Callback() { object : CompanionDeviceManager.Callback() {
override fun onDeviceFound(intentSender: IntentSender) { override fun onDeviceFound(intentSender: IntentSender) {
@ -328,12 +350,12 @@ class BleCompanionManager(private val context: Context) {
pendingMacAddress = null pendingMacAddress = null
// 🎯 立即发起BLE连接 // 🎯 立即发起BLE连接
try { // try {
Log.i(TAG, "配对成功,立即发起BLE连接: $deviceMac") // Log.i(TAG, "配对成功,立即发起BLE连接: $deviceMac")
BleService.connect(deviceMac) // BleService.connect(deviceMac)
} catch (e: Exception) { // } catch (e: Exception) {
Log.e(TAG, "配对成功后发起BLE连接失败: ${e.message}", e) // Log.e(TAG, "配对成功后发起BLE连接失败: ${e.message}", e)
} // }
return true return true
} else { } else {
@ -447,19 +469,26 @@ class BleCompanionManager(private val context: Context) {
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
fun clearAllAssociations() { fun clearAllAssociations() {
try { try {
// 获取CompanionDeviceManager
val companionDeviceManager = context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager
if (companionDeviceManager == null) {
Log.e(TAG, "无法获取CompanionDeviceManager")
return
}
// 清除本地保存的MAC地址 // 清除本地保存的MAC地址
clearPairedMacAddress() clearPairedMacAddress()
// 获取所有关联ID // 获取所有关联ID
val associationIds = getAssociationIds() val associationIds = getAssociationIds(companionDeviceManager)
// 遍历解除关联 // 遍历解除关联
for (id in associationIds) { for (id in associationIds) {
try { try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
companionDeviceManager?.disassociate(id) companionDeviceManager.disassociate(id)
} else { } else {
companionDeviceManager?.disassociate(id) companionDeviceManager.disassociate(id)
} }
Log.i(TAG, "已解除设备关联: $id") Log.i(TAG, "已解除设备关联: $id")
} catch (e: Exception) { } catch (e: Exception) {

Loading…
Cancel
Save