Categories
Android

Controlling your Android phone with ADB

Android Debug Bridge (ADB) is a tool that has a lot of power over your Android device and every Android phone has the ability to enable this. ADB can have many user, in web development it’s used to debug a website on your phone. Because it allows you to control and see any content on the page including it’s Javascript console.

It is also used to flash new recoveries.
But it can also do things like:

  • Open apps
  • Swipe actions
  • Tap actions
  • Simulate a power or home button press
  • Type out text
  • Grant permissions you can’t grant normally

Which this post is going to cover.

A bit of back story

Why did i search out all of these ADB commands?
I have lots of cheap Android TV boxes at home and use the open source home automation platform: Home Assistant
And i wanted to be able to control my android box without having to find the remote each time. Which i can now do on my phone.

This post is made with the assumption that ADB is already enabled, if it isn’t check out google’s guide to setting it up.
These are all ADB shell commands, so you will need to run `adb shell` before these commands.

pm list packages

This is simply a command to list all package names available on your android device. This is a great way to see which package name launches the app you want to launch in our next command.

monkey -p <package name> 1

This command allows you to start any package installed on your device.
Monkey is made to cause a bit of chaos by opening random apps, firing touch events, etc.
However when used with a package and event constraint it is perfect to launch apps in a simple way.

input keyevent <key>

Now we get to controlling your phones buttons which can emulate any keyboard button, but also any hardware buttons. Even if your phone doesn’t have them. This is what i use to use my phone as a remote control.
There is a huge list of keyevents to choose from. You can pick any of the codes prefixed with “KEYCODE_” and the command accepts both the constant and the value as a valid key.

input text <text>

I think this kind of explains itself, it enters any text you want into the focused on text field with incredible speed. So you don’t have to fire each keyevent for it.
One thing to note is that spaces in the text will need to be replaced with “%s”

input swipe <x start> <y start> <x end> <y end>
input tap <x> <y>

These two are commands to simulate touchscreen actions, very useful if your phone/device has a lock screen for instance which you have to swipe away.

Let’s put some of it to action

Now that we know all of these commands we can actually do some useful stuff with it. I also have a phone connected to a speaker acting as a radio, however when opening an audio stream while the screen is off it cuts off after a second or so, so i created this command

input keyevent 26; input keyevent 3; input swipe 400 1200 400 10; <command>

What this does is it first locks the screen to make sure it’s off, then it turns the screen on using the home button. And it swipes up to unlock my phone. Then i can launch any app i want to play my audio.