Driver.isLocked(), Driver.getNetworkConnection().wifiEnabled() do not work if more than one Android device is connected

Background
I am on the latest Java Client v3.2.0.
I am investigating running parallel Appium Android (Java) sessions in the following format:

  • I connect as many Android devices as my machine supports
  • A pre-test script creates as many drivers (with the appropriate capabilities) as devices;
  • The drivers are then passed as parallel data-providers to a TestNG test. This then runs the test in parallel

For the most part, my proof of concept runs fine. I now need to call the Appium method isLocked() on each connected device.

Here are sample logs from Appium Server, for when multiple devices are connected to my machine.
As you can see, Appium is using the adb -s DEVICE_SERIAL method to reference the device.

18	2015-11-25 11:20:39:358 - debug: Using adb from D:\vish\adt-bundle\sdk\platform-tools\adb.exe
32	2015-11-25 11:20:39:397 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe devices
37	2015-11-25 11:20:39:423 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W wait-for-device
38	2015-11-25 11:20:39:448 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "echo 'ready'"
41	2015-11-25 11:20:39:514 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "getprop ro.build.version.sdk"
46	2015-11-25 11:20:39:592 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "getprop persist.sys.language"
48	2015-11-25 11:20:39:639 - debug: java -jar "C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-adb\jars\appium_apk_tools.jar" "stringsFromApk" "D:\vish\ma-app-verification-android\appium-projects\AppiumAndroid\ChatflyAPKs\ChatflyQA.apk" "C:\Users\vish\AppData\Local\Temp\co.shoppin.chatfly.qa" en
50	2015-11-25 11:20:40:797 - debug: java -jar "C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-adb\jars\appium_apk_tools.jar" "stringsFromApk" "D:\vish\ma-app-verification-android\appium-projects\AppiumAndroid\ChatflyAPKs\ChatflyQA.apk" "C:\Users\vish\AppData\Local\Temp\co.shoppin.chatfly.qa"
53	2015-11-25 11:20:41:919 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W push "C:\\Users\\vish\\AppData\\Local\\Temp\\co.shoppin.chatfly.qa\\strings.json" /data/local/tmp
61	2015-11-25 11:20:41:990 - debug: executing cmd: java -jar "C:\Program Files (x86)\Appium\node_modules\appium\node_modules\appium-adb\jars\verify.jar" D:\vish\ma-app-verification-android\appium-projects\AppiumAndroid\ChatflyAPKs\ChatflyQA.apk
69	2015-11-25 11:20:43:360 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "ls /data/local/tmp/ae89887d619d8839772caaf09278ab19.apk"
72	2015-11-25 11:20:43:401 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "getprop ro.build.version.sdk"
74	2015-11-25 11:20:43:437 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "pm list packages -3 co.shoppin.chatfly.qa"
78	2015-11-25 11:20:43:813 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "am force-stop co.shoppin.chatfly.qa"
79	2015-11-25 11:20:44:428 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "pm clear co.shoppin.chatfly.qa"
81	2015-11-25 11:20:45:107 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W forward tcp:4724 tcp:4724
83	2015-11-25 11:20:45:159 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W push "C:\\Program Files (x86)\\Appium\\node_modules\\appium\\build\\android_bootstrap\\AppiumBootstrap.jar" /data/local/tmp/
85	2015-11-25 11:20:45:197 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W install "C:\Program Files (x86)\Appium\node_modules\appium\build\settings_apk\settings_apk-debug.apk"
87	2015-11-25 11:20:46:580 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W install "C:\Program Files (x86)\Appium\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
91	2015-11-25 11:20:47:458 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe -s TA64303N7W shell "ps 'uiautomator'"

Issue

However, this is what happens when I call driver.isLocked().
The actual error is

error: more than one device and emulator

Here are the logs:

2015-11-25 11:28:09:091 - info: <-- GET /wd/hub/session/190a478d-6623-46f3-9d38-245c270664b6 200 1.928 ms - 773 {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"5.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"appPackage":"co.shoppin.chatfly.qa","appActivity":"com.icelero.chatbite.ui.StartScreenActivity","newCommandTimeout":600,"autoLaunch":false,"platformVersion":"5.1","browserName":"","platformName":"Android","deviceName":"MotoX","uuid":"TA64303N7W"},"appPackage":"co.shoppin.chatfly.qa","appActivity":"com.icelero.chatbite.ui.StartScreenActivity","newCommandTimeout":600,"autoLaunch":false,"platformName":"Android","deviceName":"MotoX","uuid":"TA64303N7W"},"sessionId":"190a478d-6623-46f3-9d38-245c270664b6"}
2015-11-25 11:28:09:107 - info: --> POST /wd/hub/session/190a478d-6623-46f3-9d38-245c270664b6/appium/device/is_locked {}
2015-11-25 11:28:09:111 - debug: executing cmd: D:\vish\adt-bundle\sdk\platform-tools\adb.exe shell "dumpsys window"
2015-11-25 11:28:09:137 - debug: Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command. (Original error: Command failed: C:\\Windows\\system32\\cmd.exe /s /c \"D:\\vish\\adt-bundle\\sdk\\platform-tools\\adb.exe shell \"dumpsys window\"\"\nerror: more than one device and emulator\r\n)","killed":false,"code":4294967295,"signal":null,"cmd":"C:\\Windows\\system32\\cmd.exe /s /c \"D:\\vish\\adt-bundle\\sdk\\platform-tools\\adb.exe shell \"dumpsys window\"\"","origValue":"Command failed: C:\\Windows\\system32\\cmd.exe /s /c \"D:\\vish\\adt-bundle\\sdk\\platform-tools\\adb.exe shell \"dumpsys window\"\"\nerror: more than one device and emulator\r\n"},"sessionId":"190a478d-6623-46f3-9d38-245c270664b6"}
2015-11-25 11:28:09:137 - info: <-- POST /wd/hub/session/190a478d-6623-46f3-9d38-245c270664b6/appium/device/is_locked 500 30.583 ms - 716 
2015-11-25 11:28:09:258 - info: --> GET /wd/hub/status {}

Possible Cause
As you can see, it uses the command adb shell dumpsys window instead of adb -s DEVICE_ID. Therefore, it is natural it should fail.

UPDATE
The same problem holds true for Driver.getNetworkConnection().wifiEnabled() and other commands that work with network connection.

UPDATE 2
With Java client 3.3.0 the Driver,getNetworkConnection() etc commands are still failing when multiple devices are connected. I have filed this as setNetworkConnection() does not work if > 1 Android device connected · Issue #336 · appium/java-client · GitHub

This is a pretty serious problem for running multiple Android tests in parallel!

For now, I am working around this issue with my own isLocked() method:
This method will return true if the screen is locked.

public static boolean isLocked(String deviceId) throws Exception {
	String op = exec("adb -s " + deviceId + " shell dumpsys window");
	return op.contains("mShowingLockscreen=true");
}

(note: exec() is a method located in my code that just executes a system command and returns the output.)