Change capabilities depends if iOS/Android device is connected or not

I have created Appium test scripts with Android Studio.
Is there any way for Appium to know if real device is connected? Or is there any way to switch capabilities programmatically (device name/udid and app location) depends on if real device is connected or not?
Currently I am manually changing the capability values.

Thanks for your help!

You can set config values that can be passed on the command line or in a config file. If you are using Android, you can use “adb devices” to detect the device(s) and choose one from that list.


Thanks for your reply. I was trying to minimize the command line commands to run the test. So I set capabilities in test script class. (I use Android studio to write test scripts)

I found a work around for this.
I used try/catch to see if real device is connected.

private static RemoteWebDriver getWebDriver(DesiredCapabilities capabilities) throws MalformedURLException {
    return new RemoteWebDriver(new URL(""), capabilities);

try {
    driver = getWebDriver(getDeviceCapabilities());
} catch (Exception e) {
    driver = getWebDriver(getSimulatorCapabilities());

And it is working perfectly fine!