简体   繁体   English

如何判断ADB是否检测到android设备?

[英]How to tell if android device detected by ADB?

I am using ADB to push files from my java application to a tablet when its connected via USB. 通过USB连接时,我正在使用ADB将文件从Java应用程序推送到平板电脑。 I would like to be able to detect if a device is connected or not via USB by ADB. 我希望能够检测ADB是否通过USB连接了设备。 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? 如果未连接平板电脑,如何修改此代码以解决问题?

Thanks for the help. 谢谢您的帮助。

Andy 安迪

By using ddmlib.jar, which is also used by Eclipse plugins, you can monitor the device connect/disconnect event. 通过使用Eclipse插件也使用的ddmlib.jar,可以监视设备的connect / disconnect事件。 The ddmlib is usually found in the tools/lib directory in Android SDK. ddmlib通常位于Android SDK的tools / lib目录中。 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. 您必须包括ddmlib.jar并根据您的环境更改adb位置。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 appium 或 adb 更改 root android 设备的 IMEI 号码? - How to change the IMEI number of a rooted android device via appium or adb? 如何在Android设备的Mac OS上以Java编程方式执行ADB命令 - How to execute ADB commands programmatically in Java on Mac OS for Android device 检测到连接到PC的Android设备 - Detected android device connected to PC 获取显示在adb中的Android设备ID - Get Android device ID shown in adb adb 命令设置 android 11 设备的音量 - adb command to set volume of android 11 device 如何通过Java在Android设备上运行adb屏幕录像并结束屏幕录像? - How to can I run adb screenrecord on a Android Device from Java and end the screenrecording? 如何通过 ADB 命令和 Java 程序在 Android 虚拟设备 (AVD) 上启用 USB 调试? - How to enable USB debugging on Android Virtual Device (AVD) from ADB commands and by Java program? 转发ADB端口以将数据从Android设备发送到PC的速度 - Speed of forwarding ADB ports to send data from Android device to PC 在Linux上使用Java程序通过ADB在Android设备上安装APK - Installing apk on android device via ADB with Java program on Linux 什么是adb命令列出Android设备上安装的所有浏览器? - What is adb command to list all the browsers installed on android device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM