diff --git a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart index a59a16289..1e59b45c3 100644 --- a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart @@ -73,10 +73,8 @@ class OtaUpgradeController extends GetxController { } // 检查固件信息有效性 - if (deviceInfo.firmwareVersion.isEmpty - //|| - // deviceInfo.firmwareProductId.isEmpty - ) { + if (deviceInfo.firmwareVersion.isEmpty || + deviceInfo.firmwareProductId.isEmpty) { throw const FormatException('设备固件信息不完整'); } @@ -85,7 +83,7 @@ class OtaUpgradeController extends GetxController { // API请求 final response = await Api.getequipmentinfo({ - "id": "a2", + "id": deviceInfo.firmwareProductId, }); // 处理API响应 diff --git a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleAgent.kt b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleAgent.kt index f362b478d..00e0cdd7e 100644 --- a/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleAgent.kt +++ b/local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleAgent.kt @@ -128,7 +128,7 @@ object BleAgent : BleService.Callback, AgentServiceListener { } override fun onAudioDataReceived(data: ByteArray) { - // FileLogger.d(TAG, "onAudioDataReceived, data: ${data.size}") + FileLogger.d(TAG, "onAudioDataReceived, data: ${data.size}") AgentService.pushAudioData(data) // 可选:处理音频数据 } diff --git a/local_plugins/azure_speech/android/build.gradle.kts b/local_plugins/azure_speech/android/build.gradle.kts index 342b48535..151988920 100644 --- a/local_plugins/azure_speech/android/build.gradle.kts +++ b/local_plugins/azure_speech/android/build.gradle.kts @@ -53,4 +53,5 @@ tasks.withType { dependencies { // 添加Microsoft语音SDK implementation("com.microsoft.cognitiveservices.speech:client-sdk:1.30.0") + implementation(project(":ble_service")) } \ No newline at end of file diff --git a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureSpeechPlugin.kt b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureSpeechPlugin.kt index f149fa2be..327d194d4 100644 --- a/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureSpeechPlugin.kt +++ b/local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureSpeechPlugin.kt @@ -14,9 +14,10 @@ import io.flutter.plugin.common.EventChannel import kotlinx.coroutines.* import kotlin.coroutines.CoroutineContext import com.microsoft.cognitiveservices.speech.audio.* - +import com.yunqiinnovation.ble_service.BleService +import com.yunqiinnovation.ble_service.BleConst /** AzureSpeechPlugin */ -class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { +class AzureSpeechPlugin: FlutterPlugin,BleService.Callback, CoroutineScope { private val tag = "AzureSpeechPlugin" private lateinit var context: Context private val mainHandler = Handler(Looper.getMainLooper()) @@ -37,7 +38,10 @@ class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { private lateinit var ttsEventChannel: EventChannel private var ttsEventSink: EventChannel.EventSink? = null private lateinit var azureTtsHelper: AzureTtsHelper + // 是否已注册BLE回调 + private var isRegisteredToBle = false + // ASR 事件发送方法 private fun sendAsrEvent(event: Map) { if (asrEventSink == null) { @@ -110,6 +114,19 @@ class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { // 初始化Azure语音服务 azureTtsHelper = AzureTtsHelper(context) azureAsrHelper = AzureAsrHelper(context) + + // 2. 初始化BleService并注册回调 + if (BleService.initialize(context)) { + if (BleService.addCallback(this)) { + isRegisteredToBle = true + FileLogger.d(tag, "已注册BLE回调") + } else { + FileLogger.w(tag, "BLE回调注册失败") + } + } else { + FileLogger.e(tag, "BLE服务初始化失败") + + } } // ASR方法处理器 @@ -359,7 +376,12 @@ class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { try { // 取消所有协程 job.cancel() - + // 取消BLE回调注册 + if (isRegisteredToBle) { + BleService.removeCallback(this) + isRegisteredToBle = false + } + // 释放资源 azureTtsHelper.dispose() azureAsrHelper.dispose() @@ -367,4 +389,34 @@ class AzureSpeechPlugin: FlutterPlugin, CoroutineScope { FileLogger.e(tag, "释放资源失败: ${e.message}") } } + + + override fun onScanResult(devices: List>) { + + } + + override fun onConnectionStateChanged(state: Int) { + // 可选:处理连接状态变化 + if (state == BleConst.STATE_CONNECTED) { + FileLogger.d(tag, "Ble设备已连接,准备好语音交互") + } else if (state == BleConst.STATE_DISCONNECTED) { + FileLogger.d(tag, "Ble设备已断开,停止语音服务") + // 设备断开连接,停止语音服务 + // stopListening() + } + + } + + override fun onAudioDataReceived(data: ByteArray) { + FileLogger.d(tag, "AzureSpeechPlugin onAudioDataReceived, data: ${data.size}") + azureAsrHelper.pushAudioData(data) + } + + override fun onWakeupSignalReceived() { + + } + + override fun onDeviceInfoReceived(infoType: Int, infoData: Map) { + + } } \ No newline at end of file diff --git a/local_plugins/ble_service/android/build.gradle.kts b/local_plugins/ble_service/android/build.gradle.kts index 72a6745e6..29deed814 100644 --- a/local_plugins/ble_service/android/build.gradle.kts +++ b/local_plugins/ble_service/android/build.gradle.kts @@ -31,5 +31,4 @@ dependencies { // 使用从本地Maven仓库引用的AAR依赖 implementation("com.jieli.audio:jl_audio_decode:2.0.0") implementation(project(":deepvoice_utils")) - implementation(project(":azure_speech")) } \ No newline at end of file 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 dd6d24917..39261ee63 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 @@ -661,10 +661,10 @@ object BleService { "rawData" to responseData ) - if (responseData.isNotEmpty() && responseData.size >= 2) { - val productId = "0x${(responseData[0].toInt() and 0xFF).toString(16)}${ - (responseData[1].toInt() and 0xFF).toString(16) - }" + if (responseData.isNotEmpty()) { + // 转换为字符串前,确认是否是有效的ASCII字符 + val productId = + responseData.joinToString(".") { (it.toInt() and 0xFF).toString() } resultMap["productId"] = productId FileLogger.i(TAG, "接收到产品ID: $productId") } diff --git a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleServicePlugin.kt b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleServicePlugin.kt index e8ba005df..960852353 100644 --- a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleServicePlugin.kt +++ b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleServicePlugin.kt @@ -185,6 +185,10 @@ class BleServicePlugin : FlutterPlugin, MethodCallHandler, ActivityAware, "getDeviceVersion" -> { val success = BleService.getDeviceVersion() result.success(success) + } + "getProductId" -> { + val success = BleService.getProductId() + result.success(success) } "getBatteryInfo" -> { val success = BleService.getBatteryInfo()