A Practical Guide to Mobile AI Automation Agents

    This article explains how to build an AI agent that can operate a mobile phone as an automation terminal. It covers a three-layer architecture (perception, decision, execution), how to wrap device capabilities into atomic tools, how screen reading works through accessibility services, input injection and screen capture, the agent-to-device call chain, local versus remote deployment with pairing authentication, security and compliance boundaries, and common failure modes with tuning advice. The goal is not to replace human work but to hand repetitive, rule-based, cross-screen tasks to an agent while keeping exception handling with human reviewers.

    1. Background: Why a Controllable Mobile Terminal Is Needed

    In mobile-first workflows, many processes live entirely inside phone interfaces: internal approvals, store inspections, content moderation, compatibility regression testing, and more. These processes share a few traits: interfaces change frequently, stable APIs are missing, and traditional scripts are hard to maintain over time. Combining an LLM’s planning ability with on-device UI操作能力 produces an automation terminal that can read the screen, tap, type, and be controlled remotely. This is a pragmatic engineering path.

    The value of this approach is not to replace human workers. It is to hand repetitive, clearly defined, cross-screen operations to an agent, while leaving exception judgment to human review.

    2. Overall Architecture: Perception, Decision, and Execution Layers

    A reliable design usually has three layers:

    • Perception layer: collects the on-screen UI structure, text, control attributes, and image data.
    • Decision layer: the agent handles intent understanding, task decomposition, and selection of the next action.
    • Execution layer: translates decisions into system actions such as taps, swipes, text input, and app launches, then returns the execution results.

    Decoupling the three layers over a local network or in-process IPC means the execution layer can be replaced independently, and the decision layer can switch models on demand, without changing the phone-side code.

    3. Tool Wrapping: Abstracting UI Actions into Atomic Capabilities

    For an agent to “act,” the execution layer must package system capabilities into a set of tools with clear semantics. Common atomic capabilities include:

    1. open_app(name) Launch the specified app
    2. screenshot() Capture the current screen image
    3. read_ui_tree() Read the control tree of the current window
    4. tap(x, y) Tap by coordinates
    5. tap_text(label) Locate and tap a control by its text
    6. type(text) Write text into the focused input field
    7. swipe(x1,y1,x2,y2) Swipe
    8. back() / home() System-level back and home

    Three design points matter. First, prefer “locate by text or description” over coordinates only, to reduce fragility when interfaces change. Second, every tool should return a structured result containing a success flag, a summary of the current page, and an optional screenshot reference. Third, action granularity should be moderate: too fine makes the planning chain too long, too coarse makes local exception handling difficult.

    4. How Screen Reading Works: Structuring the UI via Accessibility Services

    The common way to read the screen on a phone is to use the system’s accessibility capability, traverse the control tree of the active window, and extract control type, text, clickability, bounding coordinates, and hierarchy, then return it as a structured node tree.

    Structured data has several advantages over a pure screenshot: text can be matched directly, controls can be located precisely, and state can be judged (for example, whether a control is clickable or obscured). In practice, keep a screenshot as a fallback. When the control tree is empty (self-drawn interfaces, video content, system dialogs), the decision layer can fall back to visual understanding.

    5. How Execution Works: Input Injection and Screen Capture

    Taps and text input are usually implemented through accessibility gesture injection or system input events; screenshots and screen recording rely on system screen capture. These capabilities are permission-sensitive, so engineering practice should:

    • Make the permission list explicit: accessibility service, floating window, screen recording, and required runtime permissions, each with its purpose stated.
    • Apply least privilege: enable only during task execution and revoke when the task ends.
    • Be observable: log a timestamp, target control, result, and duration for every action, so failures can be traced.

    6. Call Chain and Timing Orchestration

    A typical chain is: the agent emits a tool call request → a local relay service receives it → forwards it to the phone-side execution end → the execution end calls system capabilities → returns structured results → the agent decides whether to continue.

    1. Agent --HTTP/local IPC--> Relay(127.0.0.1:port) --> Phone execution end
    2. ^ |
    3. |________________ structured result + screenshot ref ____|

    One user intent is often split into multiple steps. For example, “open Settings and describe the current page” breaks into launching the app, taking a screenshot, understanding the UI, and summarizing the output. Engineering needs a per-step timeout, a maximum step count, and a retry policy on failure, so the agent does not loop repeatedly on an abnormal screen.

    7. Deployment Forms: Local Execution and Remote Control

    Form one, on-device closed loop: run the agent CLI in a terminal emulation environment, keep the execution end resident as a standalone app, and let the two communicate over the device’s loopback address. The advantage is that data never leaves the device and latency is low, which suits privacy-sensitive scenarios.

    Form two, remote control: the execution end runs on the controlled device, while the agent runs on a PC or in the cloud, with the link established through NAT traversal or a secure tunnel. This form needs extra attention to transport encryption, access allowlists, and operation auditing.

    Either way, first use requires pairing authentication: the execution end displays a one-time pairing code, the agent side submits it to establish trust, and later communication uses token validation, so device capabilities cannot be invoked from arbitrary sources.

    8. Security and Compliance Boundaries

    • Data minimization: screenshots and UI text are processed locally by default; if written to disk, they must be encrypted with a retention period.
    • Interruptible behavior: provide pause and terminate switches at any time to prevent mistakes from spreading.
    • Second confirmation for sensitive actions: deletion, payment, and permission changes require human confirmation.
    • Audit trail: fully record task inputs, action sequences, and results to meet compliance traceability.
    • Explicitly prohibited uses: not for bypassing platform risk controls, bulk registration, or scraping restricted data.

    9. Common Failure Modes and Tuning Advice

    Tapping before the interface finishes loading is the most common failure; add a stability wait and a control-existence check before each action. Duplicate control text causes ambiguous location; disambiguate with hierarchy path and index. System dialogs stealing focus require a preset dialog-handling policy. When the interface jumps mid-task, support replanning from the current state instead of relying on a fixed script.

    10. Conclusion

    Turning a phone into a terminal an agent can operate is essentially a systems engineering effort of capability layering, permission收敛, and observable state. Master four cornerstones—atomic tool design, structured screen reading, pairing authentication, and audit mechanisms—and mobile automation can be deployed reliably under compliance. Baidu AI Cloud’s general capabilities in container platforms, log services, and monitoring and alerting can support the deployment and operations of such systems.