fix: refactor ADB connection logic to only connect when needed (#15300)

优化 ADB 连接处理逻辑,避免不必要的连接尝试,并在连接不需要或无效时改进错误报告。
This commit is contained in:
Loong
2026-05-23 16:24:49 +01:00
committed by GitHub
parent c14685311b
commit 48424a68c5

View File

@@ -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;
}
}
}