fix: 修复 Wi-Fi ADB 拔线后刷新丢失 (#27)
This commit is contained in:
@@ -8,8 +8,8 @@ from typing import Callable, List, Optional, Sequence
|
||||
|
||||
|
||||
DEFAULT_ADB_TIMEOUT_SECONDS = 5.0
|
||||
DEFAULT_USB_DISCONNECT_TIMEOUT_SECONDS = 60.0
|
||||
DEFAULT_USB_POLL_INTERVAL_SECONDS = 0.5
|
||||
DEFAULT_DEVICE_READY_CHECKS = 8
|
||||
DEFAULT_DEVICE_READY_INTERVAL_SECONDS = 0.25
|
||||
|
||||
|
||||
class AndroidDeviceSearchError(RuntimeError):
|
||||
@@ -40,8 +40,8 @@ class AndroidWifiConversionResult:
|
||||
|
||||
|
||||
CommandRunner = Callable[[Sequence[str], float], subprocess.CompletedProcess]
|
||||
Sleeper = Callable[[float], None]
|
||||
ProgressCallback = Callable[[str], None]
|
||||
Sleeper = Callable[[float], None]
|
||||
|
||||
|
||||
def run_adb_command(
|
||||
@@ -68,22 +68,12 @@ class AndroidDeviceService:
|
||||
self,
|
||||
command_runner: CommandRunner = run_adb_command,
|
||||
timeout_seconds: float = DEFAULT_ADB_TIMEOUT_SECONDS,
|
||||
usb_disconnect_timeout_seconds: float = (
|
||||
DEFAULT_USB_DISCONNECT_TIMEOUT_SECONDS
|
||||
),
|
||||
usb_poll_interval_seconds: float = DEFAULT_USB_POLL_INTERVAL_SECONDS,
|
||||
sleeper: Sleeper = time.sleep,
|
||||
):
|
||||
if timeout_seconds <= 0:
|
||||
raise ValueError("ADB 超时时间必须大于 0")
|
||||
if usb_disconnect_timeout_seconds <= 0:
|
||||
raise ValueError("等待拔出 USB 的超时时间必须大于 0")
|
||||
if usb_poll_interval_seconds <= 0:
|
||||
raise ValueError("USB 检查间隔必须大于 0")
|
||||
self._run_command = command_runner
|
||||
self._timeout_seconds = timeout_seconds
|
||||
self._usb_disconnect_timeout_seconds = usb_disconnect_timeout_seconds
|
||||
self._usb_poll_interval_seconds = usb_poll_interval_seconds
|
||||
self._sleep = sleeper
|
||||
|
||||
def search(
|
||||
@@ -132,6 +122,7 @@ class AndroidDeviceService:
|
||||
self._check_cancelled(is_cancelled)
|
||||
if ":" in serial:
|
||||
self._connect_wifi(serial)
|
||||
self._wait_until_device_ready(serial, is_cancelled)
|
||||
self._check_cancelled(is_cancelled)
|
||||
return self.search(is_cancelled)
|
||||
|
||||
@@ -141,7 +132,7 @@ class AndroidDeviceService:
|
||||
is_cancelled: Optional[Callable[[], bool]] = None,
|
||||
on_progress: Optional[ProgressCallback] = None,
|
||||
) -> AndroidWifiConversionResult:
|
||||
"""开启 USB 设备的 5555 端口,等拔线后重连并返回最新列表。"""
|
||||
"""插着数据线开启 5555,连接 Wi-Fi 后返回最新设备列表。"""
|
||||
|
||||
self._validate_usb_serial(usb_serial)
|
||||
self._check_cancelled(is_cancelled)
|
||||
@@ -160,25 +151,14 @@ class AndroidDeviceService:
|
||||
"开启 Wi-Fi 调试",
|
||||
)
|
||||
|
||||
# 部分手机插线时就能建立 Wi-Fi 通道;该连接仅用于提前准备,
|
||||
# 真正的结果仍以拔线后再次连接和验证为准。
|
||||
try:
|
||||
self._connect_wifi(wifi_serial)
|
||||
except AndroidDeviceSearchError:
|
||||
pass
|
||||
|
||||
self._report(
|
||||
on_progress,
|
||||
f"已开启 {wifi_serial},请拔掉 USB 数据线…",
|
||||
)
|
||||
self._wait_until_usb_disconnected(usb_serial, is_cancelled)
|
||||
|
||||
self._check_cancelled(is_cancelled)
|
||||
self._report(on_progress, "已检测到 USB 拔出,正在重新连接 Wi-Fi…")
|
||||
self._report(on_progress, f"正在连接 Wi-Fi 设备 {wifi_serial}…")
|
||||
self._connect_wifi(wifi_serial)
|
||||
self._wait_until_device_ready(wifi_serial, is_cancelled)
|
||||
self._wait_until_device_ready(usb_serial, is_cancelled)
|
||||
|
||||
self._check_cancelled(is_cancelled)
|
||||
devices = self._list_devices()
|
||||
devices = self.search(is_cancelled)
|
||||
wifi_device = next(
|
||||
(device for device in devices if device.serial == wifi_serial),
|
||||
None,
|
||||
@@ -190,8 +170,8 @@ class AndroidDeviceService:
|
||||
"请确认手机和电脑在同一网络后重试"
|
||||
)
|
||||
|
||||
self._report(on_progress, "Wi-Fi ADB 已连接,正在刷新设备列表…")
|
||||
return AndroidWifiConversionResult(wifi_serial, self.search(is_cancelled))
|
||||
self._report(on_progress, "Wi-Fi ADB 已连接,可以保存后拔掉 USB")
|
||||
return AndroidWifiConversionResult(wifi_serial, devices)
|
||||
|
||||
@staticmethod
|
||||
def parse_wifi_ipv4(route_output: str) -> str:
|
||||
@@ -285,34 +265,27 @@ class AndroidDeviceService:
|
||||
value = (result.stdout or "").strip()
|
||||
return value or "—"
|
||||
|
||||
def _wait_until_usb_disconnected(
|
||||
def _wait_until_device_ready(
|
||||
self,
|
||||
usb_serial: str,
|
||||
serial: str,
|
||||
is_cancelled: Optional[Callable[[], bool]],
|
||||
) -> None:
|
||||
attempts = max(
|
||||
1,
|
||||
int(
|
||||
self._usb_disconnect_timeout_seconds
|
||||
/ self._usb_poll_interval_seconds
|
||||
),
|
||||
)
|
||||
missing_checks = 0
|
||||
for attempt in range(attempts):
|
||||
self._check_cancelled(is_cancelled)
|
||||
devices = self._list_devices()
|
||||
if all(device.serial != usb_serial for device in devices):
|
||||
missing_checks += 1
|
||||
if missing_checks >= 2:
|
||||
return
|
||||
else:
|
||||
missing_checks = 0
|
||||
if attempt < attempts - 1:
|
||||
self._sleep(self._usb_poll_interval_seconds)
|
||||
"""短暂等待 ADB transport 完成重启或授权握手。"""
|
||||
|
||||
raise AndroidDeviceSearchError(
|
||||
"等待拔出 USB 超时;5555 已开启,请拔线后重新点击转换"
|
||||
)
|
||||
for attempt in range(DEFAULT_DEVICE_READY_CHECKS):
|
||||
self._check_cancelled(is_cancelled)
|
||||
device = next(
|
||||
(
|
||||
item
|
||||
for item in self._list_devices()
|
||||
if item.serial == serial
|
||||
),
|
||||
None,
|
||||
)
|
||||
if device is not None and device.status == "device":
|
||||
return
|
||||
if attempt < DEFAULT_DEVICE_READY_CHECKS - 1:
|
||||
self._sleep(DEFAULT_DEVICE_READY_INTERVAL_SECONDS)
|
||||
|
||||
def _list_devices(self) -> List[AndroidDevice]:
|
||||
result = self._run(["adb", "devices", "-l"], "读取设备列表")
|
||||
|
||||
Reference in New Issue
Block a user