wolfplus 1 year ago
parent
commit
cc86ae05c0
  1. 12
      lib/data/services/bluetooth_manager.dart
  2. 30
      lib/modules/pairing/controllers/pairing_controller.dart
  3. 4
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  4. 2
      test.json

12
lib/data/services/bluetooth_manager.dart

@ -29,9 +29,19 @@ class BluetoothManager extends GetxService {
super.onInit();
Logger.info('蓝牙服务初始化');
// 设置蓝牙事件监听
_setupBluetoothListener();
// 主动检查当前已连接的设备和蓝牙状态
Future.delayed(Duration.zero, () async {
Logger.info('检查初始蓝牙连接状态');
// 先检查蓝牙是否开启
await isBluetoothEnabled();
// 然后刷新设备连接
await refreshDeviceConnection();
});
}
/// 释放资源

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

@ -83,7 +83,8 @@ class PairingController extends GetxController {
_setupAllListeners();
// 开始初始状态检查
Future.delayed(Duration.zero, () {
// 延迟稍长时间,确保BluetoothManager有足够时间获取已连接设备
Future.delayed(const Duration(milliseconds: 300), () {
_startInitialCheck();
});
}
@ -263,7 +264,10 @@ class PairingController extends GetxController {
case PairingState.waitingBle:
statusText.value = '正在建立安全连接...';
isLoading.value = true;
// 延迟一下再调用,给BLE管理器时间更新状态
Future.delayed(const Duration(milliseconds: 200), () {
_startBleConnection();
});
break;
case PairingState.completed:
@ -338,14 +342,17 @@ class PairingController extends GetxController {
return;
}
// 3. 检查经典蓝牙连接
// 3. 先刷新设备连接状态,确保获取最新的连接信息
await _bluetoothManager.refreshDeviceConnection();
// 4. 检查经典蓝牙连接
final classicDevice = _bluetoothManager.currentDevice;
if (classicDevice == null) {
_transition(PairingState.waitingClassic);
return;
}
// 4. 如果有设备,进行验证
// 5. 如果有设备,进行验证
_currentClassicDevice = classicDevice;
_transition(PairingState.verifying);
await _verifyDevice(classicDevice);
@ -387,10 +394,15 @@ class PairingController extends GetxController {
Logger.i(_tag, '设备验证通过');
_verifiedDeviceAddress = device.address;
// 检查BLE状态
if (_bleManager.isConnected) {
// 检查BLE状态 - 直接从原生层获取
final currentBleState = await _bleManager.getConnectionState();
Logger.i(_tag, '验证时BLE状态: $currentBleState');
if (currentBleState == 2) { // BleConnectionState.connected
Logger.i(_tag, 'BLE已连接,直接完成配对');
_transition(PairingState.completed);
} else {
Logger.i(_tag, 'BLE未连接,等待BLE连接');
_transition(PairingState.waitingBle);
}
} else {
@ -413,8 +425,12 @@ class PairingController extends GetxController {
}
});
// 检查是否已连接
if (_bleManager.isConnected) {
// 检查是否已连接 - 直接从原生层获取最新状态
final currentBleState = await _bleManager.getConnectionState();
Logger.i(_tag, 'BLE当前连接状态: $currentBleState');
if (currentBleState == 2) { // BleConnectionState.connected
Logger.i(_tag, 'BLE已经连接,直接完成配对');
_bleTimeoutTimer?.cancel();
_transition(PairingState.completed);
return;

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

@ -612,7 +612,7 @@ object AgentService : CoroutineScope {
val responseBuilder = StringBuilder()
var aiMetadata:String = ""
var userMetadata = JSONObject()
var broadcast = false //是否播发
var broadcast = true //是否播发
// 如果有图片,添加图片信息到元数据
if (hasImage) {
userMetadata.put("hasImage", true)
@ -631,7 +631,7 @@ object AgentService : CoroutineScope {
)
val _systemPrompt = Utils.fillTemplate(systemPrompt,location)
messagesWithSystemPrompt.put(createSystemMessage(_systemPrompt))
Log.d(TAG, "liwei--------- 系统提示词: ${_systemPrompt}")
// Log.d(TAG, "liwei--------- 系统提示词: ${_systemPrompt}")
}
// 再添加历史消息

2
test.json

@ -15,7 +15,7 @@ adb shell am broadcast \
adb shell dumpsys package com.yunqiinnovation.deepsound | grep receiver
adb logcat -v time | grep -i BleForegroundService
adb logcat -v time | grep -Ei "BleWakeReceiver|BleMediaService"

Loading…
Cancel
Save