Search code examples
androidadb

programmatically obtain a list of android devices connected to adb


Is there any way to programmatically obtain a list of android devices connected to adb ?

I want to do it in Java on a desktop application. I have the path of adb.exe but a parsing on the "adb devices" response doesn't seems to be the best idea.

Is there a more reliable method ?


Solution

  • The optimal solution depends on what you are going to do with the list. If you are going to run some adb commands on the devices you find - parsing adb devices output is the obvious choice. Another option would be going through the USB stack and collecting serial numbers of devices with ADB interface enumerated. Or my favorite one - under Linux I just create a custom udev rule which keeps track of all connected ADB devices. Mine is using a database, but it could be as simple as creating a symlink for every ADB device and then checking the file list.