以编程方式获取连接到 adb 的 android 设备列表

Bud*_*rin 3 android adb

有没有办法以编程方式获取连接到 adb 的 android 设备列表?

我想在桌面应用程序上用 Java 做这件事。我有 adb.exe 的路径,但解析“adb devices”响应似乎不是最好的主意。

有没有更可靠的方法?

Bud*_*rin 5

这就是我做的

try {
        Process process = Runtime.getRuntime().exec("adb devices");
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));  
        String line = null;  

        Pattern pattern = Pattern.compile("^([a-zA-Z0-9\\-]+)(\\s+)(device)");
        Matcher matcher;

        while ((line = in.readLine()) != null) {  
            if (line.matches(pattern.pattern())) {
                matcher = pattern.matcher(line);
                if (matcher.find())
                    System.out.println(matcher.group(1));
            }
        }  
    } catch (IOException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)