diff --git a/gui/components/wifi_adb_dialog.py b/gui/components/wifi_adb_dialog.py index 9d1ab62..4b3c7e3 100644 --- a/gui/components/wifi_adb_dialog.py +++ b/gui/components/wifi_adb_dialog.py @@ -298,13 +298,14 @@ def open_wifi_adb_dialog( if not device_id: on_line("[WiFi ADB] 请先选择WiFi设备") return - # 从设备ID中提取IP地址 - ip_match = re.search(r"(\d+\.\d+\.\d+\.\d+)", device_id) - if not ip_match: + # WiFi 设备通常是 ip:port。保留端口,避免随机无线调试端口被误断成 5555。 + target_match = re.search(r"(\d+\.\d+\.\d+\.\d+)(?::(\d{1,5}))?", device_id) + if not target_match: on_line("[WiFi ADB] 无法从设备ID中提取IP地址") return - ip = ip_match.group(1) - success, msg = disconnect_wifi_adb(adb_exe, ip, "5555") + ip = target_match.group(1) + port = target_match.group(2) or "5555" + success, msg = disconnect_wifi_adb(adb_exe, ip, port) on_line(f"[WiFi ADB] {msg}") if success: # 如果断开的就是上次记住的设备,清掉记录避免下次启动徒劳重试 @@ -466,7 +467,7 @@ def open_wifi_adb_dialog( ( ("连接", connect_wifi, "primary", 124), ("断开连接", disconnect_wifi, "soft", 132), - ("扫描局域网", lambda: open_scan_dialog(dialog, adb_exe, ip_var, on_line, refresh_device_lists), "soft", 142), + ("扫描局域网", lambda: open_scan_dialog(dialog, adb_exe, ip_var, on_line, refresh_device_lists, port_var), "soft", 142), ), columns=1, ) diff --git a/gui/components/wifi_adb_scan_dialog.py b/gui/components/wifi_adb_scan_dialog.py index f198f35..c876e2d 100644 --- a/gui/components/wifi_adb_scan_dialog.py +++ b/gui/components/wifi_adb_scan_dialog.py @@ -27,6 +27,7 @@ def open_scan_dialog( ip_var: tk.StringVar, on_line_local: Callable[[str], None], refresh_cb: Callable[[], None], + port_var: tk.StringVar | None = None, ) -> None: """打开"扫描局域网"对话框:自动推子网或手动输入 → 后台扫描 → 列表展示。""" scan_dlg = tk.Toplevel(parent) @@ -130,9 +131,12 @@ def open_scan_dialog( f"[扫描] {target} 未授权,请先在手机上确认 ADB 调试授权再选" ) return - # 把 IP 部分填回主对话框 - ip_part = str(target).split(":")[0] + # 把 IP 和端口填回主对话框。Android 11+ 无线调试常用随机端口, + # 丢掉端口会导致用户选中后仍按默认 5555 连接失败。 + ip_part, port_part = split_target_for_fields(target) ip_var.set(ip_part) + if port_var is not None and port_part: + port_var.set(port_part) on_line_local(f"[扫描] 已选中 {target},可在主对话框点'连接'或它已在 adb devices 中") try: refresh_cb() @@ -229,3 +233,16 @@ def open_scan_dialog( threading.Thread(target=worker, daemon=True).start() scan_btn.config(command=do_scan) + + +def split_target_for_fields(target: object) -> tuple[str, str]: + """Return (host, port) for an adb target string, preserving random WiFi ports.""" + text = str(target or "").strip() + if not text: + return "", "" + if ":" not in text: + return text, "" + host, port = text.rsplit(":", 1) + if host and port.isdigit(): + return host, port + return text, "" diff --git a/tests/test_wifi_adb_scan_dialog.py b/tests/test_wifi_adb_scan_dialog.py new file mode 100644 index 0000000..acc99c3 --- /dev/null +++ b/tests/test_wifi_adb_scan_dialog.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +from gui.components.wifi_adb_scan_dialog import split_target_for_fields + + +def test_split_target_for_fields_preserves_random_wireless_port(): + assert split_target_for_fields("192.168.10.105:36493") == ( + "192.168.10.105", + "36493", + ) + + +def test_split_target_for_fields_keeps_plain_ip_unchanged(): + assert split_target_for_fields("192.168.10.105") == ("192.168.10.105", "") +