Java Code Examples for com.android.ddmlib.AndroidDebugBridge#init()

The following examples show how to use com.android.ddmlib.AndroidDebugBridge#init() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HJAdb.java    From HJMirror with MIT License 6 votes vote down vote up
/**
 * Start and find Devices.
 */
public IDevice[] startAndFind() throws Exception {
    if (adb == null) {
        AndroidDebugBridge.init(false);
        DdmPreferences.setTimeOut(20000);
        adb = AndroidDebugBridge.createBridge(getAdbExec(), true);
        if (adb == null) {
            throw new Exception("Adb initilized failed!");
        }
    }
    if (adb.hasInitialDeviceList()) {
        IDevice[] devices = adb.getDevices();
        if (devices != null) {
            return devices;
        } else {
            return new IDevice[]{};
        }
    }
    return new IDevice[]{};
}
 
Example 2
Source File: FrameworkEventManager.java    From FuzzDroid with Apache License 2.0 6 votes vote down vote up
public void connectToAndroidDevice() {
	LoggerHelper.logEvent(MyLevel.RUNTIME, "Connecting to ADB...");				
	if(adb == null) {
	    AndroidDebugBridge.init(false);		    			
//		AndroidDebugBridge bridge = AndroidDebugBridge.createBridge(
//				FrameworkOptions.PLATFORM_TOOLS + File.separator + "adb", true);
		adb = AndroidDebugBridge.createBridge();				
	}
	
	waitForDevice();		
	this.device = getDevice(FrameworkOptions.devicePort);
	if(this.device == null) {
		LoggerHelper.logEvent(MyLevel.EXCEPTION_RUNTIME, String.format("Device with port %s not found! -- retry it", FrameworkOptions.devicePort));
		connectToAndroidDevice();
	}
	LoggerHelper.logEvent(MyLevel.RUNTIME, "Successfully connected to ADB...");		
}
 
Example 3
Source File: AdbDevice.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
private void initDebugBridge() {
	if (bridge == null) {
		AndroidDebugBridge.init(false);
	}
	
	if ((bridge == null) || (!bridge.isConnected())) {
		String adbLocation = System.getProperty("user.dir");
		System.out.println(adbLocation);
		if ((adbLocation != null) && (adbLocation.length() != 0)) {
			adbLocation = adbLocation + File.separator + "adb";
			File file= new File(adbLocation);
			if(!file.exists())
				adbLocation = "adb";
		}else{
			adbLocation = "adb";
		}
		bridge = AndroidDebugBridge.createBridge(adbLocation, false);
	}
}
 
Example 4
Source File: ADB.java    From MiniCapAndMiniTouch with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化adb连接
 * @return
 */
private boolean init() {
    boolean success = false;
    if (!hasInitAdb){
        String adbPath = getADBPath();
        if (adbPath != null) {
            AndroidDebugBridge.init(false);
            mAndroidDebugBridge = AndroidDebugBridge.createBridge(adbPath, true);
            if (mAndroidDebugBridge != null) {
                success = true;
                hasInitAdb = true;
            }
            // 延时处理adb获取设备信息
            if (success) {
                int loopCount = 0;
                while (mAndroidDebugBridge.hasInitialDeviceList() == false) {
                    try {
                        Thread.sleep(100);
                        loopCount++;
                    } catch (InterruptedException e) {
                    }
                    if (loopCount > 100) {
                        success = false;
                        break;
                    }
                }
            }
        }
    }

    return success;
}
 
Example 5
Source File: AwoInstaller.java    From atlas with Apache License 2.0 5 votes vote down vote up
protected static AndroidDebugBridge initAndroidDebugBridge(AndroidBuilder androidBuilder) {
    synchronized (ADB_LOCK) {
        if (!adbInitialized) {
            DdmPreferences.setTimeOut(adbConnectionTimeout);
            AndroidDebugBridge.init(false);
            adbInitialized = true;
        }
        AndroidDebugBridge androidDebugBridge = AndroidDebugBridge.createBridge(
            androidBuilder.getSdkInfo().getAdb().getAbsolutePath(), false);
        waitUntilConnected(androidDebugBridge);
        return androidDebugBridge;
    }
}
 
Example 6
Source File: AdbHelper.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Creates connection to adb and waits for this connection to be initialized and receive initial
 * list of devices.
 */
@Nullable
@SuppressWarnings("PMD.EmptyCatchBlock")
private static AndroidDebugBridge createAdb(
    AndroidPlatformTarget androidPlatformTarget, ExecutionContext context, int adbTimeout)
    throws InterruptedException {
  DdmPreferences.setTimeOut(adbTimeout);

  try {
    AndroidDebugBridge.init(/* clientSupport */ false);
  } catch (IllegalStateException ex) {
    // ADB was already initialized, we're fine, so just ignore.
  }

  String adbExecutable = androidPlatformTarget.getAdbExecutable().toString();
  log.debug("Using %s to create AndroidDebugBridge", adbExecutable);
  AndroidDebugBridge adb = AndroidDebugBridge.createBridge(adbExecutable, false);
  if (adb == null) {
    context
        .getConsole()
        .printBuildFailure("Failed to connect to adb. Make sure adb server is running.");
    return null;
  }

  long start = System.currentTimeMillis();
  while (!isAdbInitialized(adb)) {
    long timeLeft = start + ADB_CONNECT_TIMEOUT_MS - System.currentTimeMillis();
    if (timeLeft <= 0) {
      break;
    }
    Thread.sleep(ADB_CONNECT_TIME_STEP_MS);
  }
  return isAdbInitialized(adb) ? adb : null;
}
 
Example 7
Source File: ADB.java    From agent with MIT License 4 votes vote down vote up
public static void addDeviceChangeListener(AndroidDebugBridge.IDeviceChangeListener deviceChangeListener) {
    AndroidDebugBridge.init(false);
    AndroidDebugBridge.createBridge(getPath(), false);
    AndroidDebugBridge.addDeviceChangeListener(deviceChangeListener);
}
 
Example 8
Source File: EventReader.java    From droidtestrec with Apache License 2.0 4 votes vote down vote up
public EventReader(EventListener eventListener) {
    this.eventListener = eventListener;
    AndroidDebugBridge.init(false);
}
 
Example 9
Source File: DeviceConnectHelper.java    From Android-Monkey-Adapter with Apache License 2.0 4 votes vote down vote up
public static void init(final String adbLocation) {
	AndroidDebugBridge.init(false);
   	AndroidDebugBridge.createBridge(adbLocation, true);

}
 
Example 10
Source File: ADB.java    From android-screen-monitor with Apache License 2.0 4 votes vote down vote up
public boolean initialize(String[] args) {
		boolean success = true;

		String adbLocation = System
				.getProperty("com.android.screenshot.bindir");

		// You can specify android sdk directory using first argument
		// A) If you lunch jar from eclipse, set arguments in Run/Debug configurations to android sdk directory .
		//    /Applications/adt-bundle-mac-x86_64/sdk
		// A) If you lunch jar from terminal, set arguments to android sdk directory or $ANDROID_HOME environment variable.
		//    java -jar ./jar/asm.jar $ANDROID_HOME
		if (adbLocation == null) {
			if ((args != null) && (args.length > 0)) {
				adbLocation = args[0];
			} else {
				adbLocation = System.getenv("ANDROID_HOME");
			}
			// Here, adbLocation may be android sdk directory
			if (adbLocation != null) {
				adbLocation += File.separator + "platform-tools";
			}
		}

		// for debugging (follwing line is a example)
//		adbLocation = "C:\\ ... \\android-sdk-windows\\platform-tools"; // Windows
//		adbLocation = "/ ... /adt-bundle-mac-x86_64/sdk/platform-tools"; // MacOS X
		
		if (success) {
			if ((adbLocation != null) && (adbLocation.length() != 0)) {
				adbLocation += File.separator + "adb";
			} else {
				adbLocation = "adb";
			}
			System.out.println("adb path is " + adbLocation);
			AndroidDebugBridge.init(false);
			mAndroidDebugBridge = AndroidDebugBridge.createBridge(adbLocation,
					true);
			if (mAndroidDebugBridge == null) {
				success = false;
			}
		}

		if (success) {
			int count = 0;
			while (mAndroidDebugBridge.hasInitialDeviceList() == false) {
				try {
					Thread.sleep(100);
					count++;
				} catch (InterruptedException e) {
				}
				if (count > 100) {
					success = false;
					break;
				}
			}
		}

		if (!success) {
			terminate();
		}

		return success;
	}