127 lines
4.2 KiB
Python
127 lines
4.2 KiB
Python
"""主窗口 "扫描 WiFi" 按钮的一键扫描逻辑。
|
|
|
|
从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的两个闭包
|
|
``_on_scan_one_click`` 和 ``_handle_scan_result``,它们紧耦合。
|
|
|
|
依赖通过显式参数注入:扫描按钮、忙碌标志、设备变量、各类回调。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
from typing import Callable, Dict, Sequence
|
|
|
|
from gui.gui_core_pkg import discover_adb_devices, set_last_wifi_target
|
|
|
|
|
|
def _handle_scan_result(
|
|
devs: Sequence,
|
|
*,
|
|
on_line: Callable[[str], None],
|
|
device_var: tk.StringVar,
|
|
refresh_devices: Callable[[], None],
|
|
open_pick_dialog: Callable[[Sequence], None],
|
|
) -> None:
|
|
if not devs:
|
|
on_line("[扫描] 未发现开放 5555 端口的设备(同 WiFi 下设备需先启用 ADB 调试)")
|
|
return
|
|
|
|
connected = [d for d in devs if d.adb_state == "connected"]
|
|
unauthorized = [d for d in devs if d.adb_state == "unauthorized"]
|
|
failed = [d for d in devs if d.adb_state == "failed"]
|
|
|
|
if len(connected) == 1 and not unauthorized:
|
|
d = connected[0]
|
|
refresh_devices()
|
|
device_var.set(d.target)
|
|
# 记住,下次启动自动重连
|
|
try:
|
|
set_last_wifi_target(d.target, d.model or "")
|
|
except Exception:
|
|
pass
|
|
on_line(
|
|
f"[扫描] 自动连接成功: {d.target}"
|
|
+ (f" ({d.model})" if d.model else "")
|
|
+ "(已记住,下次 GUI 启动会自动重连)"
|
|
)
|
|
return
|
|
|
|
if len(connected) == 0 and len(unauthorized) >= 1:
|
|
targets = ", ".join(d.target for d in unauthorized)
|
|
on_line(
|
|
f"[扫描] 发现 {len(unauthorized)} 个未授权设备: {targets}。"
|
|
f"请在手机上确认 ADB 调试授权(勾选'始终允许这台电脑调试'),然后再点'扫描 WiFi'"
|
|
)
|
|
return
|
|
|
|
if len(connected) == 0 and len(unauthorized) == 0 and failed:
|
|
# 端口开放但 adb connect 失败:通常是 adb 服务异常 / 设备处于诡异状态。
|
|
# 把 adb 真实返回拼出来,让用户看到根因,而不是误导地说"请授权"。
|
|
for d in failed:
|
|
msg = (d.message or "").strip().replace("\n", " | ")
|
|
on_line(f"[扫描] {d.target} 连接失败 (state=failed): {msg or '未知错误'}")
|
|
on_line(
|
|
"[扫描] 提示:可在终端跑 `adb kill-server && adb connect <ip:port>` 看真实输出,"
|
|
"或检查是否多个 adb 进程冲突"
|
|
)
|
|
return
|
|
|
|
# 多个候选 → 弹出选择对话框
|
|
open_pick_dialog(devs)
|
|
|
|
|
|
def on_scan_one_click(
|
|
*,
|
|
adb_exe: str,
|
|
scan_btn: ttk.Button,
|
|
scan_busy: Dict[str, bool],
|
|
root: tk.Misc,
|
|
on_line: Callable[[str], None],
|
|
device_var: tk.StringVar,
|
|
refresh_devices: Callable[[], None],
|
|
open_pick_dialog: Callable[[Sequence], None],
|
|
) -> None:
|
|
"""主窗口"扫描 WiFi"按钮:后台扫描 → 按结果数智能处理。
|
|
|
|
- 0 个:日志提示
|
|
- 1 个 connected:自动设为当前设备并刷新
|
|
- 1 个 unauthorized:提示需要手机端授权
|
|
- 多个:弹简单的列表对话框让用户选
|
|
"""
|
|
if scan_busy["flag"]:
|
|
on_line("[扫描] 上次扫描尚未完成,请稍候")
|
|
return
|
|
scan_busy["flag"] = True
|
|
scan_btn.config(state="disabled", text="扫描中...")
|
|
on_line("[扫描] 开始扫描局域网 ADB 设备...")
|
|
|
|
def worker():
|
|
try:
|
|
devs = discover_adb_devices(
|
|
adb_exe,
|
|
on_phase=lambda msg: on_line(f"[扫描] {msg}"),
|
|
)
|
|
except Exception as e:
|
|
on_line(f"[扫描] 异常: {e}")
|
|
devs = []
|
|
|
|
def render():
|
|
scan_busy["flag"] = False
|
|
scan_btn.config(state="normal", text="扫描 WiFi")
|
|
_handle_scan_result(
|
|
devs,
|
|
on_line=on_line,
|
|
device_var=device_var,
|
|
refresh_devices=refresh_devices,
|
|
open_pick_dialog=open_pick_dialog,
|
|
)
|
|
|
|
try:
|
|
root.after(0, render)
|
|
except Exception:
|
|
pass
|
|
|
|
threading.Thread(target=worker, daemon=True).start()
|