Skip to main content
Base class defining the interface for all device drivers.

DeviceDriver

Base class for all device drivers. DeviceDriver is the base class for asynchronous device drivers in mobilerun-core-local. Use supported to check optional device operations. Most unavailable operations raise NotImplementedError; input_coordinate_size() is always available and defaults to the screenshot dimensions.

Quick Reference

Driver Methods:
  • connect(), ensure_connected()
  • tap(), swipe(), input_text(), press_button(), press_key_code(), drag()
  • start_app(), stop_app(), install_app(), uninstall_app(), get_apps(), list_packages()
  • screenshot(), input_coordinate_size(), get_ui_tree(), get_date()
Key Attributes:
  • supported: set[str] - Set of method names the driver implements. Check membership before calling.
  • supported_buttons: set[str] - Set of button names accepted by press_button() (e.g. {"back", "home", "enter"}).

How It Works

DeviceDriver sends commands to the device. StateProvider converts device data into UIState. Action functions receive an ActionContext and return an ActionResult.

Imports

Import asynchronous drivers from mobilerun-core-local:
Install mobilerun-core-local[cloud] to use CloudDriver.

Common Interface

All DeviceDriver implementations may provide these methods (check supported set for availability):

Lifecycle

  • connect() -> None - Establish connection to the device
  • ensure_connected() -> None - Connect if not already connected

Input Actions

  • tap(x: int, y: int) -> None - Tap at screen coordinates (pixels on Android; logical points on iOS)
  • swipe(x1: int, y1: int, x2: int, y2: int, duration_ms: float = 1000) -> None - Swipe gesture
  • drag(x1: int, y1: int, x2: int, y2: int, duration: float = 3.0) -> None - Drag gesture
  • input_text(text: str, clear: bool = False, stealth: bool = False, wpm: int = 0) -> bool - Text input into focused field. stealth enables human-like typing delays; wpm sets the typing speed in words per minute (0 = instant).
  • press_button(button: str) -> None - Press a named button (e.g. back, home, enter). Raises ValueError if not in supported_buttons.
  • press_key_code(key_code: int) -> None - Press an integer key code.

App Management

  • start_app(package: str, activity: str | None = None) -> str - Launch app
  • stop_app(package: str) -> str - Stop a running app
  • install_app(path: str, **kwargs) -> str - Install app
  • uninstall_app(package: str) -> str - Uninstall app
  • list_packages(include_system: bool = False) -> List[str] - List packages
  • get_apps(include_system: bool = True) -> List[Dict[str, str]] - Get apps with labels

State / Observation

  • screenshot(hide_overlay: bool = True) -> bytes - Capture screen as PNG bytes
  • input_coordinate_size(screenshot_width: int, screenshot_height: int) -> tuple[int, int] - Return the dimensions used for input coordinates. On iOS, these can differ from screenshot dimensions.
  • get_ui_tree() -> Dict[str, Any] - Get raw UI / accessibility tree
  • get_date() -> str - Get device date/time

StateProvider

Base class for state providers. Subclass it to support another platform. Its supported set lists available UI features, such as element lookup and coordinate conversion.

AndroidStateProvider

Fetches and formats device state as a UIState. Set stealth=True to vary tap coordinates within element bounds.

UIState

Holds parsed UI elements for a single device state snapshot. Key Methods:
  • get_element(index: int) -> Dict | None - Recursively find an element by its index
  • get_element_coords(index: int) -> Tuple[int, int] - Return the centre (x, y) of an element. Raises ValueError when element is missing or has no bounds.
  • get_element_info(index: int) -> Dict - Return element metadata (text, className, type, child_texts)
  • get_clear_point(index: int) -> Tuple[int, int] - Find a tap point that avoids overlapping elements (falls back to centre)
  • convert_point(x: int, y: int) -> Tuple[int, int] - Convert point to absolute pixels if normalized mode is active
Key Attributes:
  • elements - List of parsed UI elements
  • formatted_text - Formatted text representation of the UI tree
  • focused_text - Text of the currently focused element
  • phone_state - Dict with current activity, keyboard visibility, etc.
  • screen_width / screen_height - Device screen dimensions
  • use_normalized - Whether normalized coordinate mode is active

ActionContext

Everything an action function needs to interact with the device. Attributes:
  • driver - DeviceDriver instance for raw device I/O
  • ui - UIState instance for element resolution (refreshed each step)
  • shared_state - MobileAgentState for shared agent state
  • state_provider - StateProvider for fetching fresh UI state
  • app_opener_llm - LLM instance for app opening workflow (optional)
  • credential_manager - CredentialManager instance (optional)
  • streaming - Whether streaming is enabled

ActionResult

Structured return type from action functions. The summary field describes the result.

Action Functions

Action functions follow this pattern:
Available actions:
  • click(index) - Click UI element by index
  • click_at(x, y) - Click at screen coordinates
  • click_area(x1, y1, x2, y2) - Click center of area defined by coordinates
  • long_press(index) - Long press UI element by index
  • long_press_at(x, y) - Long press at screen coordinates
  • type(text, index=None, clear=False) - Optionally focus an indexed element, then input text (set clear=True to clear first)
  • type_text(text, clear=False) - Input text into the focused field
  • type_secret(secret_id, index) - Input a configured credential into an indexed element
  • swipe(coordinate, coordinate2, duration=1.0) - Swipe gesture between two coordinate lists
  • system_button(button) - Press system buttons (back, home, enter)
  • open_app(...) - Open an Android app by name or an iOS or Visual Remote app by ID
  • wait(duration=1.0) - Wait for a duration in seconds
  • complete(success, message) - Mark task as finished
Coordinate tools (click_at, click_area, long_press_at) are disabled by default. Vision enables click_at when normalized coordinates are off. Screenshot-only mode enables all three. To enable all three with standard vision, set disabled_tools: [] in ToolsConfig. See Vision Mode.

Custom Tool Integration

Adding Custom Tools


Driver and Platform Comparison

RecordingDriver and StealthDriver support the same methods as the driver they wrap.

Best Practices

1. Check supported methods before calling

2. Use ActionContext for agent-level interactions

3. Use StateProvider for UI state


Error Handling

Driver methods use consistent error handling: Unsupported operations:
PlatformUnsupportedError is a subclass of NotImplementedError. Check driver.supported before calling an optional method. press_button() raises ValueError for names outside supported_buttons. CloudDriver raises DeviceDisconnectedError for SDK connection, timeout, and conflict failures. Other connection and authentication failures may raise connection, HTTP, or permission errors. ActionResult for action functions:

See Also