7

Currently when I run adb devices it gives me a list of devices that looks something like:

emulator-5554    device
emulator-5556    device

My goal is to find a command that I can run in the shell that takes a device name as a parameter, e.g. Nexus7 and returns the corresponding device serial, e.g. emulator-5554. If that isn't possible, I want to be able to have a function that takes emulator-5554 as the parameter and returns Nexus7 (the opposite direction of the former function) which I will then loop over all the devices in adb devices and figure out which one matches Nexus7.

UPDATE

I found a workaround solution which was to specify the port number when I launch the avd and then I know which emulator maps to which avd name, but ideally I would still like to know the answer here.

Kvass
  • 8,294
  • 12
  • 65
  • 108

2 Answers2

9

It's possible with telnet to emulator. It's not a single command unfortunately, but in general it's possible to automate it with shell. Here is the basic idea:

  1. Find emulator's port number (5554):

    $ adb devices
    List of devices attached
    emulator-5554   device
    
  2. Telnet to emulator:

    $ telnet localhost 5554
    Trying 127.0.0.1...
    Connected to localhost.
    ...
    OK
    avd name
    Nexus7
    

Nexus7 is the avd name.

See also this answer to find how to make telnetting in one line: https://stackoverflow.com/a/5608081

Community
  • 1
  • 1
alex.dorokhov
  • 1,218
  • 12
  • 17
0
  • get the list of PIDs for all running emulator processes
  • parse their cmdlines, collect avd names
  • check tcp ports opened by those processes
  • match open ports against adb devices output
Alex P.
  • 30,437
  • 17
  • 118
  • 169