DEV Community

Duvan Jamid Vargas
Duvan Jamid Vargas

Posted on

I Built a Universal Remote for My Smart TV (LG webOS, Android TV, and the Generic Boxes Too)

My TV remote died the slow death all remotes die: battery drawer held shut with tape, a D-pad that only registers "down" if you hit it twice, and a mute button that stopped muting sometime in 2024. Buying a replacement felt like admitting defeat. So I built one instead — as an Android app, with no cloud, no subscription, and no "smart hub" in the middle.

What started as "control my LG TV from my phone" turned into reverse-engineering two different remote protocols and writing a fourth fallback path for TVs that don't cooperate with either one. Here's the trip.

TV Remote app control screen — D-pad, volume/channel rockers, power and media transport controls, connected live to a real LG TV

The TV landscape is more fragmented than you'd think

"Smart TV" is doing a lot of work hiding how different these things actually are under the hood:

  • LG TVs run webOS, with their own proprietary control protocol.
  • Certified Android TV devices (Google TV, Sony, TCL, etc.) expose a protobuf-based remote service that only Google's own apps are supposed to use.
  • Generic Android TV boxes — the cheap ones flashed with stock AOSP and no Google certification — expose neither of the above. All you get is whatever Android itself gives you.
  • Worst case: the TV is reachable over Bluetooth but not confidently over the network at all.

One app, four backends. Let's go through each.

Backend 1: LG webOS — SSAP over WebSocket

LG's TVs speak SSAP (webOS's internal protocol) over a WebSocket connection on the local network. There's no official public SDK for third-party apps — LG's own cordova-plugin-lgtv and years of community reverse-engineering are the closest thing to documentation. The shape of it:

  1. Open a WebSocket to the TV (wss://<tv-ip>:3001).
  2. Send a register handshake. First time, the TV pops up an on-screen prompt asking the user to allow the connection; accepting hands back a client key that's good forever after that.
  3. From then on, everything is a JSON message with a uri (basically an endpoint name) and a payload — volume, input switching, launching apps, you name it. webOS is generous here: it'll hand you the list of installed apps, the channel list, the available inputs, all queryable live.

That richness is why the LG screen in the app is the most fully-featured one — app grid, channel list, input switcher, not just a D-pad.

TV Remote app's Apps tab — favorited and installed apps pulled live from the TV over SSAP

TV Remote app's Inputs tab, showing the currently connected source live from the TV

Backend 2: Certified Android TV — Android TV Remote Service v2

Android TV devices that pass Google's certification run a Remote Service that speaks a protobuf-over-TLS protocol (port 6467 for pairing, 6466 for the actual remote session). This one's closer to "documented" — tronikos/androidtvremote2 had already done the legwork in Python, which made porting the message flow to Kotlin straightforward instead of a packet-capture exercise:

  1. Pairing: generate a self-signed X.509 certificate (there's no stock Android API for this, hence pulling in Bouncy Castle), open a TLS connection to the pairing port, and go through a PIN exchange — the TV shows a 6-digit code, you type it into the app.
  2. Once paired, the cert is remembered, so future connections skip straight to the remote session on the TLS remote port and start sending protobuf RemoteKeyInject messages for every button press.

Varint encoding/decoding for the protobuf framing (androidtv/Varint.kt) was the one bit of "hand-roll a tiny parser" in the whole project — short, but the kind of thing you want unit-tested once and never think about again.

Backend 3: the generic boxes — wireless ADB

This is the one that started as a side investigation and ended up being necessary for the app to be actually useful. A lot of cheap Android TV boxes ship with neither webOS-style SSAP nor Google's certified Remote Service — because they're not Google-certified at all. What they do have, almost always, is ADB over the network (wireless debugging), even if it's not turned on by default.

So the adb/ backend is a pure-JVM ADB client — dadb — that:

  • Generates and persists its own RSA keypair on first run (just like desktop adb does in ~/.android/adbkey).
  • Connects to <box-ip>:5555, performs the ADB CNXN/AUTH handshake, and — first time only — blocks until the user accepts the "Allow USB/wireless debugging?" popup on the TV screen itself.
  • From then on, sends input keyevent KEYCODE_DPAD_UP (and friends) as shell commands for every remote button. The key names from the on-screen D-pad map 1:1 to ADB keycodes, so no separate translation table was needed — "DPAD_UP" becomes "KEYCODE_DPAD_UP" and that's the whole mapping.

No PC, no root, no USB cable — just the phone talking ADB to the TV box over Wi-Fi, same as adb connect would from a laptop.

Backend 4: Bluetooth HID — the fallback of last resort

For the case where none of the above is reachable (TV's not on the same network segment, Wi-Fi's being weird, whatever), there's a Bluetooth HID mode: the phone advertises itself as a HID keyboard/consumer-control device and pairs with the TV the same way a physical Bluetooth remote would.

Finding the TV in the first place

None of this matters if the app can't find the TV on the network without the user typing in an IP address. discovery/ runs SSDP (for webOS/UPnP devices) and mDNS/NSD (for Android TV's _androidtvremote2._tcp service record) in parallel, and once something's found once, it gets cached locally so you don't need a fresh scan every time you open the app.

While that scan runs, there's a slowly rotating wireframe sphere on screen — amber circuit lines on a dark background, somewhere between Jarvis's UI and an Ultron cradle. Entirely unnecessary. Absolutely staying.

TV Remote app's device list screen — the "Jarvis orb" idle over SSDP/mDNS discovery, with a cached LG TV found on the network

The stack

Kotlin + Jetpack Compose, Material You dynamic theming, OkHttp for the WebSocket/HTTP work, Protobuf-lite for the Android TV messages, Bouncy Castle for the cert generation, coroutines everywhere a protocol needs a retry loop (first-time ADB/webOS pairing both block on a human tapping something on the TV, so timeouts matter).

Where it's at

It's a working app I run from source today — webOS and Android TV backends are both verified against real hardware, the ADB backend's protocol correctness is confirmed (shell commands land and execute on a real box), Bluetooth HID is implemented. It's not on the Play Store and doesn't need to be; this was always a "replace one specific broken remote" project, not a product.

Code's here if you want to see how any of the four protocols actually work, or steal the SSAP/Android TV Remote v2 client code for your own project:

github.com/duvanjamid/tv-remote

Next up in this series: why digging into why one of my TV boxes needed its own backend turned into a much bigger problem than "it's just not certified."

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The wireless ADB backend is the part that surprised me most — a pure-JVM ADB client running on the phone itself, sending input keyevent KEYCODE_DPAD_UP straight to a generic box over Wi-Fi, with no PC, root, or USB cable in the loop. And the four-backend split is honest: webOS gets the rich SSAP app grid and channel list, while the uncertified boxes just get key events. For the Bluetooth HID fallback, how does the latency compare with the Wi-Fi backends — does the D-pad still feel responsive, or is the lag noticeable in everyday use?