From 48424a68c5e7beb8a1df56284c53c677af40b944 Mon Sep 17 00:00:00 2001 From: Loong <40141251+wangl-cc@users.noreply.github.com> Date: Sat, 23 May 2026 16:24:49 +0100 Subject: [PATCH] fix: refactor ADB connection logic to only connect when needed (#15300) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化 ADB 连接处理逻辑,避免不必要的连接尝试,并在连接不需要或无效时改进错误报告。 --- src/MaaCore/Controller/AdbController.cpp | 59 +++++++++++++----------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/src/MaaCore/Controller/AdbController.cpp b/src/MaaCore/Controller/AdbController.cpp index db432bfe8b..6e3745ee14 100644 --- a/src/MaaCore/Controller/AdbController.cpp +++ b/src/MaaCore/Controller/AdbController.cpp @@ -912,38 +912,41 @@ bool asst::AdbController::connect(const std::string& adb_path, const std::string } } - // 如果不包含 `:` 且需要连接,connect 命令也不会成功 - if (address.find(':') == std::string::npos && need_connect) { - json::value info = get_info_json() | json::object { - { "what", "ConnectFailed" }, - { "why", "Address does not contain ':' and no devices found" }, - }; - callback(AsstMsg::ConnectionInfo, info); - return false; - } - - // TODO: adb lite server 尚未实现,第一次连接需要执行一次 adb.exe 启动 daemon + // 设置配置 connect、release 命令,即使这里不连接,后续也会需要用到 m_adb.connect = m_conn_ctx.replace_cmd(adb_cfg.connect); m_adb.release = m_conn_ctx.replace_cmd(adb_cfg.release); - auto connect_ret = call_command(m_adb.connect, 60LL * 1000, false /* adb 连接时不允许重试 */); - bool is_connect_success = false; - if (connect_ret) { - auto& connect_str = connect_ret.value(); - // 检查连接字符串是否包含 "connected" - is_connect_success = connect_str.find("connected") != std::string::npos; - // NOTE:这玩意啥都没干,有什么用吗? - if (connect_str.find("daemon started successfully") != std::string::npos && - connect_str.find("daemon still not running") == std::string::npos) { + if (need_connect) { + // 如果不包含 `:` 且需要连接,connect 命令也不会成功 + if (address.find(':') == std::string::npos) { + json::value info = get_info_json() | json::object { + { "what", "ConnectFailed" }, + { "why", "Cannot connect: address appears to be serial number but device not found" }, + }; + callback(AsstMsg::ConnectionInfo, info); + return false; } - } - if (!is_connect_success && need_connect) { - json::value info = get_info_json() | json::object { - { "what", "ConnectFailed" }, - { "why", "Connection command failed to exec" }, - }; - callback(AsstMsg::ConnectionInfo, info); - return false; + auto connect_ret = call_command(m_adb.connect, 60LL * 1000, false /* adb 连接时不允许重试 */); + if (connect_ret) { + auto& connect_str = connect_ret.value(); + // 检查连接字符串是否包含 "connected" + if (connect_str.find("connected") == std::string::npos) { + json::value info = get_info_json() | json::object { + { "what", "ConnectFailed" }, + { "why", "Connection command did not report \"connected\"" }, + }; + callback(AsstMsg::ConnectionInfo, info); + return false; + } + } + else { + json::value info = get_info_json() | json::object { + { "what", "ConnectFailed" }, + { "why", "Connection command failed to exec" }, + }; + callback(AsstMsg::ConnectionInfo, info); + return false; + } } }