How to turn off Wifi or enable aero plane mode without swipe method

Am not able to turn on aero plane mode or turn off wifi using appium

I used below script:
NetworkConnection mobileDriver = (NetworkConnection) aDriver;
if (mobileDriver.getNetworkConnection() != ConnectionType.AIRPLANE_MODE) {
// enabling Airplane mode
mobileDriver.setNetworkConnection(ConnectionType.AIRPLANE_MODE);

Exception:
744 - org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Error executing adbExec. Original error: ‘Command ‘C:\Users\TestUser\AppData\Local\Android\Sdk\platform-tools\adb.exe -P 5037 -s ce11160bd2dwqeqa3 shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true’ exited with code 4294967295’; Stderr: 'Security exception: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=28306, uid=2000

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=28306, uid=2000
at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:23593)
at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:23442)
at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:24271)
at com.android.server.am.ActivityManagerShellCommand.runSendBroadcast(ActivityManagerShellCommand.java:642)
at com.android.server.am.ActivityManagerShellCommand.onCommand(ActivityManagerShellCommand.java:155)
at android.os.ShellCommand.exec(ShellCommand.java:96)
at com.android.server.am.ActivityManagerService.onShellCommand(ActivityManagerService.java:18871)
at android.os.Binder.shellCommand(Binder.java:581)
at android.os.Binder.onTransact(Binder.java:481)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:4775)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3862)
at android.os.Binder.execTransact(Binder.java:682)’; Code: ‘4294967295’ (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 7.85 seconds

Android does not support programmatic airplane mode switch since version 7.0

Please suggest me alternative solution.

Hi, I’m using this custom methods to turn of and of the connection, I’m switching off the wifi, I don’t need Airplane.
Of course you can customize to turn on Airplane mode .withAirplaneModeEnabled().
required java client 6.0.0.

public void setConnectionToOFF() {
try {
driver.setConnection(new ConnectionStateBuilder().withWiFiDisabled().build());
System.out.println("Switching OFF the connection : " + driver.getConnection());
} catch (Exception e) {
System.out.println(“Connection could not be switch OFF”);
}
}

public void setConnectionToON() {
	try {
		driver.setConnection(new ConnectionStateBuilder().withWiFiEnabled().build());
		System.out.println("Switching On the connection: " + driver.getConnection());
	} catch (Exception e) {
		System.out.println("Connection could not be switch ON");
	}
}

Is this running in Oreo?

unfortunately not, you’ll get connection refused. Although the driver manage to close the WIFI :))
I’m running the tests on 6.0 so there is working.
I might suggest to do it as well. but depends on your app.
When i’m trying to run the test on 8 or 7, there is a 80% change that the test will fail. mostly the driver cannot find some elements.

Please use this below method for any Android OS:

public static void setAndroidDeviceAirplaneMode( boolean status) {

try {

String airplaneModeStatus = “”;

if (status) {

airplaneModeStatus = “1”;

} else {

airplaneModeStatus = “0”;

}

String sdkPath = System. getenv (“ANDROID_HOME”) + “/platform-tools/”;

Runtime. getRuntime ().exec(sdkPath + "adb shell settings put global airplane_mode_on " + airplaneModeStatus);

Thread. sleep (1000);

Process process = Runtime. getRuntime ()

.exec(sdkPath + “adb shell am broadcast -a android.intent.action.AIRPLANE_MODE”);

process.waitFor();

Thread. sleep (4000);

if (status) {

logger .info(“Android device Airplane mode status is set to ON”);

} else {

logger .info(“Android device Airplane mode status is set to OFF”);

}

} catch (Exception e) {

System. out .println(e.getMessage());

logger .error(“Unable to set android device Airplane mode.”);

}

}

1 Like