Browse Source

feat(ble_service): 添加AI单次对话指令支持

实现BLE服务对AI单次对话指令的接收和处理,包括开始和结束对话的指令定义、回调接口和事件分发机制
newdev_shunjiawei
liwei1dao 9 months ago
parent
commit
c5000cb08c
  1. 24
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleAgent.kt
  2. 9
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift
  3. 20
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureSpeechPlugin.kt
  4. 8
      local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureSpeechPlugin.swift
  5. 19
      local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleCommandSender.kt
  6. 6
      local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleConst.kt
  7. 50
      local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt
  8. 10
      local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleServicePlugin.kt
  9. 5
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleConst.swift
  10. 27
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift
  11. 20
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift
  12. 9
      local_plugins/ota/android/src/main/kotlin/com/example/ota/OtaPlugin.kt

24
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleAgent.kt

@ -80,7 +80,16 @@ object BleAgent : BleService.Callback, AgentServiceListener {
AgentService.interruptCurrentResponse()
return AgentService.startRecognition(true, "ble_wakeup")
}
//开始AI单次对话
fun startSingleDialog(): Boolean {
if (!isInitialized) {
Log.e(TAG, "语音代理未初始化")
return false
}
Log.d(TAG, "手动启动AI单次对话")
AgentService.interruptCurrentResponse()
return AgentService.startRecognition(true, "push_to_talk")
}
/**
* 停止语音识别
*/
@ -171,6 +180,19 @@ object BleAgent : BleService.Callback, AgentServiceListener {
//stopListening()
startListening()
}
/**
* 处理开始 AI 单次对话
*/
override fun onStartSingleDialogReceived() {
Log.d(TAG, "liwei-------------- 收到开始 AI 单次对话指令")
startSingleDialog()
}
override fun onEndSingleDialogReceived() {
Log.d(TAG, "liwei-------------- 收到结束 AI 单次对话指令")
// 停止语音识别
stopListening()
}
override fun onDeviceInfoReceived(infoType: Int, infoData: Map<String, Any>) {
// 不处理设备信息

9
local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift

@ -2025,9 +2025,16 @@ extension AgentServiceImpl: BleService.Callback {
func onWakeupSignalReceived() {
startRecognition(useBle: true, mode: "ble_wakeup")
}
// 处理开始 AI 单次对话
func onStartSingleDialogReceived() {
startRecognition(useBle: true, mode: "push_to_talk")
}
// 处理结束 AI 单次对话
func onEndSingleDialogReceived() {
stopRecognition()
}
func onDeviceInfoReceived(infoType: Int, infoData: [String: Any]) {
}

20
local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureSpeechPlugin.kt

@ -1229,6 +1229,26 @@ class AzureSpeechPlugin : BleService.Callback, FlutterPlugin {
override fun onWakeupSignalReceived() {
}
/**
* 开始 AI 单次对话回调
* @throws Exception 无特殊异常抛出
* @return 无返回值
* @param 无参数
*/
override fun onStartSingleDialogReceived() {
// 最小实现:当前插件不处理开始单次对话事件
}
/**
* 结束 AI 单次对话回调
* @throws Exception 无特殊异常抛出
* @return 无返回值
* @param 无参数
*/
override fun onEndSingleDialogReceived() {
// 最小实现:当前插件不处理结束单次对话事件
}
override fun onDeviceInfoReceived(infoType: Int, infoData: Map<String, Any>) {
// 不处理设备信息

8
local_plugins/azure_speech/ios/azure_speech/Sources/azure_speech/AzureSpeechPlugin.swift

@ -1150,6 +1150,14 @@ extension AzureSpeechPlugin: BleService.Callback {
/// 唤醒信号接收回调
public func onWakeupSignalReceived() {
}
// 处理开始 AI 单次对话
public func onStartSingleDialogReceived() {
}
// 处理结束 AI 单次对话
public func onEndSingleDialogReceived() {
}
/// 设备信息接收回调

19
local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleCommandSender.kt

@ -84,7 +84,16 @@ class BleCommandSender {
* 通知唤醒信号接收
*/
fun notifyWakeupSignalReceived()
/**
* 通知开始 AI 单次对话
*/
fun notifyStartSingleDialogReceived()
/**
* 通知结束 AI 单次对话
*/
fun notifyEndSingleDialogReceived()
}
private var callback: CommandSenderCallback? = null
@ -546,6 +555,16 @@ class BleCommandSender {
callback?.notifyWakeupSignalReceived()
}
BleConst.CMD_START_SINGLE_DIALOG -> {
Log.i(TAG, "收到开始 AI 单次对话指令")
// 接收到开始 AI 单次对话信号,打开编码器 (设备侧)
callback?.notifyStartSingleDialogReceived()
}
BleConst.CMD_END_SINGLE_DIALOG -> {
Log.i(TAG, "收到结束 AI 单次对话指令")
// 接收到结束 AI 单次对话信号,关闭编码器 (设备侧)
callback?.notifyEndSingleDialogReceived()
}
// 可以在这里添加其他主动上报类型的处理
else -> {
// 对于未知上报类型,返回原始数据

6
local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleConst.kt

@ -88,6 +88,12 @@ object BleConst {
const val CMD_VOLUME_DOWN = 0x07
/** 获取唤醒词版本 */
const val CMD_GET_WAKEUP_VERSION = 0x08
/** 开始 AI 单次对话 0x09 */
const val CMD_START_SINGLE_DIALOG = 0x09
/**结束 Al 单次对话 0x10 */
const val CMD_END_SINGLE_DIALOG = 0x10
/** 唤醒指令 */
const val CMD_WAKE_UP = 0x11

50
local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt

@ -53,6 +53,12 @@ object BleService {
// 唤醒信号相关回调
fun onWakeupSignalReceived()
// 开始 AI 单次对话相关回调
fun onStartSingleDialogReceived()
// 结束 AI 单次对话相关回调
fun onEndSingleDialogReceived()
// 设备信息相关回调 - 统一回调接口
fun onDeviceInfoReceived(infoType: Int, infoData: Map<String, Any>)
@ -338,7 +344,21 @@ object BleService {
this@BleService.notifyWakeupSignalReceived()
}
/**
* 通知开始 AI 单次对话
*/
override fun notifyStartSingleDialogReceived() {
// 使用外部类的方法来分发开始 AI 单次对话信号,避免无限递归
this@BleService.notifyStartSingleDialogReceived()
}
/**
* 通知结束 AI 单次对话
*/
override fun notifyEndSingleDialogReceived() {
// 使用外部类的方法来分发结束 AI 单次对话信号,避免无限递归
this@BleService.notifyEndSingleDialogReceived()
}
})
// 初始化唤醒词升级指令发送器
@ -1852,7 +1872,33 @@ object BleService {
}
}
}
/**
* 向所有回调监听器分发开始 AI 单次对话
*/
private fun notifyStartSingleDialogReceived() {
for (callback in callbacks) {
try {
callback.onStartSingleDialogReceived()
} catch (e: Exception) {
Log.e(TAG, "分发开始 AI 单次对话回调异常", e)
}
}
}
/**
* 向所有回调监听器分发结束 AI 单次对话
*/
private fun notifyEndSingleDialogReceived() {
for (callback in callbacks) {
try {
callback.onEndSingleDialogReceived()
} catch (e: Exception) {
Log.e(TAG, "分发结束 AI 单次对话回调异常", e)
}
}
}
/**
* 向所有回调监听器分发设备信息
*/

10
local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleServicePlugin.kt

@ -389,6 +389,16 @@ class BleServicePlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
// 将唤醒事件发送到Flutter
sendEvent(statusEventSink, mapOf("type" to "wakeup"), "发送唤醒信号异常")
}
// 处理开始 AI 单次对话
override fun onStartSingleDialogReceived() {
// 将开始对话事件发送到Flutter
sendEvent(statusEventSink, mapOf("type" to "startSingleDialog"), "发送开始对话异常")
}
// 处理结束 AI 单次对话
override fun onEndSingleDialogReceived() {
// 将结束对话事件发送到Flutter
sendEvent(statusEventSink, mapOf("type" to "endSingleDialog"), "发送结束对话异常")
}
override fun onDeviceInfoReceived(infoType: Int, infoData: Map<String, Any>) {
// 设备信息通过状态通道发送

5
local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleConst.swift

@ -85,6 +85,11 @@ class BleConst {
/** A2DP播放 */
static let CMD_PLAY_A2DP: UInt8 = 0x08
/*开始 AI 单次对话4 90x09*/
static let CMD_START_SINGLE_DIALOG: UInt8 = 0x09
/*结束 Al 单次对话 0x10*/
static let CMD_END_SINGLE_DIALOG: UInt8 = 0x10
/** 唤醒指令 */
static let CMD_WAKE_UP: UInt8 = 0x11

27
local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

@ -33,6 +33,12 @@ public class BleService: NSObject {
// 唤醒信号回调
func onWakeupSignalReceived()
// 开始 AI 单次对话回调
func onStartSingleDialogReceived()
// 结束 AI 单次对话回调
func onEndSingleDialogReceived()
// 设备信息相关回调
func onDeviceInfoReceived(infoType: Int, infoData: [String: Any])
}
@ -592,6 +598,20 @@ private var cmdReplyType: UInt8 = 0
}
}
/// 通知所有回调接收到开始 AI 单次对话信号
private func notifyStartSingleDialogReceived() {
for callback in callbacks {
callback.onStartSingleDialogReceived()
}
}
/// 通知所有回调接收到结束 AI 单次对话信号
private func notifyEndSingleDialogReceived() {
for callback in callbacks {
callback.onEndSingleDialogReceived()
}
}
/// 通知所有回调接收到设备信息
private func notifyDeviceInfoReceived(infoType: Int, infoData: [String: Any]) {
for callback in callbacks {
@ -1219,7 +1239,12 @@ private var cmdReplyType: UInt8 = 0
notifyWakeupSignalReceived()
case BleConst.CMD_START_SINGLE_DIALOG:
os_log("📢 收到开始 AI 单次对话指令", log: logger, type: .info)
notifyStartSingleDialogReceived()
case BleConst.CMD_END_SINGLE_DIALOG:
os_log("📢 收到结束 AI 单次对话指令", log: logger, type: .info)
notifyEndSingleDialogReceived()
default:
// 对于未知上报类型,返回原始数据
let resultMap: [String: Any] = [

20
local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift

@ -251,6 +251,26 @@ extension SwiftBleServicePlugin: BleService.Callback {
}
}
}
// 处理开始 AI 单次对话
public func onStartSingleDialogReceived() {
if let eventSink = statusEventSink {
DispatchQueue.main.async {
eventSink([
"type": "startSingleDialog"
])
}
}
}
// 处理结束 AI 单次对话
public func onEndSingleDialogReceived() {
if let eventSink = statusEventSink {
DispatchQueue.main.async {
eventSink([
"type": "endSingleDialog"
])
}
}
}
public func onDeviceInfoReceived(infoType: Int, infoData: [String: Any]) {
if let eventSink = statusEventSink {

9
local_plugins/ota/android/src/main/kotlin/com/example/ota/OtaPlugin.kt

@ -390,7 +390,14 @@ class OtaPlugin : BleService.Callback, FlutterPlugin, MethodCallHandler {
override fun onWakeupSignalReceived() {
}
// 处理开始 AI 单次对话
override fun onStartSingleDialogReceived() {
// 不处理开始对话
}
// 处理结束 AI 单次对话
override fun onEndSingleDialogReceived() {
// 不处理结束对话
}
override fun onDeviceInfoReceived(infoType: Int, infoData: Map<String, Any>) {
// 不处理设备信息
}

Loading…
Cancel
Save