232 lines
8.0 KiB
Python
232 lines
8.0 KiB
Python
"""WiFi ADB 局域网扫描对话框。
|
|
|
|
从 ``gui/gui_app.py`` 中抽出。原本是 ``main()`` 内的闭包 ``open_scan_dialog``,
|
|
其签名已经是显式参数形式,所以迁移到模块顶层只需要把 import 提上来即可。
|
|
|
|
被 ``open_wifi_adb_dialog`` 通过 "扫描局域网" 按钮调用。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
import tkinter as tk
|
|
from tkinter import ttk
|
|
from tkinter.constants import BOTH, LEFT, RIGHT, W, X
|
|
from typing import Callable
|
|
|
|
from gui.gui_core_pkg import (
|
|
discover_adb_devices,
|
|
get_local_subnet,
|
|
parse_port_spec,
|
|
)
|
|
|
|
|
|
def open_scan_dialog(
|
|
parent: tk.Misc,
|
|
adb_exe_local: str,
|
|
ip_var: tk.StringVar,
|
|
on_line_local: Callable[[str], None],
|
|
refresh_cb: Callable[[], None],
|
|
) -> None:
|
|
"""打开"扫描局域网"对话框:自动推子网或手动输入 → 后台扫描 → 列表展示。"""
|
|
scan_dlg = tk.Toplevel(parent)
|
|
scan_dlg.title("扫描局域网 ADB 设备")
|
|
scan_dlg.geometry("640x440")
|
|
scan_dlg.minsize(520, 360)
|
|
scan_dlg.transient(parent)
|
|
|
|
# 顶部:子网输入 + 自动推按钮 + 开始扫描
|
|
top_frame = ttk.LabelFrame(scan_dlg, text="扫描范围", padding=10)
|
|
top_frame.pack(fill=X, padx=10, pady=10)
|
|
|
|
ttk.Label(top_frame, text="子网/范围:").grid(row=0, column=0, sticky=W)
|
|
subnet_var = tk.StringVar(value=get_local_subnet() or "")
|
|
subnet_entry = ttk.Entry(top_frame, textvariable=subnet_var, width=32)
|
|
subnet_entry.grid(row=0, column=1, padx=(6, 6), sticky="we")
|
|
top_frame.grid_columnconfigure(1, weight=1)
|
|
|
|
def auto_fill():
|
|
sub = get_local_subnet()
|
|
if sub:
|
|
subnet_var.set(sub)
|
|
on_line_local(f"[扫描] 自动推导子网: {sub}")
|
|
else:
|
|
on_line_local("[扫描] 无法推导本机子网,请手动输入")
|
|
|
|
ttk.Button(top_frame, text="自动推", command=auto_fill).grid(
|
|
row=0, column=2, padx=(0, 6)
|
|
)
|
|
scan_btn = ttk.Button(top_frame, text="开始扫描")
|
|
scan_btn.grid(row=0, column=3)
|
|
|
|
# 第二行:端口规格
|
|
ttk.Label(top_frame, text="端口:").grid(row=1, column=0, sticky=W, pady=(6, 0))
|
|
scan_port_var = tk.StringVar(value="")
|
|
ttk.Entry(top_frame, textvariable=scan_port_var, width=32).grid(
|
|
row=1, column=1, columnspan=3, padx=(6, 0), pady=(6, 0), sticky="we"
|
|
)
|
|
|
|
ttk.Label(
|
|
top_frame,
|
|
text="子网示例: 192.168.5.0/24 | 192.168.5.1-100 | 192.168.5.5",
|
|
font=("Arial", 8),
|
|
foreground="gray",
|
|
).grid(row=2, column=0, columnspan=4, sticky=W, pady=(4, 0))
|
|
ttk.Label(
|
|
top_frame,
|
|
text="端口留空=自动轻量扫 5555/上次随机端口/常见无线调试端口;手动示例: 5555 | 42047",
|
|
font=("Arial", 8),
|
|
foreground="gray",
|
|
).grid(row=3, column=0, columnspan=4, sticky=W, pady=(2, 0))
|
|
|
|
# 中间:进度条 + 状态
|
|
progress_frame = ttk.Frame(scan_dlg)
|
|
progress_frame.pack(fill=X, padx=10)
|
|
progress_var = tk.DoubleVar(value=0.0)
|
|
progress_bar = ttk.Progressbar(
|
|
progress_frame, mode="determinate", variable=progress_var, maximum=100
|
|
)
|
|
progress_bar.pack(fill=X, pady=(0, 4))
|
|
status_var = tk.StringVar(value="待扫描")
|
|
ttk.Label(progress_frame, textvariable=status_var, font=("Arial", 9)).pack(
|
|
anchor=W
|
|
)
|
|
|
|
# 列表:IP / 状态 / 型号 / 备注
|
|
list_frame = ttk.LabelFrame(scan_dlg, text="发现的设备", padding=10)
|
|
list_frame.pack(fill=BOTH, expand=True, padx=10, pady=(8, 0))
|
|
|
|
cols = ("target", "state", "model", "message")
|
|
tree = ttk.Treeview(
|
|
list_frame, columns=cols, show="headings", height=8
|
|
)
|
|
tree.heading("target", text="IP:Port")
|
|
tree.heading("state", text="状态")
|
|
tree.heading("model", text="设备型号")
|
|
tree.heading("message", text="adb connect 输出")
|
|
tree.column("target", width=140, anchor=W)
|
|
tree.column("state", width=80, anchor=W)
|
|
tree.column("model", width=100, anchor=W)
|
|
tree.column("message", width=240, anchor=W)
|
|
tree.pack(side=LEFT, fill=BOTH, expand=True)
|
|
sb = ttk.Scrollbar(list_frame, orient="vertical", command=tree.yview)
|
|
sb.pack(side=LEFT, fill="y")
|
|
tree.configure(yscrollcommand=sb.set)
|
|
|
|
# 底部按钮
|
|
bottom_frame = ttk.Frame(scan_dlg)
|
|
bottom_frame.pack(fill=X, padx=10, pady=10)
|
|
|
|
def use_selected():
|
|
sel = tree.selection()
|
|
if not sel:
|
|
on_line_local("[扫描] 请先在列表中选中一个设备")
|
|
return
|
|
item = tree.item(sel[0])
|
|
target = item["values"][0]
|
|
state = item["values"][1]
|
|
if state == "unauthorized":
|
|
on_line_local(
|
|
f"[扫描] {target} 未授权,请先在手机上确认 ADB 调试授权再选"
|
|
)
|
|
return
|
|
# 把 IP 部分填回主对话框
|
|
ip_part = str(target).split(":")[0]
|
|
ip_var.set(ip_part)
|
|
on_line_local(f"[扫描] 已选中 {target},可在主对话框点'连接'或它已在 adb devices 中")
|
|
try:
|
|
refresh_cb()
|
|
except Exception:
|
|
pass
|
|
scan_dlg.destroy()
|
|
|
|
use_btn = ttk.Button(
|
|
bottom_frame, text="使用选中设备", command=use_selected, state="disabled"
|
|
)
|
|
use_btn.pack(side=LEFT)
|
|
ttk.Button(bottom_frame, text="关闭", command=scan_dlg.destroy).pack(
|
|
side=RIGHT
|
|
)
|
|
|
|
def on_select(_event=None):
|
|
sel = tree.selection()
|
|
use_btn.config(state="normal" if sel else "disabled")
|
|
|
|
tree.bind("<<TreeviewSelect>>", on_select)
|
|
|
|
# 扫描线程
|
|
scanning = {"flag": False}
|
|
|
|
def do_scan():
|
|
if scanning["flag"]:
|
|
on_line_local("[扫描] 上次扫描尚未完成,请稍候")
|
|
return
|
|
spec = subnet_var.get().strip()
|
|
if not spec:
|
|
on_line_local("[扫描] 请输入子网/IP 范围")
|
|
return
|
|
scanning["flag"] = True
|
|
scan_btn.config(state="disabled")
|
|
tree.delete(*tree.get_children())
|
|
progress_var.set(0)
|
|
|
|
def on_progress(done, total):
|
|
try:
|
|
pct = (done * 100.0) / max(total, 1)
|
|
progress_var.set(pct)
|
|
status_var.set(f"端口扫描 {done}/{total}")
|
|
scan_dlg.update_idletasks()
|
|
except Exception:
|
|
pass
|
|
|
|
def on_phase(text):
|
|
try:
|
|
status_var.set(text)
|
|
scan_dlg.update_idletasks()
|
|
except Exception:
|
|
pass
|
|
|
|
def worker():
|
|
try:
|
|
port_list = parse_port_spec(scan_port_var.get().strip())
|
|
devs = discover_adb_devices(
|
|
adb_exe_local,
|
|
subnet_spec=spec,
|
|
ports=None if not scan_port_var.get().strip() else port_list,
|
|
on_progress=on_progress,
|
|
on_phase=on_phase,
|
|
)
|
|
except Exception as e:
|
|
on_line_local(f"[扫描] 异常: {e}")
|
|
devs = []
|
|
|
|
# 回主线程更新 UI
|
|
def render():
|
|
for d in devs:
|
|
tree.insert(
|
|
"",
|
|
"end",
|
|
values=(d.target, d.adb_state, d.model, d.message[:120]),
|
|
)
|
|
if devs:
|
|
status_var.set(f"扫描完成,发现 {len(devs)} 台设备")
|
|
on_line_local(
|
|
f"[扫描] 完成:{len(devs)} 台候选 — "
|
|
+ ", ".join(f"{d.target}({d.adb_state})" for d in devs[:5])
|
|
)
|
|
else:
|
|
status_var.set("扫描完成,未发现设备")
|
|
on_line_local("[扫描] 未发现 WiFi ADB 设备")
|
|
progress_var.set(100)
|
|
scan_btn.config(state="normal")
|
|
scanning["flag"] = False
|
|
|
|
try:
|
|
scan_dlg.after(0, render)
|
|
except Exception:
|
|
pass
|
|
|
|
threading.Thread(target=worker, daemon=True).start()
|
|
|
|
scan_btn.config(command=do_scan)
|