Unable to disable wifi in android using appium

Environment

  • Appium version : 1.9
  • Last Appium version that did not exhibit the issue (if applicable):
  • Desktop OS/version used to run Appium:
  • Node.js version : v8.11.4
  • Npm : 5.6.0
  • Mobile platform/version under test: Android 6.0
  • emulator/simulator: Nexus5
  • Appium CLI : 4.1.2

Details

I am testing a scenarios that i will test in case of wifi/ data is off and in case of wifi/data is on.
So now i have to automate the turn on/of data/wifi.

I am using the below code snippet:
driver.setConnection(Connection.AIRPLANE);

But I am getting an error:

Cannot resolve method setConnection’(io.appium.java_client.android.Connection)’ .

Is there any other way we can use it to enable/disable wifi ?

@Ayetri
Disconnection:
ConnectionState state = driver.setConnection(new ConnectionStateBuilder()
.withWiFiDisabled()
.build());

to connect wifi:
ConnectionState state = driver.setConnection(new ConnectionStateBuilder()
.withWiFiEnabled()
.build());

try this way!