There might be an issue with your code, try something like this, just adjust the code to your needs.
package com.metricell;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.connection.ConnectionStateBuilder;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
import java.net.URL;
//This method is used to setup the driver and testing environment. Will need to call this at the start of every test
public class MetricellTest {
private AppiumDriver driver;
@Before
public void setUp() throws IOException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.DEVICE_NAME, “ZY224Gs7NG”);
caps.setCapability(MobileCapabilityType.APP, “com.metricell.datalogger”);
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, “com.metricell.datalogger.ui.MainActivity”);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, “MobilePlatform.ANDROID”);
caps.setCapability(MobileCapabilityType.NO_RESET, “True”);
// TODO Screenshots for test failures before/after caps.setCapability(MobileCapabilityType.TAKES_SCREENSHOT, “true”);
driver = new AppiumDriver(new URL(“http://localhost:4723/wd/hub”), caps);
}
@After
public void tearDown() {
try {
driver.quit();
} catch (Exception ign) {}
}
@Test
public void setConnectionToOFf() {
try {
driver.setConnection(new ConnectionStateBuilder().withWiFiDisabled().build());
log.info("Switching OFF the connection : " + driver.getConnection());
} catch (Exception e) {
log.info(“Connection could not be switch OFF”);
}
@Test
public void setConnectionToON() {
try {
driver.setConnection(new ConnectionStateBuilder().withWiFiEnabled().build());
log.info("Switching On the connection: " + driver.getConnection());
} catch (Exception e) {
log.info(“Connection could not be switch ON”);
}
}
}