Giving an agent a real phone

Some things only exist inside a phone app. My health fund lets me download my own medical documents, but only from its Android app, behind an SMS code. I wanted an agent to do that for me the way it already handles my email.

The usual ways to drive a phone from a computer all need the phone’s cooperation: developer mode, adb, an accessibility app, screen mirroring. I wanted the opposite. Nothing installed on the phone and no settings changed. The agent gets what a person gets: eyes on the screen and fingers on it.

That is phone-kvm. A small board plugs into the phone’s USB-C port. The phone sees a keyboard, a touchscreen, a mouse and the system keys, which every Android supports out of the box, and it powers the board through the same cable. A camera on the board looks at the screen. The agent talks to the board over Wi-Fi.

Why a camera

The obvious way to see a phone’s screen is its video output. Two problems. Most Android phones have no video output over USB-C at all. And the ones that do blank every secure window: the lock screen’s PIN pad, banking apps, exactly the screens I needed. Here is the same moment captured both ways:

Left, Android’s own screenshot of the PIN screen, which is entirely black. Right, the camera’s view of the same screen, with the keypad readable.

On the left is what Android hands out when you ask for a screenshot. On the right is what a camera sees. A camera can’t be told to look away.

The board

A render of the board: the ESP32-S3 module at the top, the camera connector on the left edge, the USB-C port at the bottom

An ESP32-S3 module, because it has a USB device controller, Wi-Fi, a camera interface and enough memory for a 2-megapixel JPEG. A USB-C port where the phone is the host and supplies power, with ESD protection and a fuse. A 3.3 V regulator sized for the Wi-Fi radio’s peaks, which matter when your power source is a phone’s port. A connector for a common 2 MP camera module. A serial header and two buttons for flashing, since the USB port belongs to the phone. 32 × 50 mm, two layers, about $7 of parts.

I didn’t draw it. The circuit is a Python file listing every part and every connection, and a generator writes the KiCad schematic and board from it, placing the parts and routing the traces with its own grid router. Change the circuit, run it again, get a new board that passes the electrical and design-rule checks for JLCPCB’s limits, plus the order files.

The board is deliberately dumb. It relays input reports and takes pictures. Everything that needs thinking happens on the computer: finding the screen in the picture, mapping pixels to touches, calibration, deciding when the screen has stopped changing.

How to see straight

The camera looks at the phone from an angle, through a bezel, with blur and noise. phonectl, the program the agent calls, finds the lit screen as the brightest four-sided shape in the frame, fits a line to each edge at sub-pixel precision, straightens the picture and averages four frames to halve the noise. The agent gets back an upright screenshot:

Left, the camera’s raw frame: a phone lying sideways at a slight angle. Right, the straightened screenshot the agent receives.

How sharp does the camera need to be? A small study put phone screens through a simulated camera at four resolutions and counted how many words OCR could still read. At 0.3 MP it read 23 to 54 %. At 2 MP it read 85 to 100 %, which is enough for a model to read an interface. 2 MP is also what the cheapest common camera module has.

The screenshot command has one rule that matters more than resolution: it never returns a picture taken before the last input showed up. Calibration measures how long an input takes to appear on screen, and the screenshot then waits until two captures in a row agree. An agent that taps and looks never sees the screen from before its tap.

Building hardware that doesn’t exist yet

The board has never been made. Everything below ran against a simulation of it, and that simulation was most of the work.

The real firmware, the same image that would be flashed onto the chip, runs in Espressif’s QEMU emulator. QEMU had no model of the ESP32-S3’s USB controller or its camera, so we added both: about 600 lines of C emulating the USB controller down to its registers, and a fake camera that hands the firmware the current picture of the scene. The board’s USB traffic crosses into Linux over USB/IP, and from there into a headless Android 16 emulator, whose own kernel drivers enumerate it as they would a real board. The “camera” renders the emulator’s display on a desk at an angle, with a bezel, lens blur and sensor noise.

Neither the firmware nor phonectl knows it is simulated. Of the project’s 24 tests, 16 run end to end through it: typing every printable character, swipes, pinches, the hardware keys, unlocking with a PIN, a password and a pattern.

One surprise: grabbing the emulator’s display freezes it for about 60 ms, after which it delivers the queued input all at once, so a swipe turned into a jump. The fix was to stop refreshing the camera scene while input is flowing. A real phone would never do this. Simulations have their own bugs, and you end up fixing those too.

A real phone

My laptop can’t pretend to be a USB device, so for a real phone the simulated board gets a stand-in cable: the board’s four input devices are created inside the phone’s own kernel over a debugging connection, each with exactly the descriptor the firmware sends. A cheap webcam plays the board’s camera. Debugging has to be on for this, which is the thing the real board won’t need.

Left, the webcam’s view of a cracked Android phone on a dark desk. Right, the straightened screenshot of the same phone, showing an app search for “clock”.

Yes, the screen is cracked. The phone still works.

Things that broke on contact with reality:

  • No touchscreen. My phone’s kernel is built without the driver that tells Android an external touch device is a screen. It treats the board’s touchscreen as a touchpad, so taps do nothing. The mouse became the pointer.
  • The mouse lands in the wrong place. Android speeds up the cursor based on how fast it has been moving. Sending each move as a single report after 400 ms of stillness makes the scaling constant, but aiming from a measured scale was still 5 % off, 70 pixels low at the bottom of the screen. Now mouse to moves the cursor, finds it by comparing two pictures, and corrects by whatever is missing. It takes about 10 seconds an aim and it hits.
  • Stripes. A phone screen is lit in pulses, and a webcam on automatic exposure shows them as dark bands. A fixed exposure of about 17 ms fixed it.
  • Bluetooth was a dead end. Before the debugging route, I tried making the laptop a Bluetooth keyboard. The Bluetooth stack crashed, taking my actual Bluetooth mouse with it, and the phone never connected anyway.

Did it work?

Yes. An agent, looking only through the webcam and acting only through the board’s input devices, opened the health fund’s app, logged in through the SMS code and saved nine lab result PDFs. It took about 24 minutes and dozens of commands, most of them screenshots, followed by mouse aims and clicks.

One footnote. Claude declined to type my password, ID number and SMS code, even into my own account, so the login run was Codex.

Where it stands

From “crazy idea” in a chat about circuit simulators to that first real task took about a day and a half. The board design is finished and passes its checks. It hasn’t been ordered.

What only the real board can answer:

  • whether a phone’s USB port can power the board through Wi-Fi’s peaks;
  • whether the camera connector’s pinout matches the actual module;
  • the optics: focus at 10 to 20 cm, glare, moiré against the pixel grid, and a stand to hold the camera that hasn’t been designed;
  • whether the phone wakes from sleep over USB;
  • how other Android skins treat an external touchscreen.

Smaller limits: typing is US keyboard only, so no Hebrew yet, and the phone doesn’t charge while the board is plugged in. A later revision can add a second USB-C port for a charger.

I wrote none of the code. The agents wrote the firmware, the QEMU devices, the board generator, the rig and phonectl. My part was the idea, the cracked phone, the webcam, and saying “the point is to design boards, not just use a Raspberry Pi” when the first version reached for one.