如何判断ADB是否检测到Android设备? [英] How to tell if android device detected by ADB?

查看:1446
本文介绍了如何判断ADB是否检测到Android设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过USB连接时,我正在使用ADB将文件从我的java应用程序推送到平板电脑。我希望能够通过ADB检测设备是否通过USB连接。我用来推送文件的代码是:

I am using ADB to push files from my java application to a tablet when its connected via USB. I would like to be able to detect if a device is connected or not via USB by ADB. The code I am using to push the files across is:

public void wiredsync(){
        try {
            abdsourcesync = buildpath; 
            adbtabletsync = "/mnt/sdcard/test"; 
            System.out.println("Starting Sync via adb with command " + "adb" + " push "+ buildpath + " " + adbtabletsync);
                    Process process = Runtime.getRuntime().exec("adb" + " push "+ buildpath + " " + adbtabletsync);
                    InputStreamReader reader = new InputStreamReader(process.getInputStream());
                    Scanner scanner = new Scanner(reader);
                    scanner.close();
                } catch(IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }//end wiredsync

如何将此代码修改为是否已连接平板电脑?

How can I modify this code to work out if a tablet is connected or not?

感谢您的帮助。

Andy

推荐答案

通过使用Eclipse插件也使用的ddmlib.jar,您可以监视设备连接/断开事件。 ddmlib通常位于Android SDK的tools / lib目录中。但是没有关于如何使用它的官方文件。下面是代码示例。您必须包含ddmlib.jar并根据您的环境更改adb位置。

By using ddmlib.jar, which is also used by Eclipse plugins, you can monitor the device connect/disconnect event. The ddmlib is usually found in the tools/lib directory in Android SDK. But there is no the official documents about how to use it. Below is the code example. You have to include the ddmlib.jar and change the adb location according to your environment.

import java.io.IOException;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;
import com.android.ddmlib.IDevice;

public class Main {

    public static void main(String[] args) throws IOException {
        AndroidDebugBridge.init(false);

        AndroidDebugBridge debugBridge = AndroidDebugBridge.createBridge("D:\\android-sdk\\platform-tools\\adb.exe", true);
        if (debugBridge == null) {
            System.err.println("Invalid ADB location.");
            System.exit(1);
        }

        AndroidDebugBridge.addDeviceChangeListener(new IDeviceChangeListener() {

            @Override
            public void deviceChanged(IDevice device, int arg1) {
                // not implement
            }

            @Override
            public void deviceConnected(IDevice device) {
                System.out.println(String.format("%s connected", device.getSerialNumber()));
            }

            @Override
            public void deviceDisconnected(IDevice device) {
                System.out.println(String.format("%s disconnected", device.getSerialNumber()));

            }

        });

        System.out.println("Press enter to exit.");
        System.in.read();
    }
}

这篇关于如何判断ADB是否检测到Android设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆