Browse Source

Merge branch '重写ble' of https://github.com/deepcloud2048/deep_voice into 重写ble

weicu
fdp 1 year ago
parent
commit
89d5b9e8b7
  1. 1
      lib/core/translations/language/en_us.dart
  2. 2
      lib/core/translations/language/zh_cn.dart
  3. 33
      lib/data/services/ble_manager.dart
  4. 1
      lib/modules/agent/controllers/agent_controller.dart
  5. 9
      lib/modules/pairing/controllers/pairing_controller.dart
  6. 4
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentService.kt
  7. 1
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt
  8. 2
      local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/BleMediaService.kt
  9. 27
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift
  10. 11
      local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServicePlugin.swift

1
lib/core/translations/language/en_us.dart

@ -540,6 +540,7 @@ const Map<String, String> enUS = {
"loadMoreMessages": "Load more messages...",
"loadFailedClickRetry": "Load failed, click to retry",
"swipeUpForMore": "Swipe up for more",
"callingTool": "I'm helping you, please wait!", // 正在帮您,请稍等!
// 预设提示词
"presetMusic": "Play some music", // Play some music

2
lib/core/translations/language/zh_cn.dart

@ -511,7 +511,7 @@ const Map<String, String> zhCN = {
"loadMoreMessages": "加载更多消息...",
"loadFailedClickRetry": "加载失败,点击重试",
"swipeUpForMore": "向上滑动查看更多",
"callingTool": "正在调用工具!",
"callingTool": "正在帮您,请稍等!",
// 预设提示词
"presetMusic": "来点音乐", // 来点音乐

33
lib/data/services/ble_manager.dart

@ -6,6 +6,7 @@ import 'package:get_storage/get_storage.dart';
import 'package:get/get.dart';
import 'package:ble_service/ble_service.dart';
import 'package:agent_service/agent_service.dart';
import 'package:voitrans/data/models/appconfig_model.dart';
import '../../data/models/appconfig.dart';
import '../../core/utils/logger.dart';
@ -639,6 +640,35 @@ class BleManager extends GetxService {
}
}
String getDeviceName() {
if (deviceInfo.value != null) {
return deviceInfo.value!.name;
}
return "";
}
// 加载TTS偏好设置
Future<bool> _loadTtsInterrupe() async {
try {
if (!_storage.hasData("agent_tts_interrupe")) {
// 获取设备类型以确定检查策略
String deviceName = getDeviceName();
DBProduct? product = AppConfig.getproduct(deviceName);
if (deviceName != "" && product != null) {
// Logger.d('AgentController',
// 'liwe----------product $deviceName ${product.toJson()}');
return !product.iscontinuouschat!;
} else {
return false;
}
} else {
return _storage.read('agent_tts_interrupe') ?? false;
}
} catch (e) {
return false;
}
}
/// 启动BLE服务
Future<bool> startAgentService() async {
try {
@ -668,6 +698,8 @@ class BleManager extends GetxService {
// 从Flutter资源包中加载.mcp.json
final jsonData = AppConfig.mcpConfig();
Logger.info('启动BLE服务,musicServiceType: ${musicServiceType}');
final isTtsInterruptEnabled = await _loadTtsInterrupe();
// 根据APP语言动态配置语音识别语言
List<String> supportedLanguages = [];
try {
@ -737,6 +769,7 @@ class BleManager extends GetxService {
'supportedLanguages': supportedLanguages,
'musicServiceType': musicServiceType,
"navigationMode": navigationMode,
"isttsinterrupt": isTtsInterruptEnabled ? "true" : "false",
"callingTool": "callingTool".tr
};

1
lib/modules/agent/controllers/agent_controller.dart

@ -281,6 +281,7 @@ class AgentController extends GetxController with WidgetsBindingObserver {
isTtsInterruptEnabled.value =
_storage.read('agent_tts_interrupe') ?? false;
}
await AgentService.isInterrupt(isTtsInterruptEnabled.value);
} catch (e) {
// 如果读取失败,使用默认值
Logger.e(TAG, '加载TTS偏好设置失败: $e');

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

@ -807,13 +807,13 @@ class PairingController extends GetxController with WidgetsBindingObserver {
}
} else {
//未绑定设备
_bleManager.clearPairedDevices();
Logger.i(_tag, '验证时BLE状态: $currentBleState');
if (currentBleState == 2) {
//已连接 清理
// BleConnectionState.connected
Logger.i(_tag, 'BLE已连接,断开并清理缓存');
_bleManager.clearPairedDevices();
_bleManager.disconnect();
}
}
@ -944,6 +944,11 @@ class PairingController extends GetxController with WidgetsBindingObserver {
}
}
}
final currentBleState = await _bleManager.getConnectionState();
if (currentBleState == 2) {
_bleManager.clearPairedDevices();
_bleManager.disconnect();
}
await Future.delayed(Duration(seconds: 1));
}
_bleManager.stopScan();

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

@ -203,11 +203,15 @@ object AgentService : CoroutineScope {
serverurl = config["serverurl"]?.toString() ?: "";
usertoken = config["usertoken"]?.toString() ?: "";
callingTool = config["callingTool"]?.toString() ?: "";
// 初始化上下文和服务
this.context = appContext.applicationContext
// 从 SharedPreferences 读取 _isInterrupt 状态
loadIsInterruptFromCache()
if (config["isttsinterrupt"] != null){
_isInterrupt.set(config["isttsinterrupt"]?.toString() == "true")
}
this.chatApiService = ChatApiService(context)
this.chatStorageHelper = ChatStorageHelper(context)

1
local_plugins/agent_service/android/src/main/kotlin/com/yunqiinnovation/agent_service/AgentServicePlugin.kt

@ -77,6 +77,7 @@ class AgentServicePlugin : FlutterPlugin, MethodCallHandler, EventChannel.Stream
config["usertoken"]?.toString()?.let { intent.putExtra("usertoken", it) }
config["musicServiceType"]?.toString()?.let { intent.putExtra("musicServiceType", it) }
config["navigationMode"]?.toString()?.let { intent.putExtra("navigationMode", it) }
config["isttsinterrupt"]?.toString()?.let { intent.putExtra("isttsinterrupt", it) }
config["callingTool"]?.toString()?.let { intent.putExtra("callingTool", it) }

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

@ -571,6 +571,8 @@ private var companionManager: BleCompanionManager? = null
it.getStringExtra(KEY_MUSICSERVICE_TYPE)?.let { value -> config[KEY_MUSICSERVICE_TYPE] = value }
it.getStringExtra(APP_NAVIGATION_MODE)?.let { value -> config[APP_NAVIGATION_MODE] = value }
it.getStringExtra(APP_CALLING_TOOL)?.let { value -> config[APP_CALLING_TOOL] = value }
it.getStringExtra("isttsinterrupt")?.let { value -> config["isttsinterrupt"] = value }
// 提取 supportedLanguages 参数
it.getStringArrayListExtra(KEY_SUPPORTED_LANGUAGES)?.let { languages ->
if (languages.isNotEmpty()) {

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

@ -27,6 +27,10 @@ class AgentServiceImpl: NSObject {
static let shared = AgentServiceImpl()
// UserDefaults 相关常量
private let PREF_NAME = "agent_service_prefs"
private let KEY_IS_INTERRUPT = "is_interrupt"
private var listeners = [AgentServiceListener]()
private let listenersLock = NSLock()
@ -252,6 +256,8 @@ class AgentServiceImpl: NSObject {
if isInitialized {
loadChatHistory()
// 从 UserDefaults 加载 isInterrupt 状态
loadIsInterruptFromCache()
}
LocationService.shared.getCurrentLocation() { location in
// self.locationInfo = location
@ -2238,4 +2244,25 @@ extension AgentServiceImpl: AzureAsrHelper.ContinuousRecognizeCallback {
stopRequestedDuringStart = false
stopIdleCheck()
}
// MARK: - isInterrupt 缓存方法
/**
* 从 UserDefaults 加载 isInterrupt 状态
*/
private func loadIsInterruptFromCache() {
let userDefaults = UserDefaults.standard
let cachedValue = userDefaults.bool(forKey: KEY_IS_INTERRUPT) // 默认值为 false
isInterrupt = cachedValue
os_log("从缓存加载 isInterrupt 状态: %{public}@", log: logger, type: .info, String(isInterrupt))
}
/**
* 保存 isInterrupt 状态到 UserDefaults
*/
func saveIsInterruptToCache(_ isInterrupt: Bool) {
let userDefaults = UserDefaults.standard
userDefaults.set(isInterrupt, forKey: KEY_IS_INTERRUPT)
os_log("保存 isInterrupt 状态到缓存: %{public}@", log: logger, type: .info, String(isInterrupt))
}
}

11
local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServicePlugin.swift

@ -79,8 +79,15 @@ public class AgentServicePlugin: NSObject, FlutterPlugin {
impl.startRecognition(mode: mode)
result(true)
case "isInterrupt":
impl.isInterrupt = !impl.isInterrupt
result(impl.isInterrupt)
if let args = call.arguments as? [String: Any],
let isInterrupt = args["isinterrupt"] as? Bool {
impl.isInterrupt = isInterrupt
// 保存到 UserDefaults
impl.saveIsInterruptToCache(isInterrupt)
result(impl.isInterrupt)
} else {
result(FlutterError(code: "INVALID_ARGUMENTS", message: "isinterrupt 参数缺失或类型错误", details: nil))
}
case "recognizeCallback":
impl.recognizeCallback()

Loading…
Cancel
Save