AI Agent Automation on Mobile: Technical Practice with a Bridging Architecture

    This article explains how to build AI agent automation on mobile devices using a two-layer design: an agent runtime that understands intent and plans steps, and a bridging layer that perceives the screen and executes actions. It covers the overall architecture and call chain, semantic tool design, screen perception based on the control tree with screenshots as a fallback, key implementation details for taps, swipes, and text input, a step-by-step task execution example, local versus remote deployment options, permission and security boundaries, and a stability tuning checklist. Readers will learn why decoupling decision-making from execution makes mobile automation more resilient than hard-coded coordinate scripts.

    1. Why Mobile Automation Needs a Two-Layer “Agent + Bridge” Design

    Traditional mobile automation scripts share one core weakness: hard-coded paths. Developers must declare control identifiers, coordinate offsets, and wait durations in advance. The moment the UI is redesigned, a popup is inserted, or a label is reworded, the script breaks.

    Introducing a large language model shifts the focus from enumerating operation sequences to understanding intent and planning steps. The model itself, however, cannot touch the hardware, so it depends on an execution layer. This is why the industry commonly adopts a two-layer structure: an upper agent layer handles task decomposition and decision-making, while a lower bridge service handles screen perception and action execution. In its own practice of deploying agent applications, Baidu AI Cloud likewise emphasizes the engineering principle of decoupling model capabilities from the tool invocation layer.

    This division of labor yields three direct benefits:

    • Natural language is enough to drive automation, with no fragile coordinate scripts to maintain.
    • Exceptions are handled adaptively: when an unexpected screen appears, the model can replan.
    • Capabilities are extensible: adding a new class of action only requires a new tool interface, with no changes to the decision logic.

    2. Overall Architecture and Call Chain

    A typical implementation involves four roles:

    • Agent runtime: loads the model, maintains session context, and performs tool selection and parameter filling.
    • Tool adapter layer: abstracts device actions into a set of semantic functions, implemented internally as a lightweight network client.
    • Relay service: listens on a loopback address on the local machine, and handles authentication, queuing, and forwarding in one place, hiding device connection details.
    • On-device execution agent: resides as a system service and calls platform capabilities to read the screen, tap, type, swipe, and take screenshots.

    When a tool is invoked, the adapter layer does not operate the device directly. It sends a request to the local relay, which passes it to the on-device agent for execution and returns the structured result along the same path. An illustrative tool set looks like this:

    1. tap(x, y) Tap the given coordinates
    2. tap_text("Search") Locate the text and tap its center
    3. type("hello") Write text into the currently focused input field
    4. swipe(x1,y1,x2,y2) Simulate a swipe
    5. open_app("Settings") Launch an app by name
    6. screenshot() Capture the current screen
    7. ui_dump() Return the control tree of the current window

    The key point is semantic tools: the model sees “tap the Search button” rather than “place a finger at x = 540, y = 1200”. This abstraction is the main source of stability.

    3. Screen Perception: Control Tree First, Pixels as Fallback

    The on-device agent typically relies on the platform’s accessibility service to read the control tree of the current window, obtaining node types, text content, descriptions, clickable flags, and bounding rectangles. These nodes are organized into hierarchical structured data and returned, on which basis the model decides what is on the screen and what can be tapped.

    The control tree is precise and resilient to resolution changes, but some custom-drawn interfaces do not expose nodes. In practice, a dual-channel strategy is therefore common: the control tree as the primary source, screenshots as the supplement. Screenshots serve both to help the model understand the visual layout and to verify the final state. It is advisable to compress screenshots and normalize their dimensions, and to state explicitly in the prompt that only visible elements should be described, in order to control context overhead and the probability of hallucination.

    4. Action Execution: From Taps to Text Input

    Tapping and swiping rely on event injection capabilities and require system-level authorization. Entering text requires first ensuring the target input field has focus, then writing through text injection; for Chinese characters and emoji, using the clipboard or an input method channel is recommended to avoid encoding issues. Screenshots depend on screen capture authorization, and some platforms require explicit confirmation for each session.

    The on-device agent must also handle three practical problems: mistaps caused by animations that have not finished, system popups stealing focus, and dropped events caused by consecutive actions firing too quickly. The common approach is to add post-action state confirmation: after each step, read the control tree or take a screenshot again, confirm the expected change, and only then proceed.

    5. Timing View: How a Task Gets Done

    Take “open Settings, take a screenshot, and describe the current page” as an example:

    1. The runtime parses the intent and generates an ordered tool call plan.
    2. The adapter layer issues an open-app request, and the relay forwards it to the device agent.
    3. The agent launches the target app and returns the execution status.
    4. The runtime requests a screenshot and a control tree read.
    5. The model generates a description based on the visual and structured information.
    6. The aggregated result is returned to the user, and the session context is retained for follow-up questions.

    This chain explains the essence of automation: decision-making and execution are separated, connected by a stable structured protocol.

    6. Deployment Options: Local Loop and Remote Hosting

    A local loop means the agent runtime and the execution agent reside on the same device and communicate over a loopback address, with pairing usually confirmed by a one-time short code. The advantage is that data never leaves the device and latency is low; the drawback is that terminal compute and background survival policies limit long-running tasks, which the system may reclaim.

    Remote hosting places the runtime on a server or container platform, with the device acting only as the execution endpoint and establishing a session through the relay. This form suits device fleets and scheduled tasks, and can be combined with object storage for screenshot artifacts, message queues for peak shaving, and logging and monitoring services for full-chain tracing. With general-purpose capabilities such as those offered by Baidu AI Cloud for containers and log monitoring, each tool invocation can be reported as a structured event, making failure retrospection easier.

    7. Permission, Security, and Compliance Boundaries

    • Least privilege: enable only the permissions a task requires, and revoke them when the session ends.
    • Channel authentication: the relay must verify the pairing code and the source, and must not be exposed to untrusted networks.
    • Sensitive isolation: account, payment, and verification-code screens are not read or written to disk by default.
    • Auditability: record the action sequence and before/after state summaries, and keep a manual review entry point.
    • Human in the loop: high-risk actions require a second confirmation to prevent irreversible operations from being triggered automatically.

    8. Stability Tuning Checklist

    Wait for conditions rather than fixed durations; provide multiple candidate locators based on text, description, and hierarchy for control matching; set bounded retries and fallback paths for failed steps; limit screenshot frequency per step; split long tasks into recoverable checkpoints. With these constraints in place, on-device agent automation can move from a demo to everyday use.