> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mobilerun.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Device Setup

> Setting up Android and iOS devices for Mobilerun automation

## Overview

Portal is recommended for local Android control; ADB-only control is also supported. Local iOS uses `mobilerun-ios` with WebDriverAgent.

> [tab] Android Setup
>
> ## Prerequisites
>
> Portal requires Android 8.0 (API 26) or newer. ADB-only setup requires ADB and UIAutomator.
>
> **1. Install ADB**
>
> **macOS**: `brew install android-platform-tools`
>
> **Linux**: `sudo apt install adb`
>
> **Windows**: Download from [Android Developer Site](https://developer.android.com/studio/releases/platform-tools)
>
> Verify: `adb version`
>
> **2. Enable USB Debugging**
>
> 1. Go to **Settings** > **About phone**
> 2. Tap **Build number** 7 times (enables Developer options)
> 3. Go to **Settings** > **Developer options**
> 4. Enable **USB debugging**
> 5. Connect device and tap **Always allow**
>
> Verify: `adb devices`
>
> **3. Install Portal App (Recommended)**
>
> ```bash theme={null}
> # Automatic setup (downloads compatible Portal APK)
> mobilerun setup
>
> # Or specify device
> mobilerun setup --device SERIAL_NUMBER
> ```
>
> This command:
>
> * Downloads the compatible Portal APK
> * Installs or upgrades the APK and grants the Android permissions allowed during installation
> * Enables its accessibility service or opens Android Settings for approval
>
> Open Portal once and grant **Display over other apps**. Grant notification permission, Notification Access, screen-capture consent, and **Install unknown apps** only when your tasks need those features.
>
> To run without Portal, skip this step and set `device.portal_mode: disabled` in the framework configuration.
>
> **4. Verify Setup**
>
> ```bash theme={null}
> mobilerun ping
> # Output: Portal is installed and accessible. You're good to go!
> ```
>
> `mobilerun ping` checks Portal. For ADB-only setup, use `adb devices -l` or `adb -s SERIAL get-state`.
>
> ***
>
> ## Portal App
>
> The Mobilerun Portal (`com.mobilerun.portal`) provides:
>
> * **Accessibility Tree** - Extracts UI elements and their properties
> * **Device State** - Tracks current activity, keyboard visibility
> * **Action Execution** - Tap, swipe, text input, and other actions
> * **Local Control** - HTTP, WebSocket, and ADB interfaces
> * **Cloud Connection** - Optional remote control and screen streaming
> * **Events and Triggers** - Optional device events and trigger rules
>
> Keep **Cloud Connection** off for local Framework control. When enabled, Portal connects to the configured server and can send device state, actions, events, and screen-sharing data.
>
> For advanced APIs, see the Portal documentation: [Local API](https://github.com/droidrun/mobilerun-portal/blob/main/docs/local-api.md), [Reverse Connection](https://github.com/droidrun/mobilerun-portal/blob/main/docs/reverse-connection.md), [WebSocket Events](https://github.com/droidrun/mobilerun-portal/blob/main/docs/websocket-events.md), and [Triggers and Events](https://github.com/droidrun/mobilerun-portal/blob/main/docs/triggers.md).
>
> ***
>
> ## Communication Modes
>
> > [accordion] ADB-only mode
> >
> > **How it works:**
> >
> > * Tap, swipe, buttons, app lifecycle, packages, install/uninstall, screenshots, and date use ADB.
> > * UI state comes from UIAutomator.
> > * Text uses Android's ADB input command and is limited to printable ASCII; Portal is needed for reliable Unicode input.
> >
> > **Framework configuration:**
> >
> > ```yaml theme={null}
> > device:
> >   platform: android
> >   portal_mode: disabled
> > ```
> >
> > `portal_mode: disabled` also bypasses the framework's automatic Portal setup.
>
> > [accordion] Portal over TCP
> >
> > **How it works:**
> >
> > * Portal runs HTTP server on device port 8080
> > * ADB forwards local port → device port 8080
> > * Mobilerun sends authenticated HTTP requests to `localhost:PORT`
> > * In `auto` mode, Mobilerun uses the ContentProvider or ADB if HTTP is unavailable
> >
> > **Enable:**
> >
> > ```bash theme={null}
> > # CLI
> > mobilerun run "your command" --tcp
> >
> > # Python
> > config = MobileConfig(device=DeviceConfig(serial="DEVICE_SERIAL", use_tcp=True))
> > ```
> >
> > **Troubleshooting:**
> >
> > ```bash theme={null}
> > # Check port forwarding
> > adb forward --list
> >
> > # Test Portal server
> > adb shell netstat -an | grep 8080
> >
> > # Remove all forwards and retry
> > adb forward --remove-all
> > mobilerun ping --tcp
> > ```
>
> > [accordion] Portal ContentProvider mode
> >
> > **How it works:**
> >
> > * Portal exposes content provider at `content://com.mobilerun.portal/`
> > * Commands sent via ADB shell: `content query --uri ...`
> > * JSON responses parsed from shell output
> > * Authentication is handled through ADB
> >
> > **Usage:**
> >
> > ```bash theme={null}
> > # Default mode (no flag needed)
> > mobilerun ping
> >
> > # Python
> > config = MobileConfig(device=DeviceConfig(serial="DEVICE_SERIAL", use_tcp=False))
> > ```
> >
> > **Troubleshooting:**
> >
> > ```bash theme={null}
> > # Test content provider directly
> > adb shell content query --uri content://com.mobilerun.portal/state
> >
> > # Should include: Row: 0 result=... and a successful JSON status
> > ```
>
> ### Portal policy (`portal_mode`)
>
> ```python theme={null}
> from mobilerun import DeviceConfig, MobileConfig
>
> config = MobileConfig(
>     device=DeviceConfig(
>         portal_mode="auto",      # auto | required | disabled
>         auto_setup=True,
>         use_tcp=False,
>     )
> )
> ```
>
> | Setting | Meaning |
> | - | - |
> | `auto` | Use Portal when available; otherwise use ADB. |
> | `required` | Stop with an error if Portal or its keyboard is unavailable. |
> | `disabled` | Use ADB only and skip Portal setup. |
>
> `auto_setup=True` installs or repairs Portal before connecting. Set it to `False` to skip that step.
>
> ### Local HTTP and WebSocket authentication
>
> Portal generates a bearer token for direct local HTTP and WebSocket clients. Copy it from the Portal main screen or retrieve it over ADB:
>
> ```bash theme={null}
> adb shell content query --uri content://com.mobilerun.portal/auth_token
> ```
>
> Mobilerun handles Portal authentication automatically. Direct WebSocket clients and HTTP calls other than `GET /ping` must use the token shown in Portal or retrieved through ADB.
>
> ***
>
> ## Advanced Setup
>
> > [accordion] Wireless Debugging (Android 11+)
> >
> > ### Setup
> >
> > **1. Enable Wireless Debugging**
> >
> > 1. **Settings** > **Developer options** > **Wireless debugging**
> > 2. Note the IP address and debugging port shown on the main Wireless debugging screen (for example, `192.168.1.100:37757`)
> >
> > **2. Pair Device (First Time)**
> >
> > **Pairing Code Method:**
> >
> > 1. Tap **Pair device with pairing code**
> > 2. Note the pairing code and the pairing-specific IP:port shown in that dialog
> > 3. Run `adb pair IP:PAIRING_PORT`
> > 4. Enter pairing code
> >
> > Android Studio can alternatively pair by scanning the device's QR code. The `adb pair` CLI expects an address/port and pairing code, not the QR payload.
> >
> > **3. Connect**
> >
> > ```bash theme={null}
> > adb connect IP:PORT
> > adb -s IP:PORT get-state
> >
> > # Optional: validate Portal as well
> > mobilerun ping --device IP:PORT
> > ```
> >
> > ### Common Issues
> >
> > * Connection refused → Check same WiFi network and firewall
> > * Frequent drops → Use 5GHz WiFi or stay near router
> > * Can't find IP → Run `adb shell ip addr show wlan0 | grep "inet "` via USB
>
> > [accordion] Wireless Debugging (Android 10 and Below)
> >
> > **1. Enable TCP/IP Mode (USB Required)**
> >
> > ```bash theme={null}
> > # Connect via USB first
> > adb tcpip 5555
> > ```
> >
> > **2. Find Device IP**
> >
> > ```bash theme={null}
> > adb shell ip addr show wlan0 | grep inet
> > ```
> >
> > **3. Connect Wirelessly**
> >
> > ```bash theme={null}
> > # Disconnect USB cable
> > adb connect DEVICE_IP:5555
> > adb -s DEVICE_IP:5555 get-state
> >
> > # Optional: validate Portal as well
> > mobilerun ping --device DEVICE_IP:5555
> > ```
>
> > [accordion] Multiple Devices
> >
> > ### List Devices
> >
> > ```bash theme={null}
> > mobilerun devices
> > # Found 2 connected device(s):
> > #   • emulator-5554
> > #   • 192.168.1.100:5555
> > ```
> >
> > ### Target Specific Device
> >
> > ```bash theme={null}
> > # CLI
> > mobilerun run "your command" --device emulator-5554
> >
> > # Python
> > config = MobileConfig(device=DeviceConfig(serial="emulator-5554"))
> > agent = MobileAgent(goal="your task", config=config)
> > ```
> >
> > ### Parallel Control
> >
> > ```python theme={null}
> > import asyncio
> > from mobilerun import DeviceConfig, MobileConfig, MobileAgent
> > from async_adbutils import adb
> >
> > async def control_device(serial: str, command: str):
> >     device_config = DeviceConfig(serial=serial)
> >     config = MobileConfig(device=device_config)
> >     agent = MobileAgent(goal=command, config=config)
> >     return await agent.run()
> >
> > async def main():
> >     devices = await adb.list()
> >
> >     tasks = [
> >         control_device(devices[0].serial, "Open settings"),
> >         control_device(devices[1].serial, "Check battery"),
> >     ]
> >
> >     results = await asyncio.gather(*tasks)
> >     print(results)
> >
> > asyncio.run(main())
> > ```
>
> ***
>
> ## Troubleshooting
>
> > [accordion] Device not found
> >
> > **Symptoms:** `adb devices` shows no devices or `unauthorized`
> >
> > **Solutions:**
> >
> > 1. Unplug/replug USB cable, try different port
> > 2. Revoke USB debugging authorizations (Developer options)
> > 3. Reconnect and tap "Always allow"
> > 4. Restart ADB: `adb kill-server && adb start-server`
> > 5. **Windows**: Install [Google USB Driver](https://developer.android.com/studio/run/win-usb)
>
> > [accordion] Portal not installed
> >
> > **Symptoms:** `mobilerun ping` fails with "Portal is not installed"
> >
> > **Solutions:**
> >
> > 1. Reinstall: `mobilerun setup`
> > 2. Check: `adb shell pm list packages | grep mobilerun`
> > 3. Verify APK architecture matches device (arm64-v8a for most devices)
> > 4. If Portal is intentionally not used, set `device.portal_mode: disabled` and validate ADB with `adb devices -l` instead of `mobilerun ping`
>
> > [accordion] Accessibility service not enabled
> >
> > **Symptoms:** `mobilerun ping` fails with "accessibility service not enabled"
> >
> > **Solutions:**
> >
> > 1. Auto-enable:
> >    ```bash theme={null}
> >    adb shell settings put secure enabled_accessibility_services \
> >      com.mobilerun.portal/com.mobilerun.portal.service.MobilerunAccessibilityService
> >    adb shell settings put secure accessibility_enabled 1
> >    ```
> > 2. Manual: Settings > Accessibility > Mobilerun Portal > Toggle ON
> > 3. Verify:
> >    ```bash theme={null}
> >    adb shell settings get secure enabled_accessibility_services
> >    # Should contain: com.mobilerun.portal/...
> >    ```
>
> > [accordion] Text input not working
> >
> > **Symptoms:** `input_text()` fails or types gibberish
> >
> > **Solutions:**
> >
> > 1. When Portal is available, Mobilerun tries to enable the Portal keyboard when it connects:
> >    ```bash theme={null}
> >    # Verify
> >    adb shell settings get secure default_input_method
> >    # Should show: com.mobilerun.portal/.input.MobilerunKeyboardIME
> >    ```
> > 2. Manual switch: Long press space bar → Select "Mobilerun Keyboard"
> > 3. Focus the element first (tap it), then input text
> > 4. In ADB-only mode, use printable ASCII; Unicode, control characters, and literal `%s` are rejected
>
> > [accordion] Empty UI state
> >
> > **Symptoms:** `get_ui_tree()` or the formatted agent state is empty or incomplete
> >
> > **Solutions:**
> >
> > 1. With Portal enabled, verify its accessibility path with `mobilerun ping`
> > 2. In ADB-only mode, inspect `adb shell uiautomator dump /dev/tty`
> > 3. Some apps expose incomplete trees (WebViews, games, and custom-rendered UI)
> > 4. Wait for the UI to settle after a tap or swipe, then request fresh state
> > 5. With Portal enabled and overlay permission granted, use its overlay for visual debugging

> [tab] iOS Setup
>
> > [!NOTE]
> >
> > This guide describes `mobilerun-ios --local` on ports `8080`–`8089`. See [iOS Driver](https://docs.mobilerun.ai/framework/sdk/ios-tools.md) for the driver API.
>
> ***
>
> ## Prerequisites
>
> * **iPhone** on iOS 16 or later, and a **USB data cable**
> * **Mac**, or a Linux host with `usbmuxd`
> * **Signing account** for WebDriverAgent (WDA), one of:
>   * an Apple Account signed in to **Xcode** (a free personal team works), or
>   * an **App Store Connect** API key from a paid developer team
> * Not needed on iOS 27 and later with native control, which installs nothing on the phone
>
> `mobilerun-ios setup` enables Developer Mode, mounts the developer disk image, and signs and installs WDA. You only need the device steps of the [Connect an iPhone guide](https://docs.mobilerun.ai/guides/connect-iphone.md); logging in is not required for local mode.
>
> ***
>
> ## Install and Run `mobilerun-ios`
>
> `mobilerun-ios --local` controls the iPhone over USB and exposes the device API on your host. The framework discovers that API automatically.
>
> **1. Install the CLI**
>
> Choose either Homebrew or the installation script:
>
> ```bash Homebrew theme={null}
> brew install droidrun/tap/mobilerun-ios
> ```
>
> ```bash Installation script theme={null}
> curl -fsSL https://github.com/droidrun/mobilerun-ios-releases/releases/latest/download/install.sh | sh
> ```
>
> Verify the installation:
>
> ```bash theme={null}
> mobilerun-ios --version
> ```
>
> **2. Find the iPhone UDID**
>
> Connect and unlock the iPhone, accept the **Trust This Computer** prompt, then run:
>
> ```bash theme={null}
> mobilerun-ios list
> ```
>
> Copy the UDID shown for the iPhone you want to control.
>
> **3. Prepare the iPhone**
>
> ```bash theme={null}
> mobilerun-ios setup <udid>
> mobilerun-ios verify <udid> --deep
> ```
>
> Setup asks which signing account to use and waits for any steps on the phone, such as the Developer Mode restart and trusting the developer. See [Connect an iPhone](https://docs.mobilerun.ai/guides/connect-iphone.md#run-setup). On a native-mode phone (iOS 27 and later), run `verify` without `--deep`.
>
> **4. Start local mode**
>
> ```bash theme={null}
> mobilerun-ios --local <udid>
> ```
>
> Keep this command running. By default, it serves the device API at `http://127.0.0.1:8080`.
>
> **5. Verify the connection**
>
> ```bash theme={null}
> curl http://127.0.0.1:8080/ping
> ```
>
> The response should contain `"result":"pong"`. Then test the framework:
>
> ```bash theme={null}
> mobilerun run "Take a screenshot" --ios
> ```
>
> To select the server explicitly, pass its URL:
>
> ```bash theme={null}
> mobilerun run "Take a screenshot" --ios --device http://127.0.0.1:8080
> ```
>
> > [!NOTE]
> >
> > When several iPhones are attached, ports advance per device (`8080`, `8081`, and so on). Each device URL is printed when `mobilerun-ios` starts. Framework auto-discovery scans ports `8080` through `8089`.
>
> > [!WARNING]
> >
> > Loopback local mode does not require authentication. If you bind to a non-loopback address, `mobilerun-ios` requires `--local-token`. Set the same token in `MOBILERUN_DEVICE_TOKEN` or `device.auth_token` in the framework configuration.
>
> ***
>
> ## How iOS Connects
>
> The iOS local driver connects differently from Android:
>
> | Feature | Android | iOS |
> | - | - | - |
> | Communication | ADB alone, or ADB + Portal HTTP/ContentProvider | Local HTTP API (port 8080 by default) |
> | Device bridge | ADB; optional Portal APK | `mobilerun-ios`, using WebDriverAgent or native control (iOS 27+) |
> | Setup tool | Optional `mobilerun setup` | `mobilerun-ios setup <udid>` |
> | Accessibility | Portal Accessibility API or ADB UIAutomator | WebDriverAgent / XCUITest, or Apple's accessibility service in native mode |
> | Text Input | Portal keyboard or printable-ASCII ADB input | WebDriverAgent text input, or native keyboard events |
> | Connection | ADB over USB/TCP | USB through `mobilerun-ios` |
>
> `mobilerun-ios` reads the screen, performs actions, manages apps, and takes screenshots through WebDriverAgent, or on iOS 27 and later through Apple's built-in services ([native control](https://docs.mobilerun.ai/mobilerun-ios/overview.md#wda-and-native-control)). The Framework connects to its local server automatically.
>
> ***
>
> ## Usage
>
> ### CLI
>
> ```bash theme={null}
> # Auto-discovers mobilerun-ios on ports 8080-8089
> mobilerun run "your command" --ios
>
> # Explicit local server URL
> mobilerun run "your command" --ios --device http://127.0.0.1:8080
> ```
>
> ### Python API
>
> ```python theme={null}
> from mobilerun import MobileAgent, MobileConfig, DeviceConfig
>
> config = MobileConfig(
>     device=DeviceConfig(
>         platform="ios",
>         serial="http://127.0.0.1:8080",  # optional, auto-discovered if omitted
>     )
> )
>
> agent = MobileAgent(
>     goal="Open Settings and check WiFi",
>     config=config
> )
>
> result = await agent.run()
> ```
>
> ***
>
> ## Supported Features
>
> | Feature | Status | Notes |
> | - | - | - |
> | `tap()` | ✅ | |
> | `swipe()` | ✅ | |
> | `input_text()` | ✅ | |
> | `screenshot()` | ✅ | |
> | `get_ui_tree()` | ✅ | Accessibility tree and device state |
> | `start_app()` | ✅ | By bundle identifier |
> | `stop_app()` | ✅ | By bundle identifier |
> | `get_apps()` | ✅ | Includes system-app filtering |
> | `get_date()` | ✅ | |
> | `press_button()` | ✅ | `home`, `back`, `enter`, `delete`, and `app_switch` |
> | `drag()` | ❌ | |
> | `install_app()` | ✅ | From a path on the Mac or a URL |
> | `uninstall_app()` | ❌ | Not supported |
>
> ***
>
> ## Troubleshooting
>
> > [accordion] `mobilerun-ios list` shows no devices
> >
> > **Symptoms:** The connected iPhone does not appear in the device list
> >
> > **Solutions:**
> >
> > 1. Unlock the iPhone and accept the **Trust This Computer** prompt
> > 2. Confirm the USB cable supports data, not only charging
> > 3. On Linux, check that `usbmuxd` is running
>
> > [accordion] WebDriverAgent does not start
> >
> > **Symptoms:** `mobilerun-ios --local` times out while waiting for WebDriverAgent
> >
> > **Solutions:**
> >
> > 1. Run `mobilerun-ios verify <udid> --deep` to check that the runner launches
> > 2. Trust the developer under **Settings** > **General** > **VPN & Device Management**, with the phone online
> > 3. If the signature expired (after 7 days with a free Apple Account), run `mobilerun-ios setup <udid> --reinstall`
> > 4. Keep the device unlocked and run `mobilerun-ios --local <udid>` again
>
> > [accordion] Local server not reachable
> >
> > **Symptoms:** `curl http://127.0.0.1:8080/ping` times out or is refused
> >
> > **Solutions:**
> >
> > 1. Confirm `mobilerun-ios --local <udid>` is still running
> > 2. Check its startup output for the actual server URL
> > 3. If another process uses port 8080, choose an available high port, for example: `mobilerun-ios --local --local-addr 127.0.0.1:18080 <udid>`
> > 4. Test the printed URL with `/ping`. Because custom ports are not auto-discovered, pass it to the framework explicitly with `--device http://127.0.0.1:18080`
>
> > [accordion] Framework auto-discovery fails
> >
> > **Symptoms:** `mobilerun run ... --ios` cannot find the iPhone
> >
> > **Solutions:**
> >
> > 1. Confirm the local server is on one of the automatically scanned ports, `8080` through `8089`
> > 2. Pass the URL explicitly: `--device http://127.0.0.1:8080`
> > 3. Authenticated servers are not auto-discovered. Set `MOBILERUN_DEVICE_TOKEN` and pass the printed URL with `--device`.

***

## Next Steps

* Learn about the [Agent System](https://docs.mobilerun.ai/framework/concepts/architecture.md)
* Explore [Configuration Options](https://docs.mobilerun.ai/framework/sdk/configuration.md)
* Try [Custom Tools](https://docs.mobilerun.ai/framework/features/custom-tools.md)
* Implement [Structured Output](https://docs.mobilerun.ai/framework/features/structured-output.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.