to top
public class

WifiHelper

extends Object
implements IWifiHelper
java.lang.Object
   ↳ com.android.tradefed.device.WifiHelper

Class Overview

Helper class for manipulating wifi services on device.

Summary

Constants
String INSTRUMENTATION_PKG
int PACKAGE_VERSION_CODE
Fields
static final String CHECK_PACKAGE_CMD
static final String FULL_INSTRUMENTATION_NAME
static final Pattern PACKAGE_VERSION_PAT
Public Constructors
WifiHelper(ITestDevice device)
Public Methods
boolean addOpenNetwork(String ssid)
Adds the open security network identified by ssid.
boolean addWpaPskNetwork(String ssid, String psk)
Adds the WPA-PSK security network identified by ssid.
boolean checkConnectivity(String urlToCheck)
Checks connectivity by sending HTTP request to the given url.
boolean connectToNetwork(String ssid, String psk, String urlToCheck)
Connects to a wifi network and check connectivity.
boolean disableWifi()
Disables wifi state on device.
boolean disconnectFromNetwork()
Disconnect from the current wifi network and disable wifi.
boolean enableWifi()
Enables wifi state on device.
static File extractWifiUtilApk()
Helper method to extract the wifi util apk from the classpath
String getBSSID()
Gets the basic service set identifier (BSSID) of the currently access point.
String getIpAddress()
Gets the IP address associated with the wifi interface.
String getSSID()
Gets the service set identifier of the currently connected network.
Map<String, String> getWifiInfo()
Gets the current wifi connection information.
boolean hasValidIp()
boolean isWifiEnabled()
Check if wifi is currently enabled.
boolean removeAllNetworks()
Removes all known networks.
boolean startMonitor(long interval, String urlToCheck)
Starts network connectivity monitoring.
List<Long> stopMonitor()
Stops network connectivity monitoring.
boolean waitForIp(long timeout)
Wait until an ip address is assigned to wifi adapter.
boolean waitForWifiDisabled(long timeout)
Wait for isWifiEnabled() to be false.
boolean waitForWifiDisabled()
Wait for isWifiEnabled() to be false with a default timeout.
boolean waitForWifiEnabled()
Wait for isWifiEnabled() to be true with a default timeout.
boolean waitForWifiEnabled(long timeout)
Wait for isWifiEnabled() to be true.
boolean waitForWifiState(WifiState... expectedStates)
Waits until one of the expected wifi states occurs.
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.android.tradefed.device.IWifiHelper

Constants

public static final String INSTRUMENTATION_PKG

Constant Value: "com.android.tradefed.utils.wifi"

static final int PACKAGE_VERSION_CODE

Constant Value: 21 (0x00000015)

Fields

static final String CHECK_PACKAGE_CMD

static final String FULL_INSTRUMENTATION_NAME

static final Pattern PACKAGE_VERSION_PAT

Public Constructors

public WifiHelper (ITestDevice device)

Public Methods

public boolean addOpenNetwork (String ssid)

Adds the open security network identified by ssid.

To connect to any wifi network, a network profile must be created in wpa_supplicant configuration first. This will call wpa_cli to add the open security network identified by ssid.

Parameters
ssid the ssid of network to add.
Returns
  • true if network was added successfully, false otherwise.

public boolean addWpaPskNetwork (String ssid, String psk)

Adds the WPA-PSK security network identified by ssid.

Parameters
ssid the ssid of network to add.
psk the WPA-PSK passphrase to use
Returns
  • true if network was added successfully, false otherwise.

public boolean checkConnectivity (String urlToCheck)

Checks connectivity by sending HTTP request to the given url.

Parameters
urlToCheck a destination url for a HTTP request check
Returns
  • true if the device pass connectivity check.

public boolean connectToNetwork (String ssid, String psk, String urlToCheck)

Connects to a wifi network and check connectivity.

Parameters
ssid the ssid of network to connect
psk the WPA-PSK passphrase to use. This can be null.
urlToCheck a destination url for a HTTP request check
Returns
  • true if the device pass connectivity check.

public boolean disableWifi ()

Disables wifi state on device.

Returns
  • true if wifi was disabled successfully

public boolean disconnectFromNetwork ()

Disconnect from the current wifi network and disable wifi.

Returns
  • true if the operation succeeded.

public boolean enableWifi ()

Enables wifi state on device.

Returns
  • true if wifi was enabled successfully

public static File extractWifiUtilApk ()

Helper method to extract the wifi util apk from the classpath

Throws
IOException

public String getBSSID ()

Gets the basic service set identifier (BSSID) of the currently access point.

public String getIpAddress ()

Gets the IP address associated with the wifi interface. Returns null if there was a failure retrieving ip address.

public String getSSID ()

Gets the service set identifier of the currently connected network.

public Map<String, String> getWifiInfo ()

Gets the current wifi connection information.

This includes SSID, BSSID, IP address, link speed, and RSSI.

Returns
  • a map containing wifi connection information.

public boolean hasValidIp ()

public boolean isWifiEnabled ()

Check if wifi is currently enabled.

public boolean removeAllNetworks ()

Removes all known networks.

public boolean startMonitor (long interval, String urlToCheck)

Starts network connectivity monitoring.

Parameters
interval interval between connectivity checks.
urlToCheck a URL to check connectivity with.
Returns
  • true if the operation succeeded.

public List<Long> stopMonitor ()

Stops network connectivity monitoring.

This also returns the latency history since the last startMonitor(long, String) call.

Returns
  • the latency history.

public boolean waitForIp (long timeout)

Wait until an ip address is assigned to wifi adapter.

Parameters
timeout how long to wait
Returns
  • true if an ip address is assigned before timeout, false otherwise

public boolean waitForWifiDisabled (long timeout)

Wait for isWifiEnabled() to be false.

Parameters
timeout time in ms to wait
Returns
  • true if wifi was disabled before timeout, false otherwise.

public boolean waitForWifiDisabled ()

Wait for isWifiEnabled() to be false with a default timeout.

Returns
  • true if wifi was disabled before timeout, false otherwise.

public boolean waitForWifiEnabled ()

Wait for isWifiEnabled() to be true with a default timeout.

Returns
  • true if wifi was enabled before timeout, false otherwise.

public boolean waitForWifiEnabled (long timeout)

Wait for isWifiEnabled() to be true.

Parameters
timeout time in ms to wait
Returns
  • true if wifi was enabled before timeout, false otherwise.

public boolean waitForWifiState (WifiState... expectedStates)

Waits until one of the expected wifi states occurs.

Parameters
expectedStates one or more wifi states to expect
Returns
  • true if the one of the expected states occurred. false if none of the states occurred before timeout is reached