Execute ADB Commands To Query a Particular Android Device/Emulator

When using the versatile Android Debug Bridge (adb) tool to issue commands, if there’s only one device or emulator attached then that’s fine as the commands will be executed on exactly that. But if there’s more than one instance then we’ll get an error on the terminal saying error: more than one device and emulator.

In such cases, we’ll need to direct our commands to a specific device doing which is pretty simple. First we should check out all the devices attached:

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

$ adb devices
List of devices attached
017296d5904c    	device	device

The dump generated basically consists of two pieces of data in every line. The first bit (017296d5904c for example) is the serial number which is a unique identifier for each device whereas the second portion is the connection state of the instance whose value is device in this case which means that the instance is connected to the adb server. Other values for connection state can be offline (not connected to adb or not responding) or no device (no emulator/device connected).

Now in order to direct an adb command to a particular device all we need to do is use the -s option to specify the serial number of the device.

# my android device
$ adb -s 017296d5904c shell pm list packages
# my android emulator
$ adb -s shell pm list packages

Now if there’s only one attached USB device and/or only one running emulator instance then we don’t need the serial number. Instead we can just use the -d and -e options respectively.

# my android device
$ adb -d shell pm list packages
# my android emulator
$ adb -e shell pm list packages

Hope that helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Author: Rishabh

Rishabh is a full stack web and mobile developer from India. Follow me on Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *