Skip to main content

Thư viện Python để giao tiếp với ADB (Android Debug Bridge)

Project description

PyT-ADB - Thư viện Python để giao tiếp với ADB

PyT-ADB là một thư viện Python giúp giao tiếp với ADB (Android Debug Bridge), cho phép bạn thực hiện các thao tác trên thiết bị Android từ mã Python, như cài đặt ứng dụng, chụp ảnh màn hình, gửi lệnh shell và nhiều chức năng khác.

Cài đặt

pip install pyt-adb

Yêu cầu

  • Python 3.11 trở lên
  • ADB được cài đặt trong PATH (hoặc chỉ định đường dẫn trực tiếp)

Tính năng chính

  • Quản lý kết nối ADB
  • Thao tác với thiết bị Android (multi-device hỗ trợ)
  • Cài đặt/gỡ cài đặt ứng dụng
  • Truyền file giữa máy tính và thiết bị
  • Shell commands
  • Nhập liệu và thao tác UI
  • Chụp ảnh và quay video màn hình
  • Kiểm tra trạng thái thiết bị
  • Các thao tác hệ thống (reboot, kết nối wifi, etc.)

Cách sử dụng

Kết nối và làm việc với ADB

from pyt_adb import ADB

# Khởi tạo ADB
adb = ADB()  # Sử dụng ADB từ PATH
# HOẶC
adb = ADB(adb_path="/path/to/adb")  # Chỉ định đường dẫn cụ thể

# Liệt kê các thiết bị đã kết nối
devices = adb.devices()
print(devices)  # [{'serial': 'ABCD123', 'status': 'device'}, ...]

# Chạy lệnh ADB
output = adb.run_command("version")
print(output)

# Chạy lệnh Shell
output = adb.shell("ls", "-l", "/sdcard")
print(output)

Làm việc với một thiết bị cụ thể

from pyt_adb import Device

# Sử dụng thiết bị mặc định (thiết bị đầu tiên tìm thấy)
device = Device()

# HOẶC chỉ định thiết bị cụ thể
device = Device(serial="ABCD123")

# Thông tin thiết bị
model = device.get_model()
android_version = device.get_android_version()
api_level = device.get_api_level()

print(f"Thiết bị: {model}, Android {android_version}, API {api_level}")

# Thao tác tập tin
device.push("local_file.txt", "/sdcard/remote_file.txt")
device.pull("/sdcard/remote_file.txt", "local_copy.txt")

# Cài đặt/Gỡ cài đặt ứng dụng
device.adb.install("app.apk")
device.adb.uninstall("com.example.app")

# Làm việc với ứng dụng
device.start_app("com.example.app", ".MainActivity")
device.stop_app("com.example.app")
device.clear_app_data("com.example.app")

# Chụp ảnh màn hình
device.take_screenshot("screenshot.png")

# Quay video màn hình
device.record_screen("screen_recording.mp4", time_limit=10)

# Nhập và thao tác UI
device.input_text("Hello world")
device.tap(500, 500)  # Chạm vào tọa độ (500, 500)
device.swipe(100, 500, 400, 500)  # Vuốt từ trái sang phải
device.press_back()
device.press_home()

# Lấy thông tin hệ thống
battery_info = device.get_battery_info()
print(f"Pin: {battery_info.get('level')}%")

# Khởi động lại
device.reboot()  # Khởi động bình thường
device.reboot("recovery")  # Khởi động vào recovery mode

# Kết nối qua WiFi
ip = device.get_ip_address()
device.connect_wifi(ip)

Xử lý lỗi

Thư viện cung cấp các lớp ngoại lệ để xử lý các lỗi có thể gặp phải:

from pyt_adb import ADB, ADBError, DeviceNotFoundError

try:
    adb = ADB()
    device = adb.devices()
except DeviceNotFoundError:
    print("Không tìm thấy thiết bị nào được kết nối!")
except ADBError as e:
    print(f"Lỗi ADB: {e}")

Tham khảo

Tài liệu về ADB: https://developer.android.com/tools/adb

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyt_adb-0.1.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyt_adb-0.1.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file pyt_adb-0.1.1.tar.gz.

File metadata

  • Download URL: pyt_adb-0.1.1.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for pyt_adb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 002f3b3b8917e724622b771d4cf56dd3b6e6743c24d29a63e6ce1585d2c62df7
MD5 aea5ca9c6ff2735b16f836989a1f2694
BLAKE2b-256 159490f6d7434c9805ab3faf2b3b1a010e929768b17fc0b0d2d9d53cf5439963

See more details on using hashes here.

File details

Details for the file pyt_adb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyt_adb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for pyt_adb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 15e073d06fada59bd8f83fa8ad94d941da8cfb9f543b92c15b08bdf0fcd12ee0
MD5 b7796e5dce3415852be43b95b67d2933
BLAKE2b-256 330ab82d26acea442b989bd5a18ffd8148f02c885d9d016aed2ac2417e08ac26

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page