### Async Client Example Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Demonstrates the basic setup for using the asynchronous ADB client. Requires importing `asyncio` and `aiofiles` along with `ClientAsync`. ```python import asyncio import aiofiles from ppadb.client_async import ClientAsync as AdbClient ``` -------------------------------- ### Install Docker Compose (Console) Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Command to install Docker Compose using pip. This is a prerequisite for running the project's test cases which utilize Docker for environment setup. ```console pip install docker-compose ``` -------------------------------- ### Install ppadb using pip Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Install the latest version of the pure-python-adb package using pip. This package requires Python 3.6+. ```console pip install -U pure-python-adb ``` -------------------------------- ### List Devices and Manage APKs Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Retrieves a list of all connected devices, then iterates through them to install, check installation status, and uninstall a specified APK. Uses the 'ppadb' client library. ```python from ppadb.client import Client as AdbClient apk_path = "example.apk" # Default is "127.0.0.1" and 5037 client = AdbClient(host="127.0.0.1", port=5037) devices = client.devices() for device in devices: device.install(apk_path) # Check apk is installed for device in devices: print(device.is_installed("example.package")) # Uninstall for device in devices: device.uninstall("example.package") ``` -------------------------------- ### Run Test Cases with Docker Compose (Console) Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Command to start the Docker containers defined in the 'docker-compose.yml' file. This command will set up the necessary environment, including an Android emulator and a Python environment, to run the project's test suite. ```console docker-compose up ``` -------------------------------- ### Execute ADB Shell Command Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Executes a command on the connected Android device's shell. This example demonstrates running 'echo hello world !'. ```python from ppadb.client import Client as AdbClient # Default is "127.0.0.1" and 5037 client = AdbClient(host="127.0.0.1", port=5037) device = client.device("emulator-5554") device.shell("echo hello world !") ``` -------------------------------- ### Pull File from Device Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Downloads a file from the Android device to the local machine. This example first captures a screenshot to a temporary location on the device and then pulls it. ```python from ppadb.client import Client as AdbClient client = AdbClient(host="127.0.0.1", port=5037) device = client.device("emulator-5554") device.shell("screencap -p /sdcard/screen.png") device.pull("/sdcard/screen.png", "screen.png") ``` -------------------------------- ### Connect to ADB Server and Get Version Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Establishes a connection to the ADB server and retrieves its version number. Assumes ADB server is running on the default host ('127.0.0.1') and port (5037). ```python from ppadb.client import Client as AdbClient # Default is "127.0.0.1" and 5037 client = AdbClient(host="127.0.0.1", port=5037) print(client.version()) ``` -------------------------------- ### Enable Debug Logging for ppadb Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Configures the Python logging module to display DEBUG level messages from the 'ppadb' library. This is useful for troubleshooting connection or command execution issues. ```python import logging logging.getLogger("ppadb").setLevel(logging.DEBUG) ``` -------------------------------- ### Capture Screenshot Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Captures a screenshot of the Android device screen and saves it as a PNG file. It uses the 'screencap' command and writes the binary output to a local file. ```python from ppadb.client import Client as AdbClient client = AdbClient(host="127.0.0.1", port=5037) device = client.device("emulator-5554") result = device.screencap() with open("screen.png", "wb") as fp: fp.write(result) ``` -------------------------------- ### Read Logcat Line by Line Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Reads and prints logcat output from an Android device line by line. It utilizes `makefile` to create a file-like object from the socket connection for easier line reading. Processes the first 10 lines. ```python from ppadb.client import Client def dump_logcat_by_line(connect): file_obj = connect.socket.makefile() for index in range(0, 10): print("Line {}: {}".format(index, file_obj.readline().strip())) file_obj.close() connect.close() client = Client() device = client.device("emulator-5554") device.shell("logcat", handler=dump_logcat_by_line) ``` -------------------------------- ### Push File to Device Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Transfers a local file (e.g., an APK) to a specified path on the Android device. This function is part of the 'ppadb' library. ```python from ppadb.client import Client as AdbClient client = AdbClient(host="127.0.0.1", port=5037) device = client.device("emulator-5554") device.push("example.apk", "/sdcard/example.apk") ``` -------------------------------- ### Modify ADB Host Configuration (Python) Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Python code snippet showing how to modify the 'adb_host' variable within the 'test/conftest.py' file. This change is necessary to direct test cases to connect to the 'emulator' when running tests with Docker. ```python adb_host="emulator" ``` -------------------------------- ### Connect to a Specific Device Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Connects to a specific ADB device identified by its serial number. Requires an ADB server connection. ```python from ppadb.client import Client as AdbClient # Default is "127.0.0.1" and 5037 client = AdbClient(host="127.0.0.1", port=5037) device = client.device("emulator-5554") ``` -------------------------------- ### Remote ADB Connection Management Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Manages remote ADB connections. This includes connecting to a remote ADB server, obtaining a device object for the remote connection, and disconnecting all remote connections. ```python from ppadb.client import Client as AdbClient client = AdbClient(host="127.0.0.1", port=5037) client.remote_connect("172.20.0.1", 5555) device = client.device("172.20.0.1:5555") # Disconnect all devices client.remote_disconnect() ##Disconnect 172.20.0.1 # client.remote_disconnect("172.20.0.1") ##Or # client.remote_disconnect("172.20.0.1", 5555) ``` -------------------------------- ### Save Screenshot using ADB Device (Python) Source: https://github.com/swind/pure-python-adb/blob/master/README.rst This asynchronous function captures a screenshot from an Android device using the ADB protocol and saves it as a PNG file. It requires the 'aiofiles' library for asynchronous file operations and the 'pure-python-adb' library for device interaction. The function takes an ADB device object as input and returns the filename of the saved screenshot. ```python async def _save_screenshot(device): result = await device.screencap() file_name = f"{device.serial}.png" async with aiofiles.open(f"{file_name}", mode='wb') as f: await f.write(result) return file_name async def main(): client = AdbClient(host="127.0.0.1", port=5037) devices = await client.devices() for device in devices: print(device.serial) result = await asyncio.gather(*[_save_screenshot(device) for device in devices]) print(result) asyncio.run(main()) ``` -------------------------------- ### Dump Logcat Output Source: https://github.com/swind/pure-python-adb/blob/master/README.rst Streams and prints logcat output from an Android device. It reads data in chunks and decodes it as UTF-8. The handler function processes the stream. ```python def dump_logcat(connection): while True: data = connection.read(1024) if not data: break print(data.decode('utf-8')) connection.close() from ppadb.client import Client as AdbClient # Default is "127.0.0.1" and 5037 client = AdbClient(host="127.0.0.1", port=5037) device = client.device("emulator-5554") device.shell("logcat", handler=dump_logcat) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.