# import uiautomator2 as u2 # d = u2.connect('127.0.0.1:52377') # 例如:"127.0.0.1:61468" # # print('>>>>',d.info) import subprocess import os import subprocess import os def run_adb_command(command, adb_path=None): """ 执行 ADB 命令并返回输出,可以通过环境变量指定 ADB 路径。 Args: command (list): 包含 ADB 命令及其参数的列表。 adb_path (str, optional): ADB 可执行文件的完整路径。 如果为 None,则使用系统环境变量中的 ADB, 或者在代码中设置的自定义环境变量。 Returns: str: 命令的标准输出,如果执行失败则返回 None。 """ env = os.environ.copy() # 复制当前的环境变量 adb_executable = adb_path + "\\adb.exe" if adb_path: # 在子进程的环境变量中设置 ADB 的路径 # adb_executable = adb_path env["PATH"] = os.pathsep.join([os.path.dirname(adb_path), env.get("PATH", "")]) # else: try: full_command = [adb_executable] + ["shell"] + command result = subprocess.run( full_command, capture_output=True, text=True, check=True, env=env # 将设置的环境变量传递给子进程 ) return result.stdout.strip() except subprocess.CalledProcessError as e: print(f"ADB 命令 '{' '.join(command)}' 执行失败:") print(f"错误代码: {e.returncode}") print(f"错误输出:\n{e.stderr}") return None except FileNotFoundError: print(f"错误: ADB 可执行文件 '{adb_executable}' 未找到。请检查路径是否正确。") return None def list_devices(adb_path=None): """ 列出已连接的 Android 设备。 Args: adb_path (str, optional): ADB 可执行文件的完整路径。 Defaults to None. """ command = ["devices"] output = run_adb_command(command, adb_path) if output: print("已连接的设备:") for line in output.splitlines()[1:]: if line.strip(): device_info = line.split('\t') print(f"- {device_info[0]}") else: print("未找到已连接的 Android 设备。") def get_screen_size(device_serial=None, adb_path=None): """ 获取指定设备的屏幕尺寸。 Args: device_serial (str, optional): 设备的序列号。 Defaults to None. adb_path (str, optional): ADB 可执行文件的完整路径。 Defaults to None. """ command_base = [] if device_serial: command_base.extend(["-s", device_serial]) command_base.extend(["wm", "size"]) output = run_adb_command(command_base, adb_path) if output: if "Physical size" in output: size_str = output.split(":")[1].strip() print(f"屏幕尺寸: {size_str}") return size_str else: print(f"无法获取屏幕尺寸: {output}") return None return None def tap_screen(x, y, device_serial=None, adb_path=None): """ 模拟点击屏幕指定坐标。 Args: x (int): 点击的 X 坐标。 y (int): 点击的 Y 坐标。 device_serial (str, optional): 设备的序列号。 Defaults to None. adb_path (str, optional): ADB 可执行文件的完整路径。 Defaults to None. """ command_base = [] if device_serial: command_base.extend(["-s", device_serial]) command_base.extend(["input", "tap", str(x), str(y)]) print(f"模拟点击坐标 ({x}, {y})") run_adb_command(command_base, adb_path) def swipe_screen(start_x, start_y, end_x, end_y, duration_ms=None, adb_path=None): """ 模拟屏幕滑动操作。 Args: start_x (int): 滑动起始 X 坐标。 start_y (int): 滑动起始 Y 坐标。 end_x (int): 滑动结束 X 坐标。 end_y (int): 滑动结束 Y 坐标。 duration_ms (int, optional): 滑动持续时间 (毫秒). Defaults to None. adb_path (str, optional): ADB 可执行文件的完整路径。 Defaults to None. """ command_base = ["input", "swipe", str(start_x), str(start_y), str(end_x), str(end_y)] if duration_ms is not None: command_base.append(str(duration_ms)) run_adb_command(command_base, adb_path) if __name__ == "__main__": # 假设你的 ADB 路径是: # adb_custom_path = "/path/to/your/android-sdk/platform-tools/adb" # 请替换为你的实际路径 adb_custom_path = "../bin/windows/platform-tools" # 请替换为你的实际路径 print("--- 列出已连接的设备 (通过设置环境变量) ---") list_devices(adb_path=adb_custom_path) # 尝试获取第一个连接设备的屏幕尺寸 (通过设置环境变量) print("\n--- 获取屏幕尺寸 (通过设置环境变量) ---") screen_size_str = get_screen_size(adb_path=adb_custom_path) screen_width = None screen_height = None if screen_size_str: try: width, height = map(int, screen_size_str.split("x")) screen_width = width screen_height = height print(f"屏幕宽度: {screen_width}, 屏幕高度: {screen_height}") except ValueError: print("无法解析屏幕尺寸字符串。") # 模拟点击屏幕中间 (如果获取到屏幕尺寸,并设置环境变量) if screen_width and screen_height: center_x = screen_width // 2 center_y = screen_height // 2 print("\n--- 模拟点击屏幕中间 (通过设置环境变量) ---") tap_screen(center_x, center_y, adb_path=adb_custom_path) swipe_screen(center_x, center_y, center_x, center_y * 1.5, adb_path=adb_custom_path)