Turning the wi-fi or mobile data on or off is very simple.
See below code.
AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), dc);
// turn on all (data and wi-fi)
driver.setConnection(Connection.ALL);
assertEquals(Connection.ALL, driver.getConnection());
// turn off all (data and wi-fi)
driver.setConnection(Connection.NONE);
assertEquals(Connection.NONE, driver.getConnection());
// turn on airplane
driver.setConnection(Connection.AIRPLANE);
assertEquals(Connection.AIRPLANE, driver.getConnection());
// turn on data
driver.setConnection(Connection.DATA);
assertEquals(Connection.DATA, driver.getConnection());
// tunr on wi-fi
driver.setConnection(Connection.WIFI);
assertEquals(Connection.WIFI, driver.getConnection());
In Appium API old versions <4.0.0 (for Java) the way to deal with network connection was:
AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), dc);
// init wi-fi and data enabled and airplane mode disabled
NetworkConnectionSetting ncs = new NetworkConnectionSetting(false, true, true);
driver.setNetworkConnection(ncs);
// turn off wi-fi
ncs.setWifi(false);
driver.setNetworkConnection(ncs);
// turn off data
ncs.setData(false);
driver.setNetworkConnection(ncs);
// turn on airplane mde
ncs.setAirplaneMode(true);
driver.setNetworkConnection(ncs);
// turn off airplane mode
ncs.setAirplaneMode(true);
driver.setNetworkConnection(ncs);
// turn on data
ncs.setData(true);
driver.setNetworkConnection(ncs);
// turn on wi-fi
ncs.setWifi(true);
driver.setNetworkConnection(ncs);