Skip to main content

Devicebase Python SDK

Project description

Devicebase Python SDK

Python SDK for the Devicebase device automation API. Control Android/HarmonyOS/iOS devices programmatically via HTTP and WebSocket interfaces.

Installation

pip install devicebase

Quick Start

from devicebase import DeviceBaseClient

# Initialize client with device serial
client = DeviceBaseClient(serial="device123")

# Get device info
info = client.get_device_info()

# Control the device
client.tap(x=100, y=200)
client.swipe(x1=0, y1=500, x2=500, y2=500)
client.launch_app("com.example.app")

# Take a screenshot
screenshot_bytes = client.get_screenshot()
with open("screenshot.jpg", "wb") as f:
    f.write(screenshot_bytes)

Configuration

Configure via environment variables or constructor parameters:

import os

# Option 1: Environment variables (optional, has default values)
os.environ["DEVICEBASE_API_KEY"] = "your-api-key"
# DEVICEBASE_BASE_URL defaults to https://api.devicebase.cn if not set
client = DeviceBaseClient(serial="device123")

# Option 2: Constructor parameters
client = DeviceBaseClient(
    serial="device123",
    base_url="https://api.devicebase.cn",
    api_key="your-api-key"
)

Both HTTP and HTTPS protocols are supported.

Device Control

Touch Operations

# Single tap
client.tap(x=100, y=200)

# Double tap
client.double_tap(x=100, y=200)

# Long press
client.long_press(x=100, y=200)

# Swipe
client.swipe(x1=0, y1=500, x2=500, y2=500)

Navigation

# Press back button
client.back()

# Press home button
client.home()

App Operations

# Launch an app
client.launch_app("微信")

# Get current foreground app
app_info = client.get_current_app()
print(app_info.data["package"])

Text Input

# Input text into focused field
client.input_text("Hello World")

# Clear text in focused field
client.clear_text()

UI Hierarchy

# Dump UI hierarchy (useful for automation)
hierarchy = client.dump_hierarchy()
print(hierarchy.data)

Screenshots

# Get screenshot as JPEG bytes
screenshot = client.get_screenshot()

# Download screenshot as file attachment
download = client.download_screenshot()

WebSocket Streaming

Screen Streaming (Minicap)

import asyncio

async def stream_screen():
    client = DeviceBaseClient(serial="device123")
    minicap = client.minicap_client()

    async for frame in minicap.stream_frames():
        # frame is JPEG bytes
        with open("frame.jpg", "wb") as f:
            f.write(frame)

asyncio.run(stream_screen())

Touch Control (Minitouch)

import asyncio

async def control_touch():
    client = DeviceBaseClient(serial="device123")
    minitouch = client.minitouch_client()

    async with minitouch:
        # Tap at coordinates
        await minitouch.tap(100, 200)

        # Swipe gesture
        await minitouch.swipe(0, 500, 500, 500, duration_ms=300)

asyncio.run(control_touch())

Error Handling

from devicebase import (
    DeviceBaseClient,
    AuthenticationError,
    DeviceNotFoundError,
    ValidationError,
)

try:
    client = DeviceBaseClient(serial="device123")
    info = client.get_device_info()
except AuthenticationError:
    print("Invalid or missing API key")
except DeviceNotFoundError:
    print("Device not found or not connected")
except ValidationError as e:
    print(f"Invalid request: {e}")

Examples

More complete examples can be found in the examples/ directory.

License

MIT

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

devicebase-2026.4.21.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

devicebase-2026.4.21-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file devicebase-2026.4.21.tar.gz.

File metadata

  • Download URL: devicebase-2026.4.21.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for devicebase-2026.4.21.tar.gz
Algorithm Hash digest
SHA256 61682a7f3968f80b853e68f3f80e24439363c707ebb4266e90b7ccc7a89aa513
MD5 1a50efa375e23e97bb6ed847cfd38a09
BLAKE2b-256 d19a12fc79f048434df2d15fa4e2638613ce499e3511839eaeba17624c736cf0

See more details on using hashes here.

File details

Details for the file devicebase-2026.4.21-py3-none-any.whl.

File metadata

  • Download URL: devicebase-2026.4.21-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for devicebase-2026.4.21-py3-none-any.whl
Algorithm Hash digest
SHA256 93fb938307e4ba465b917c60bbf33ae10ccff012159da51cc4e4c3dccf375190
MD5 f52d93e1a20a6ab3f9fa7cde3f782bb9
BLAKE2b-256 43307c2e2ba8b72f6692a34fc2069052436f112e2ca0232755f02486a611983f

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