public class

WifiManager

extends Object
java.lang.Object
   ↳ android.net.wifi.WifiManager

Class Overview

This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE). It deals with several categories of items:

  • The list of configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.
  • The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.
  • Results of access point scans, containing enough information to make decisions about what access point to connect to.
  • It defines the names of various Intent actions that are broadcast upon any sort of change in Wi-Fi state.
This is the API to use when performing Wi-Fi specific operations. To perform operations that pertain to network connectivity at an abstract level, use ConnectivityManager.

Summary

Nested Classes
class WifiManager.MulticastLock Allows an application to receive Wifi Multicast packets. 
class WifiManager.WifiLock Allows an application to keep the Wi-Fi radio awake. 
Constants
String ACTION_PICK_WIFI_NETWORK Activity Action: Pick a Wi-Fi network to connect to.
int ERROR_AUTHENTICATING The error code if there was a problem authenticating.
String EXTRA_BSSID The lookup key for a String giving the BSSID of the access point to which we are connected.
String EXTRA_NETWORK_INFO The lookup key for a NetworkInfo object associated with the Wi-Fi network.
String EXTRA_NEW_RSSI The lookup key for an int giving the new RSSI in dBm.
String EXTRA_NEW_STATE The lookup key for a SupplicantState describing the new state Retrieve with getParcelableExtra(String).
String EXTRA_PREVIOUS_WIFI_STATE The previous Wi-Fi state.
String EXTRA_SUPPLICANT_CONNECTED The lookup key for a boolean that indicates whether a connection to the supplicant daemon has been gained or lost.
String EXTRA_SUPPLICANT_ERROR The lookup key for a SupplicantState describing the supplicant error code if any Retrieve with getIntExtra(String, int).
String EXTRA_WIFI_STATE The lookup key for an int that indicates whether Wi-Fi is enabled, disabled, enabling, disabling, or unknown.
String NETWORK_IDS_CHANGED_ACTION The network IDs of the configured networks could have changed.
String NETWORK_STATE_CHANGED_ACTION Broadcast intent action indicating that the state of Wi-Fi connectivity has changed.
String RSSI_CHANGED_ACTION The RSSI (signal strength) has changed.
String SCAN_RESULTS_AVAILABLE_ACTION An access point scan has completed, and results are available from the supplicant.
String SUPPLICANT_CONNECTION_CHANGE_ACTION Broadcast intent action indicating that a connection to the supplicant has been established (and it is now possible to perform Wi-Fi operations) or the connection to the supplicant has been lost.
String SUPPLICANT_STATE_CHANGED_ACTION Broadcast intent action indicating that the state of establishing a connection to an access point has changed.One extra provides the new SupplicantState.
int WIFI_MODE_FULL In this Wi-Fi lock mode, Wi-Fi will be kept active, and will behave normally, i.e., it will attempt to automatically establish a connection to a remembered access point that is within range, and will do periodic scans if there are remembered access points but none are in range.
int WIFI_MODE_SCAN_ONLY In this Wi-Fi lock mode, Wi-Fi will be kept active, but the only operation that will be supported is initiation of scans, and the subsequent reporting of scan results.
String WIFI_STATE_CHANGED_ACTION Broadcast intent action indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown.
int WIFI_STATE_DISABLED Wi-Fi is disabled.
int WIFI_STATE_DISABLING Wi-Fi is currently being disabled.
int WIFI_STATE_ENABLED Wi-Fi is enabled.
int WIFI_STATE_ENABLING Wi-Fi is currently being enabled.
int WIFI_STATE_UNKNOWN Wi-Fi is in an unknown state.
Public Methods
int addNetwork(WifiConfiguration config)
Add a new network description to the set of configured networks.
static int calculateSignalLevel(int rssi, int numLevels)
Calculates the level of the signal.
static int compareSignalLevel(int rssiA, int rssiB)
Compares two signal strengths.
WifiManager.MulticastLock createMulticastLock(String tag)
Create a new MulticastLock
WifiManager.WifiLock createWifiLock(int lockType, String tag)
Creates a new WifiLock.
WifiManager.WifiLock createWifiLock(String tag)
Creates a new WifiLock.
boolean disableNetwork(int netId)
Disable a configured network.
boolean disconnect()
Disassociate from the currently active access point.
boolean enableNetwork(int netId, boolean disableOthers)
Allow a previously configured network to be associated with.
List<WifiConfiguration> getConfiguredNetworks()
Return a list of all the networks configured in the supplicant.
WifiInfo getConnectionInfo()
Return dynamic information about the current Wi-Fi connection, if any is active.
DhcpInfo getDhcpInfo()
Return the DHCP-assigned addresses from the last successful DHCP request, if any.
List<ScanResult> getScanResults()
Return the results of the latest access point scan.
int getWifiState()
Gets the Wi-Fi enabled state.
boolean isWifiEnabled()
Return whether Wi-Fi is enabled or disabled.
boolean pingSupplicant()
Check that the supplicant daemon is responding to requests.
boolean reassociate()
Reconnect to the currently active access point, even if we are already connected.
boolean reconnect()
Reconnect to the currently active access point, if we are currently disconnected.
boolean removeNetwork(int netId)
Remove the specified network from the list of configured networks.
boolean saveConfiguration()
Tell the supplicant to persist the current list of configured networks.
boolean setWifiEnabled(boolean enabled)
Enable or disable Wi-Fi.
boolean startScan()
Request a scan for access points.
int updateNetwork(WifiConfiguration config)
Update the network description of an existing configured network.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String ACTION_PICK_WIFI_NETWORK

Since: API Level 1

Activity Action: Pick a Wi-Fi network to connect to.

Input: Nothing.

Output: Nothing.

Constant Value: "android.net.wifi.PICK_WIFI_NETWORK"

public static final int ERROR_AUTHENTICATING

Since: API Level 1

The error code if there was a problem authenticating.

Constant Value: 1 (0x00000001)

public static final String EXTRA_BSSID

Since: API Level 1

The lookup key for a String giving the BSSID of the access point to which we are connected. Only present when the new state is CONNECTED. Retrieve with getStringExtra(String).

Constant Value: "bssid"

public static final String EXTRA_NETWORK_INFO

Since: API Level 1

The lookup key for a NetworkInfo object associated with the Wi-Fi network. Retrieve with getParcelableExtra(String).

Constant Value: "networkInfo"

public static final String EXTRA_NEW_RSSI

Since: API Level 1

The lookup key for an int giving the new RSSI in dBm.

Constant Value: "newRssi"

public static final String EXTRA_NEW_STATE

Since: API Level 1

The lookup key for a SupplicantState describing the new state Retrieve with getParcelableExtra(String).

Constant Value: "newState"

public static final String EXTRA_PREVIOUS_WIFI_STATE

Since: API Level 1

The previous Wi-Fi state.

See Also
Constant Value: "previous_wifi_state"

public static final String EXTRA_SUPPLICANT_CONNECTED

Since: API Level 1

The lookup key for a boolean that indicates whether a connection to the supplicant daemon has been gained or lost. true means a connection now exists. Retrieve it with getBooleanExtra(String, boolean).

Constant Value: "connected"

public static final String EXTRA_SUPPLICANT_ERROR

Since: API Level 1

The lookup key for a SupplicantState describing the supplicant error code if any Retrieve with getIntExtra(String, int).

Constant Value: "supplicantError"

public static final String EXTRA_WIFI_STATE

Since: API Level 1

The lookup key for an int that indicates whether Wi-Fi is enabled, disabled, enabling, disabling, or unknown. Retrieve it with getIntExtra(String, int).

Constant Value: "wifi_state"

public static final String NETWORK_IDS_CHANGED_ACTION

Since: API Level 1

The network IDs of the configured networks could have changed.

Constant Value: "android.net.wifi.NETWORK_IDS_CHANGED"

public static final String NETWORK_STATE_CHANGED_ACTION

Since: API Level 1

Broadcast intent action indicating that the state of Wi-Fi connectivity has changed. One extra provides the new state in the form of a NetworkInfo object. If the new state is CONNECTED, a second extra may provide the BSSID of the access point, as a String.

Constant Value: "android.net.wifi.STATE_CHANGE"

public static final String RSSI_CHANGED_ACTION

Since: API Level 1

The RSSI (signal strength) has changed.

See Also
Constant Value: "android.net.wifi.RSSI_CHANGED"

public static final String SCAN_RESULTS_AVAILABLE_ACTION

Since: API Level 1

An access point scan has completed, and results are available from the supplicant. Call getScanResults() to obtain the results.

Constant Value: "android.net.wifi.SCAN_RESULTS"

public static final String SUPPLICANT_CONNECTION_CHANGE_ACTION

Since: API Level 1

Broadcast intent action indicating that a connection to the supplicant has been established (and it is now possible to perform Wi-Fi operations) or the connection to the supplicant has been lost. One extra provides the connection state as a boolean, where true means CONNECTED.

Constant Value: "android.net.wifi.supplicant.CONNECTION_CHANGE"

public static final String SUPPLICANT_STATE_CHANGED_ACTION

Since: API Level 1

Broadcast intent action indicating that the state of establishing a connection to an access point has changed.One extra provides the new SupplicantState. Note that the supplicant state is Wi-Fi specific, and is not generally the most useful thing to look at if you are just interested in the overall state of connectivity.

Constant Value: "android.net.wifi.supplicant.STATE_CHANGE"

public static final int WIFI_MODE_FULL

Since: API Level 3

In this Wi-Fi lock mode, Wi-Fi will be kept active, and will behave normally, i.e., it will attempt to automatically establish a connection to a remembered access point that is within range, and will do periodic scans if there are remembered access points but none are in range.

Constant Value: 1 (0x00000001)

public static final int WIFI_MODE_SCAN_ONLY

Since: API Level 3

In this Wi-Fi lock mode, Wi-Fi will be kept active, but the only operation that will be supported is initiation of scans, and the subsequent reporting of scan results. No attempts will be made to automatically connect to remembered access points, nor will periodic scans be automatically performed looking for remembered access points. Scans must be explicitly requested by an application in this mode.

Constant Value: 2 (0x00000002)

public static final String WIFI_STATE_CHANGED_ACTION

Since: API Level 1

Broadcast intent action indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown. One extra provides this state as an int. Another extra provides the previous state, if available.

Constant Value: "android.net.wifi.WIFI_STATE_CHANGED"

public static final int WIFI_STATE_DISABLED

Since: API Level 1

Wi-Fi is disabled.

Constant Value: 1 (0x00000001)

public static final int WIFI_STATE_DISABLING

Since: API Level 1

Wi-Fi is currently being disabled. The state will change to WIFI_STATE_DISABLED if it finishes successfully.

Constant Value: 0 (0x00000000)

public static final int WIFI_STATE_ENABLED

Since: API Level 1

Wi-Fi is enabled.

Constant Value: 3 (0x00000003)

public static final int WIFI_STATE_ENABLING

Since: API Level 1

Wi-Fi is currently being enabled. The state will change to WIFI_STATE_ENABLED if it finishes successfully.

Constant Value: 2 (0x00000002)

public static final int WIFI_STATE_UNKNOWN

Since: API Level 1

Wi-Fi is in an unknown state. This state will occur when an error happens while enabling or disabling.

Constant Value: 4 (0x00000004)

Public Methods

public int addNetwork (WifiConfiguration config)

Since: API Level 1

Add a new network description to the set of configured networks. The networkId field of the supplied configuration object is ignored.

The new network will be marked DISABLED by default. To enable it, called enableNetwork(int, boolean).

Parameters
config the set of variables that describe the configuration, contained in a WifiConfiguration object.
Returns
  • the ID of the newly created network description. This is used in other operations to specified the network to be acted upon. Returns -1 on failure.

public static int calculateSignalLevel (int rssi, int numLevels)

Since: API Level 1

Calculates the level of the signal. This should be used any time a signal is being shown.

Parameters
rssi The power of the signal measured in RSSI.
numLevels The number of levels to consider in the calculated level.
Returns
  • A level of the signal, given in the range of 0 to numLevels-1 (both inclusive).

public static int compareSignalLevel (int rssiA, int rssiB)

Since: API Level 1

Compares two signal strengths.

Parameters
rssiA The power of the first signal measured in RSSI.
rssiB The power of the second signal measured in RSSI.
Returns
  • Returns <0 if the first signal is weaker than the second signal, 0 if the two signals have the same strength, and >0 if the first signal is stronger than the second signal.

public WifiManager.MulticastLock createMulticastLock (String tag)

Since: API Level 4

Create a new MulticastLock

Parameters
tag a tag for the MulticastLock to identify it in debugging messages. This string is never shown to the user under normal conditions, but should be descriptive enough to identify your application and the specific MulticastLock within it, if it holds multiple MulticastLocks.
Returns
  • a new, unacquired MulticastLock with the given tag.

public WifiManager.WifiLock createWifiLock (int lockType, String tag)

Since: API Level 3

Creates a new WifiLock.

Parameters
lockType the type of lock to create. See WIFI_MODE_FULL and WIFI_MODE_SCAN_ONLY for descriptions of the types of Wi-Fi locks.
tag a tag for the WifiLock to identify it in debugging messages. This string is never shown to the user under normal conditions, but should be descriptive enough to identify your application and the specific WifiLock within it, if it holds multiple WifiLocks.
Returns
  • a new, unacquired WifiLock with the given tag.

public WifiManager.WifiLock createWifiLock (String tag)

Since: API Level 1

Creates a new WifiLock.

Parameters
tag a tag for the WifiLock to identify it in debugging messages. This string is never shown to the user under normal conditions, but should be descriptive enough to identify your application and the specific WifiLock within it, if it holds multiple WifiLocks.
Returns
  • a new, unacquired WifiLock with the given tag.

public boolean disableNetwork (int netId)

Since: API Level 1

Disable a configured network. The specified network will not be a candidate for associating. This may result in the asynchronous delivery of state change events.

Parameters
netId the ID of the network as returned by addNetwork(WifiConfiguration).
Returns
  • true if the operation succeeded

public boolean disconnect ()

Since: API Level 1

Disassociate from the currently active access point. This may result in the asynchronous delivery of state change events.

Returns
  • true if the operation succeeded

public boolean enableNetwork (int netId, boolean disableOthers)

Since: API Level 1

Allow a previously configured network to be associated with. If disableOthers is true, then all other configured networks are disabled, and an attempt to connect to the selected network is initiated. This may result in the asynchronous delivery of state change events.

Parameters
netId the ID of the network in the list of configured networks
disableOthers if true, disable all other networks. The way to select a particular network to connect to is specify true for this parameter.
Returns
  • true if the operation succeeded

public List<WifiConfiguration> getConfiguredNetworks ()

Since: API Level 1

Return a list of all the networks configured in the supplicant. Not all fields of WifiConfiguration are returned. Only the following fields are filled in:

  • networkId
  • SSID
  • BSSID
  • priority
  • allowedProtocols
  • allowedKeyManagement
  • allowedAuthAlgorithms
  • allowedPairwiseCiphers
  • allowedGroupCiphers

Returns
  • a list of network configurations in the form of a list of WifiConfiguration objects.

public WifiInfo getConnectionInfo ()

Since: API Level 1

Return dynamic information about the current Wi-Fi connection, if any is active.

Returns
  • the Wi-Fi information, contained in WifiInfo.

public DhcpInfo getDhcpInfo ()

Since: API Level 1

Return the DHCP-assigned addresses from the last successful DHCP request, if any.

Returns
  • the DHCP information

public List<ScanResult> getScanResults ()

Since: API Level 1

Return the results of the latest access point scan.

Returns
  • the list of access points found in the most recent scan.

public int getWifiState ()

Since: API Level 1

public boolean isWifiEnabled ()

Since: API Level 1

Return whether Wi-Fi is enabled or disabled.

Returns
  • true if Wi-Fi is enabled
See Also

public boolean pingSupplicant ()

Since: API Level 1

Check that the supplicant daemon is responding to requests.

Returns
  • true if we were able to communicate with the supplicant and it returned the expected response to the PING message.

public boolean reassociate ()

Since: API Level 1

Reconnect to the currently active access point, even if we are already connected. This may result in the asynchronous delivery of state change events.

Returns
  • true if the operation succeeded

public boolean reconnect ()

Since: API Level 1

Reconnect to the currently active access point, if we are currently disconnected. This may result in the asynchronous delivery of state change events.

Returns
  • true if the operation succeeded

public boolean removeNetwork (int netId)

Since: API Level 1

Remove the specified network from the list of configured networks. This may result in the asynchronous delivery of state change events.

Parameters
netId the integer that identifies the network configuration to the supplicant
Returns
  • true if the operation succeeded

public boolean saveConfiguration ()

Since: API Level 1

Tell the supplicant to persist the current list of configured networks.

Note: It is possible for this method to change the network IDs of existing networks. You should assume the network IDs can be different after calling this method.

Returns
  • true if the operation succeeded

public boolean setWifiEnabled (boolean enabled)

Since: API Level 1

Enable or disable Wi-Fi.

Parameters
enabled true to enable, false to disable.
Returns
  • true if the operation succeeds (or if the existing state is the same as the requested state).

public boolean startScan ()

Since: API Level 1

Request a scan for access points. Returns immediately. The availability of the results is made known later by means of an asynchronous event sent on completion of the scan.

Returns
  • true if the operation succeeded, i.e., the scan was initiated

public int updateNetwork (WifiConfiguration config)

Since: API Level 1

Update the network description of an existing configured network.

Parameters
config the set of variables that describe the configuration, contained in a WifiConfiguration object. It may be sparse, so that only the items that are being changed are non-null. The networkId field must be set to the ID of the existing network being updated.
Returns
  • Returns the networkId of the supplied WifiConfiguration on success.
    Returns -1 on failure, including when the networkId field of the WifiConfiguration does not refer to an existing network.