How to enable the wifi if we run the code on airplane mode (Android)

Hi Everyone,

Please let me know as to how to enable the wifi. I have used the following codes still no success.

  1. ConnectionStateBuilder csb = new ConnectionStateBuilder();
    csb.withWiFiEnabled().build();

  2. AndroidDriver driver = (AndroidDriver) driver;
    driver.toggleAirplaneMode();

Hello. Try this:

ConnectionStateBuilder connectionStateBuilder = new ConnectionStateBuilder();
((AndroidDriver<?>) driver).setConnection(connectionStateBuilder.withWiFiEnabled().build());

Should work if you have “driver” variable already initialized

Hi @alex-k,

Thanks for replying.

I have used the code and still unable to find the solution to it.

I have used the following code:

HasNetworkConnection driver = (HasNetworkConnection) session.driver;
ConnectionStateBuilder connectionStateBuilder = new ConnectionStateBuilder();
((AndroidDriver<?>) driver).setConnection(connectionStateBuilder.withWiFiEnabled().build());

Do let me know if I am missing anything.

Thanks!

Oh, by “driver” I meant “AppiumDriver driver”, not the “HasNetworkConnection driver”.

Hi @alex-k,

Thanks for replying.

I had used the AppiumDriver as well and getting the following error:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=17700, uid=2000

I am currently using the following code:

AppiumDriver driver = (AppiumDriver) session.driver;

ConnectionStateBuilder connectionStateBuilder = new ConnectionStateBuilder();

((AndroidDriver<?>) driver).setConnection(connectionStateBuilder.withWiFiEnabled().build());

Do let me know if I am missing anything.

Thanks!

maybe add to Appium server start: --relaxed-security true

Disable additional security checks, so it is possible to use some advanced features, provided by drivers supporting this option. Only enable it if all the clients are in the trusted network and it is not the case if a client could potentially break out of the session sandbox. Can override enabling of specific features with --deny-insecure. See also the security doc

Hi @Aleksei,

Still unable to do it as I am getting the same issue. I have used the following code in the capabilities:

  1. capabilities.setCapability("–relaxed-security", “true”);
  2. capabilities.setCapability("–allow-insecure", “true”);

Now, not getting the “SecurityException” issue.

it is no capability! it is command line argument to start Appium server like:

appium --log-level error --port 5001 -–relaxed-security true

Hi @Aleksei,

Still no solution found for this. I had followed the steps which you had told me:

-> had checked the “Relaxed Security” on the Appium Desktop.

If I am missing anything, please do let me know.