Browse Source

修复ios连接不了问题

weicu
fdp 1 year ago
parent
commit
a0fd8103e1
  1. 36
      lib/modules/pairing/controllers/pairing_controller.dart
  2. 2
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  3. 5
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift
  4. 15
      local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt
  5. 20
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

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

@ -672,9 +672,10 @@ class PairingController extends GetxController with WidgetsBindingObserver {
_verifiedDeviceAddress?.toUpperCase()) { _verifiedDeviceAddress?.toUpperCase()) {
// 已有关联,检查BLE连接状态 // 已有关联,检查BLE连接状态
Logger.i(_tag, '设备已关联,检查BLE连接状态'); Logger.i(_tag, '设备已关联,检查BLE连接状态');
print('fdp--connectble--pairedMacAddress: $pairedMacAddress');
final currentBleState = await _bleManager.getConnectionState(); final currentBleState = await _bleManager.getConnectionState();
Logger.i(_tag, '验证时BLE状态: $currentBleState'); Logger.i(_tag, '验证时BLE状态: $currentBleState');
print('fdp--connectble--currentBleState: $currentBleState');
if (currentBleState == 2) { if (currentBleState == 2) {
// BleConnectionState.connected // BleConnectionState.connected
Logger.i(_tag, 'BLE已连接,直接完成配对'); Logger.i(_tag, 'BLE已连接,直接完成配对');
@ -701,21 +702,24 @@ class PairingController extends GetxController with WidgetsBindingObserver {
} }
// Logger.i(_tag, '开始BLE设备关联: $_verifiedDeviceName:$_verifiedDeviceAddress'); // Logger.i(_tag, '开始BLE设备关联: $_verifiedDeviceName:$_verifiedDeviceAddress');
if (Platform.isIOS) {
// 调用BLE管理器的关联方法
final success = await _bleManager.startDeviceAssociation(
deviceNamePattern: _verifiedDeviceName!,
macAddress: _verifiedDeviceAddress!,
);
// // 调用BLE管理器的关联方法 if (success) {
// final success = await _bleManager.startDeviceAssociation( Logger.i(_tag, 'BLE设备关联成功');
// deviceNamePattern: _verifiedDeviceName!, //关联成功后,转到等待BLE连接状态
// macAddress: _verifiedDeviceAddress!, _transition(PairingState.waitingBle);
// ); } else {
_setError(ErrorType.associationFailed,
// if (success) { 'bleDeviceAssociationFailed'.tr); //BLE设备关联失败
// Logger.i(_tag, 'BLE设备关联成功'); }
// 关联成功后,转到等待BLE连接状态 } else {
_transition(PairingState.waitingBle); _transition(PairingState.waitingBle);
// } else { }
// _setError(ErrorType.associationFailed,
// 'bleDeviceAssociationFailed'.tr); //BLE设备关联失败
// }
} catch (e) { } catch (e) {
Logger.e(_tag, 'BLE设备关联异常: $e'); Logger.e(_tag, 'BLE设备关联异常: $e');
_setError(ErrorType.associationFailed, _setError(ErrorType.associationFailed,
@ -803,7 +807,7 @@ class PairingController extends GetxController with WidgetsBindingObserver {
} }
} else { } else {
//未绑定设备 //未绑定设备
_bleManager.clearPairedDevices(); _bleManager.clearPairedDevices();
Logger.i(_tag, '验证时BLE状态: $currentBleState'); Logger.i(_tag, '验证时BLE状态: $currentBleState');
if (currentBleState == 2) { if (currentBleState == 2) {
//已连接 清理 //已连接 清理

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

@ -396,7 +396,7 @@ object AgentService : CoroutineScope {
} }
context.sendBroadcast(intent) context.sendBroadcast(intent)
if(currentRecognitionMode == "ble_wakeup"&&_isInterrupt.get()){ if(currentRecognitionMode == "ble_wakeup"&&_isInterrupt.get()){
BleService.openEncoder() BleService.openB1Encoder()
} }
if(isplaymusic){ if(isplaymusic){
isplaymusic = false isplaymusic = false

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

@ -1667,9 +1667,10 @@ class ChatApiStreamCallback: StreamCallback {
// 停止等待音效 // 停止等待音效
agentService.audioPlayer?.stopAwaitSound() agentService.audioPlayer?.stopAwaitSound()
} }
os_log("onComplete agentService.isInterrupt:%{public}@", log: agentService.logger, type: .info, String(agentService.isInterrupt))
if(agentService.isInterrupt){ if(agentService.isInterrupt){
BleService.shared.closeCodec() BleService.shared.closeCodec()
} }
if speakResponse && reply && broadcast && sessionid == agentService.currsessionId{ if speakResponse && reply && broadcast && sessionid == agentService.currsessionId{
agentService.azureTtsHelper?.flushStream(sessionid:sessionid) agentService.azureTtsHelper?.flushStream(sessionid:sessionid)
@ -1835,7 +1836,7 @@ extension AgentServiceImpl: TtsEventListener {
case .playbackCompleted: case .playbackCompleted:
restartIdleCheck() restartIdleCheck()
if(isInterrupt){ if(isInterrupt){
BleService.shared.openEncoder() BleService.shared.openB1Encoder()
} }
sendEvent(name: "playback_completed", data: ["status": "playback_completed"]) sendEvent(name: "playback_completed", data: ["status": "playback_completed"])
MusicService.shared.restoreVolume() MusicService.shared.restoreVolume()

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

@ -1287,7 +1287,6 @@ object BleService {
Log.i(TAG, "A2DP播放...") Log.i(TAG, "A2DP播放...")
return commandSender.sendCommand(BleConst.CMD_PLAY_A2DP.toByte()) return commandSender.sendCommand(BleConst.CMD_PLAY_A2DP.toByte())
} }
/** /**
* 打开编码器 * 打开编码器
* @param channelMode 声道模式 * @param channelMode 声道模式
@ -1296,6 +1295,20 @@ object BleService {
fun openEncoder(channelMode: Int = BleConst.AUDIO_CHANNEL_STEREO): Boolean { fun openEncoder(channelMode: Int = BleConst.AUDIO_CHANNEL_STEREO): Boolean {
Log.i(TAG, "打开编码器...") Log.i(TAG, "打开编码器...")
startOpusStreamDecoding(false, 1, 16000, 40) 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( val cmdData = byteArrayOf(
BleConst.CODEC_CONTROL_ENCODE_ON.toByte(), BleConst.CODEC_CONTROL_ENCODE_ON.toByte(),
channelMode.toByte() channelMode.toByte()

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

@ -635,7 +635,25 @@ private var cmdReplyType: UInt8 = 0
//return result //return result
return true 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 { func openDecoder() -> Bool {
os_log("打开解码...", log: logger, type: .info) os_log("打开解码...", log: logger, type: .info)

Loading…
Cancel
Save