feat(scan): WiFi-ADB 扫描支持自定义/多端口(覆盖 Android 11+ 随机端口)

之前只扫 5555。Android 11+ "无线调试" 用 37000-44999 随机端口,
固定的 adb tcpip 也可改端口。新接口让扫描适配任意端口规格。

新增 parse_port_spec:解析端口规格字符串
  - 5555            单端口
  - "5555,5556"     逗号分隔
  - "37000-37010"   范围(最大 1024 个)
  - 混合格式与序列输入
  - 非法/越界 → 退化为默认 5555
  - 单个范围 > 1024 端口直接忽略,避免误扫整个端口段

scan_open_ports 重构:
  - 接受 ports 参数(list[int]),传 ports 时优先于旧 port=int
  - 返回 list[(ip, port)] 而不是 list[ip],按 (IP 数值序, port 升序) 稳定排序

discover_adb_devices 支持 ports 参数,按 (ip, port) 逐个 verify_adb_target

接入 GUI 与 webui:
  - gui_app.py 扫描对话框新增"端口"输入框 + 提示
  - gui_app.py 启动重连:上次端口非 5555 时把它一并扫描
  - web_gui.py /api/wifi/scan 接受 ports;/api/wifi/auto-reconnect 同上次端口逻辑

新增 13 个 parse_port_spec 单测 + 4 个 scan_open_ports 多端口测试 +
2 个 discover_adb_devices 多端口集成测试。
真机验证:6 端口 (5555 + 37000-37005) × 254 IP,~8.5s 扫完,
正确发现 192.168.5.2:5555 (PEPM00)。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-23 01:02:48 +08:00
1 parent 4ab4504425
commit 52b811d0cb
5 files changed
+317 -35

No files matched your search

+27 -3
View File
@@ -419,7 +419,15 @@ def main():
root.after(0, notify_scan_start)
try:
devs = discover_adb_devices(adb_exe)
# 如果上次记的端口非 5555,扫描时把它一起带上(覆盖 Android 11+ 随机端口)
last_port_int = 5555
if ":" in target:
try:
last_port_int = int(target.rsplit(":", 1)[1])
except ValueError:
pass
scan_ports = [5555] if last_port_int == 5555 else [5555, last_port_int]
devs = discover_adb_devices(adb_exe, ports=scan_ports)
except Exception as e:
def render_scan_err():
on_line(f"[启动] 自动扫描失败: {e}")
@@ -640,12 +648,25 @@ def main():
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="5555")
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",
text="子网示例: 192.168.5.0/24 | 192.168.5.1-100 | 192.168.5.5",
font=("Arial", 8),
foreground="gray",
).grid(row=1, column=0, columnspan=4, sticky=W, pady=(4, 0))
).grid(row=2, column=0, columnspan=4, sticky=W, pady=(4, 0))
ttk.Label(
top_frame,
text="端口示例: 5555 | 5555,5556 | 37000-40000 (Android 11+ 无线调试随机端口在 37000-44999)",
font=("Arial", 8),
foreground="gray",
).grid(row=3, column=0, columnspan=4, sticky=W, pady=(2, 0))
# 中间:进度条 + 状态
progress_frame = ttk.Frame(scan_dlg)
@@ -756,9 +777,12 @@ def main():
def worker():
try:
from gui.gui_core_pkg import parse_port_spec
port_list = parse_port_spec(scan_port_var.get().strip())
devs = discover_adb_devices(
adb_exe_local,
subnet_spec=spec,
ports=port_list,
on_progress=on_progress,
on_phase=on_phase,
)