WifiManager

public class WifiManager
extends Object

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


This class provides the primary API for managing all aspects of Wi-Fi connectivity.

On releases before N, this object should only be obtained from an application context, and not from any other derived context to avoid memory leaks within the calling process.

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.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument WifiManager.class or Context.getSystemService(String) with the argument Context.WIFI_SERVICE.

Summary

Nested classes

class WifiManager.LocalOnlyHotspotCallback

Callback class for applications to receive updates about the LocalOnlyHotspot status. 

class WifiManager.LocalOnlyHotspotReservation

LocalOnlyHotspotReservation that contains the WifiConfiguration for the active LocalOnlyHotspot request. 

class WifiManager.MulticastLock

Allows an application to receive Wifi Multicast packets. 

class WifiManager.WifiLock

Allows an application to keep the Wi-Fi radio awake. 

class WifiManager.WpsCallback

This class was deprecated in API level P. This is deprecated  

Constants

String ACTION_PICK_WIFI_NETWORK

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

String ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE

Activity Action: Show a system activity that allows the user to enable scans to be available even with Wi-Fi turned off.

int ERROR_AUTHENTICATING

This constant was deprecated in API level P. This is no longer supported.

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

This constant was deprecated in API level P. This is no longer supported.

String EXTRA_PREVIOUS_WIFI_STATE

The previous Wi-Fi state.

String EXTRA_RESULTS_UPDATED

Lookup key for a boolean extra in intent SCAN_RESULTS_AVAILABLE_ACTION representing if the scan was successful or not.

String EXTRA_SUPPLICANT_CONNECTED

This constant was deprecated in API level P. This is no longer supported.

String EXTRA_SUPPLICANT_ERROR

This constant was deprecated in API level P. This is no longer supported.

String EXTRA_WIFI_INFO

The lookup key for a WifiInfo object giving the information about the access point to which we are connected.

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.

String SUPPLICANT_CONNECTION_CHANGE_ACTION

This constant was deprecated in API level P. This is no longer supported.

String SUPPLICANT_STATE_CHANGED_ACTION

This constant was deprecated in API level P. This is no longer supported.

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_FULL_HIGH_PERF

In this Wi-Fi lock mode, Wi-Fi will be kept active as in mode WIFI_MODE_FULL but it operates at high performance with minimum packet loss and low packet latency even when the device screen is off.

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.

int WPS_AUTH_FAILURE

This constant was deprecated in API level P. This is deprecated

int WPS_OVERLAP_ERROR

This constant was deprecated in API level P. This is deprecated

int WPS_TIMED_OUT

This constant was deprecated in API level P. This is deprecated

int WPS_TKIP_ONLY_PROHIBITED

This constant was deprecated in API level P. This is deprecated

int WPS_WEP_PROHIBITED

This constant was deprecated in API level P. This is deprecated

Public methods

int addNetwork(WifiConfiguration config)

Add a new network description to the set of configured networks.

void addOrUpdatePasspointConfiguration(PasspointConfiguration config)

Add or update a Passpoint configuration.

static int calculateSignalLevel(int rssi, int numLevels)

Calculates the level of the signal.

void cancelWps(WifiManager.WpsCallback listener)

This method was deprecated in API level P. This API is deprecated

static int compareSignalLevel(int rssiA, int rssiB)

Compares two signal strengths.

WifiManager.MulticastLock createMulticastLock(String tag)

Create a new MulticastLock

WifiManager.WifiLock createWifiLock(String tag)

Creates a new WifiLock.

WifiManager.WifiLock createWifiLock(int lockType, 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 attemptConnect)

Allow a previously configured network to be associated with.

List<WifiConfiguration> getConfiguredNetworks()

Return a list of all the networks configured for the current foreground user.

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<PasspointConfiguration> getPasspointConfigurations()

Return the list of installed Passpoint configurations.

List<ScanResult> getScanResults()

Return the results of the latest access point scan.

int getWifiState()

Gets the Wi-Fi enabled state.

boolean is5GHzBandSupported()
boolean isDeviceToApRttSupported()
boolean isEnhancedPowerReportingSupported()
boolean isP2pSupported()
boolean isPreferredNetworkOffloadSupported()
boolean isScanAlwaysAvailable()

Check if scanning is always available.

boolean isTdlsSupported()
boolean isWifiEnabled()

Return whether Wi-Fi is enabled or disabled.

boolean pingSupplicant()

This method was deprecated in API level 26. Will return the output of isWifiEnabled() instead.

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.

void removePasspointConfiguration(String fqdn)

Remove the Passpoint configuration identified by its FQDN (Fully Qualified Domain Name).

boolean saveConfiguration()

This method was deprecated in API level 26. There is no need to call this method - addNetwork(WifiConfiguration), updateNetwork(WifiConfiguration) and removeNetwork(int) already persist the configurations automatically.

void setTdlsEnabled(InetAddress remoteIPAddress, boolean enable)

Enable/Disable TDLS on a specific local route.

void setTdlsEnabledWithMacAddress(String remoteMacAddress, boolean enable)

Similar to setTdlsEnabled(InetAddress, boolean), except this version allows you to specify remote endpoint with a MAC address.

boolean setWifiEnabled(boolean enabled)

Enable or disable Wi-Fi.

void startLocalOnlyHotspot(WifiManager.LocalOnlyHotspotCallback callback, Handler handler)

Request a local only hotspot that an application can use to communicate between co-located devices connected to the created WiFi hotspot.

boolean startScan()

This method was deprecated in API level P. The ability for apps to trigger scan requests will be removed in a future release.

void startWps(WpsInfo config, WifiManager.WpsCallback listener)

This method was deprecated in API level P. This API is deprecated

int updateNetwork(WifiConfiguration config)

Update the network description of an existing configured network.

Protected methods

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Inherited methods

Constants

ACTION_PICK_WIFI_NETWORK

added in API level 1
String ACTION_PICK_WIFI_NETWORK

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

Input: Nothing.

Output: Nothing.

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

ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE

added in API level 18
String ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE

Activity Action: Show a system activity that allows the user to enable scans to be available even with Wi-Fi turned off.

Notification of the result of this activity is posted using the onActivityResult(int, int, Intent) callback. The resultCode will be RESULT_OK if scan always mode has been turned on or RESULT_CANCELED if the user has rejected the request or an error has occurred.

Constant Value: "android.net.wifi.action.REQUEST_SCAN_ALWAYS_AVAILABLE"

ERROR_AUTHENTICATING

added in API level 1
int ERROR_AUTHENTICATING

This constant was deprecated in API level P.
This is no longer supported.

The error code if there was a problem authenticating.

Constant Value: 1 (0x00000001)

EXTRA_BSSID

added in API level 1
String EXTRA_BSSID

The lookup key for a String giving the BSSID of the access point to which we are connected. No longer used.

Constant Value: "bssid"

EXTRA_NETWORK_INFO

added in API level 1
String EXTRA_NETWORK_INFO

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

Constant Value: "networkInfo"

EXTRA_NEW_RSSI

added in API level 1
String EXTRA_NEW_RSSI

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

Constant Value: "newRssi"

EXTRA_NEW_STATE

added in API level 1
String EXTRA_NEW_STATE

This constant was deprecated in API level P.
This is no longer supported.

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

Constant Value: "newState"

EXTRA_PREVIOUS_WIFI_STATE

added in API level 1
String EXTRA_PREVIOUS_WIFI_STATE

The previous Wi-Fi state.

See also:

Constant Value: "previous_wifi_state"

EXTRA_RESULTS_UPDATED

added in API level 23
String EXTRA_RESULTS_UPDATED

Lookup key for a boolean extra in intent SCAN_RESULTS_AVAILABLE_ACTION representing if the scan was successful or not. Scans may fail for multiple reasons, these may include:

  1. An app requested too many scans in a certain period of time. This may lead to additional scan request rejections via "scan throttling" for both foreground and background apps. Note: Apps holding android.Manifest.permission.NETWORK_SETTINGS permission are exempted from scan throttling.
  2. The device is idle and scanning is disabled.
  3. Wifi hardware reported a scan failure.

Constant Value: "resultsUpdated"

EXTRA_SUPPLICANT_CONNECTED

added in API level 1
String EXTRA_SUPPLICANT_CONNECTED

This constant was deprecated in API level P.
This is no longer supported.

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"

EXTRA_SUPPLICANT_ERROR

added in API level 1
String EXTRA_SUPPLICANT_ERROR

This constant was deprecated in API level P.
This is no longer supported.

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

Constant Value: "supplicantError"

EXTRA_WIFI_INFO

added in API level 14
String EXTRA_WIFI_INFO

The lookup key for a WifiInfo object giving the information about the access point to which we are connected. No longer used.

Constant Value: "wifiInfo"

EXTRA_WIFI_STATE

added in API level 1
String EXTRA_WIFI_STATE

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"

NETWORK_IDS_CHANGED_ACTION

added in API level 1
String NETWORK_IDS_CHANGED_ACTION

The network IDs of the configured networks could have changed.

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

NETWORK_STATE_CHANGED_ACTION

added in API level 1
String NETWORK_STATE_CHANGED_ACTION

Broadcast intent action indicating that the state of Wi-Fi connectivity has changed. An extra provides the new state in the form of a NetworkInfo object.

See also:

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

RSSI_CHANGED_ACTION

added in API level 1
String RSSI_CHANGED_ACTION

The RSSI (signal strength) has changed.

See also:

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

SCAN_RESULTS_AVAILABLE_ACTION

added in API level 1
String SCAN_RESULTS_AVAILABLE_ACTION

An access point scan has completed, and results are available. Call getScanResults() to obtain the results. The broadcast intent may contain an extra field with the key EXTRA_RESULTS_UPDATED and a boolean value indicating if the scan was successful.

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

SUPPLICANT_CONNECTION_CHANGE_ACTION

added in API level 1
String SUPPLICANT_CONNECTION_CHANGE_ACTION

This constant was deprecated in API level P.
This is no longer supported.

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"

SUPPLICANT_STATE_CHANGED_ACTION

added in API level 1
String SUPPLICANT_STATE_CHANGED_ACTION

This constant was deprecated in API level P.
This is no longer supported.

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"

WIFI_MODE_FULL

added in API level 3
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.

Constant Value: 1 (0x00000001)

WIFI_MODE_FULL_HIGH_PERF

added in API level 12
int WIFI_MODE_FULL_HIGH_PERF

In this Wi-Fi lock mode, Wi-Fi will be kept active as in mode WIFI_MODE_FULL but it operates at high performance with minimum packet loss and low packet latency even when the device screen is off. This mode will consume more power and hence should be used only when there is a need for such an active connection.

An example use case is when a voice connection needs to be kept active even after the device screen goes off. Holding the regular WIFI_MODE_FULL lock will keep the wifi connection active, but the connection can be lossy. Holding a WIFI_MODE_FULL_HIGH_PERF lock for the duration of the voice call will improve the call quality.

When there is no support from the hardware, this lock mode will have the same behavior as WIFI_MODE_FULL

Constant Value: 3 (0x00000003)

WIFI_MODE_SCAN_ONLY

added in API level 3
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. 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)

WIFI_STATE_CHANGED_ACTION

added in API level 1
String WIFI_STATE_CHANGED_ACTION

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"

WIFI_STATE_DISABLED

added in API level 1
int WIFI_STATE_DISABLED

Wi-Fi is disabled.

Constant Value: 1 (0x00000001)

WIFI_STATE_DISABLING

added in API level 1
int WIFI_STATE_DISABLING

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

Constant Value: 0 (0x00000000)

WIFI_STATE_ENABLED

added in API level 1
int WIFI_STATE_ENABLED

Wi-Fi is enabled.

Constant Value: 3 (0x00000003)

WIFI_STATE_ENABLING

added in API level 1
int WIFI_STATE_ENABLING

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

Constant Value: 2 (0x00000002)

WIFI_STATE_UNKNOWN

added in API level 1
int WIFI_STATE_UNKNOWN

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

Constant Value: 4 (0x00000004)

WPS_AUTH_FAILURE

added in API level 21
int WPS_AUTH_FAILURE

This constant was deprecated in API level P.
This is deprecated

Authentication failure on WPS

Constant Value: 6 (0x00000006)

WPS_OVERLAP_ERROR

added in API level 21
int WPS_OVERLAP_ERROR

This constant was deprecated in API level P.
This is deprecated

WPS overlap detected

Constant Value: 3 (0x00000003)

WPS_TIMED_OUT

added in API level 21
int WPS_TIMED_OUT

This constant was deprecated in API level P.
This is deprecated

WPS timed out

Constant Value: 7 (0x00000007)

WPS_TKIP_ONLY_PROHIBITED

added in API level 21
int WPS_TKIP_ONLY_PROHIBITED

This constant was deprecated in API level P.
This is deprecated

TKIP only prohibited

Constant Value: 5 (0x00000005)

WPS_WEP_PROHIBITED

added in API level 21
int WPS_WEP_PROHIBITED

This constant was deprecated in API level P.
This is deprecated

WEP on WPS is prohibited

Constant Value: 4 (0x00000004)

Public methods

addNetwork

added in API level 1
int addNetwork (WifiConfiguration config)

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 WifiConfiguration: the set of variables that describe the configuration, contained in a WifiConfiguration object. If the WifiConfiguration has an Http Proxy set the calling app must be System, or be provisioned as the Profile or Device Owner.

Returns
int 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.

addOrUpdatePasspointConfiguration

added in API level 26
void addOrUpdatePasspointConfiguration (PasspointConfiguration config)

Add or update a Passpoint configuration. The configuration provides a credential for connecting to Passpoint networks that are operated by the Passpoint service provider specified in the configuration. Each configuration is uniquely identified by its FQDN (Fully Qualified Domain Name). In the case when there is an existing configuration with the same FQDN, the new configuration will replace the existing configuration.

Parameters
config PasspointConfiguration: The Passpoint configuration to be added

Throws
IllegalArgumentException if configuration is invalid
UnsupportedOperationException if Passpoint is not enabled on the device.

calculateSignalLevel

added in API level 1
int calculateSignalLevel (int rssi, int numLevels)

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

Parameters
rssi int: The power of the signal measured in RSSI.

numLevels int: The number of levels to consider in the calculated level.

Returns
int A level of the signal, given in the range of 0 to numLevels-1 (both inclusive).

cancelWps

added in API level 21
void cancelWps (WifiManager.WpsCallback listener)

This method was deprecated in API level P.
This API is deprecated

WPS support has been deprecated from Client mode and this method will immediately trigger onFailed(int) with a generic error.

Parameters
listener WifiManager.WpsCallback: for callbacks on success or failure. Can be null.

Throws
IllegalStateException if the WifiManager instance needs to be initialized again

compareSignalLevel

added in API level 1
int compareSignalLevel (int rssiA, int rssiB)

Compares two signal strengths.

Parameters
rssiA int: The power of the first signal measured in RSSI.

rssiB int: The power of the second signal measured in RSSI.

Returns
int 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.

createMulticastLock

added in API level 4
WifiManager.MulticastLock createMulticastLock (String tag)

Create a new MulticastLock

Parameters
tag String: 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
WifiManager.MulticastLock a new, unacquired MulticastLock with the given tag.

createWifiLock

added in API level 1
WifiManager.WifiLock createWifiLock (String tag)

Creates a new WifiLock.

Parameters
tag String: 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
WifiManager.WifiLock a new, unacquired WifiLock with the given tag.

createWifiLock

added in API level 3
WifiManager.WifiLock createWifiLock (int lockType, String tag)

Creates a new WifiLock.

Parameters
lockType int: the type of lock to create. See WIFI_MODE_FULL, WIFI_MODE_FULL_HIGH_PERF and WIFI_MODE_SCAN_ONLY for descriptions of the types of Wi-Fi locks.

tag String: 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
WifiManager.WifiLock a new, unacquired WifiLock with the given tag.

disableNetwork

added in API level 1
boolean disableNetwork (int netId)

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. Applications are not allowed to disable networks created by other applications.

Parameters
netId int: the ID of the network as returned by addNetwork(WifiConfiguration) or getConfiguredNetworks().

Returns
boolean true if the operation succeeded

disconnect

added in API level 1
boolean disconnect ()

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

Returns
boolean true if the operation succeeded

enableNetwork

added in API level 1
boolean enableNetwork (int netId, boolean attemptConnect)

Allow a previously configured network to be associated with. If attemptConnect is true, an attempt to connect to the selected network is initiated. This may result in the asynchronous delivery of state change events.

Note: If an application's target SDK version is LOLLIPOP or newer, network communication may not use Wi-Fi even if Wi-Fi is connected; traffic may instead be sent through another network, such as cellular data, Bluetooth tethering, or Ethernet. For example, traffic will never use a Wi-Fi network that does not provide Internet access (e.g. a wireless printer), if another network that does offer Internet access (e.g. cellular data) is available. Applications that need to ensure that their network traffic uses Wi-Fi should use APIs such as bindSocket(java.net.Socket), openConnection(java.net.URL), or bindProcessToNetwork(Network) to do so. Applications are not allowed to enable networks created by other applications.

Parameters
netId int: the ID of the network as returned by addNetwork(WifiConfiguration) or getConfiguredNetworks().

attemptConnect boolean: The way to select a particular network to connect to is specify true for this parameter.

Returns
boolean true if the operation succeeded

getConfiguredNetworks

added in API level 1
List<WifiConfiguration> getConfiguredNetworks ()

Return a list of all the networks configured for the current foreground user. Not all fields of WifiConfiguration are returned. Only the following fields are filled in:

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

Returns
List<WifiConfiguration> a list of network configurations in the form of a list of WifiConfiguration objects.

getConnectionInfo

added in API level 1
WifiInfo getConnectionInfo ()

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

In the connected state, access to the SSID and BSSID requires the same permissions as getScanResults(). If such access is not allowed, getSSID() will return "<unknown ssid>" and getBSSID() will return "02:00:00:00:00:00".

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

getDhcpInfo

added in API level 1
DhcpInfo getDhcpInfo ()

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

Returns
DhcpInfo the DHCP information

getPasspointConfigurations

added in API level 26
List<PasspointConfiguration> getPasspointConfigurations ()

Return the list of installed Passpoint configurations. An empty list will be returned when no configurations are installed.

Throws
UnsupportedOperationException if Passpoint is not enabled on the device.

getScanResults

added in API level 1
List<ScanResult> getScanResults ()

Return the results of the latest access point scan.

Returns
List<ScanResult> the list of access points found in the most recent scan. An app must hold ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to get valid results. If there is a remote exception (e.g., either a communication problem with the system service or an exception within the framework) an empty list will be returned.

getWifiState

added in API level 1
int getWifiState ()

Gets the Wi-Fi enabled state.

See also:

is5GHzBandSupported

added in API level 21
boolean is5GHzBandSupported ()

Returns
boolean true if this adapter supports 5 GHz band

isDeviceToApRttSupported

added in API level 21
boolean isDeviceToApRttSupported ()

Returns
boolean true if this adapter supports Device-to-AP RTT

isEnhancedPowerReportingSupported

added in API level 21
boolean isEnhancedPowerReportingSupported ()

Returns
boolean true if this adapter supports advanced power/performance counters

isP2pSupported

added in API level 21
boolean isP2pSupported ()

Returns
boolean true if this adapter supports WifiP2pManager (Wi-Fi Direct)

isPreferredNetworkOffloadSupported

added in API level 21
boolean isPreferredNetworkOffloadSupported ()

Returns
boolean true if this adapter supports offloaded connectivity scan

isScanAlwaysAvailable

added in API level 18
boolean isScanAlwaysAvailable ()

Check if scanning is always available. If this return true, apps can issue startScan() and fetch scan results even when Wi-Fi is turned off. To change this setting, see ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE.

Returns
boolean

isTdlsSupported

added in API level 21
boolean isTdlsSupported ()

Returns
boolean true if this adapter supports Tunnel Directed Link Setup

isWifiEnabled

added in API level 1
boolean isWifiEnabled ()

Return whether Wi-Fi is enabled or disabled.

Returns
boolean true if Wi-Fi is enabled

See also:

pingSupplicant

added in API level 1
boolean pingSupplicant ()

This method was deprecated in API level 26.
Will return the output of isWifiEnabled() instead.

Check that the supplicant daemon is responding to requests.

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

reassociate

added in API level 1
boolean reassociate ()

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
boolean true if the operation succeeded

reconnect

added in API level 1
boolean reconnect ()

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

Returns
boolean true if the operation succeeded

removeNetwork

added in API level 1
boolean removeNetwork (int netId)

Remove the specified network from the list of configured networks. This may result in the asynchronous delivery of state change events. Applications are not allowed to remove networks created by other applications.

Parameters
netId int: the ID of the network as returned by addNetwork(WifiConfiguration) or getConfiguredNetworks().

Returns
boolean true if the operation succeeded

removePasspointConfiguration

added in API level 26
void removePasspointConfiguration (String fqdn)

Remove the Passpoint configuration identified by its FQDN (Fully Qualified Domain Name).

Parameters
fqdn String: The FQDN of the Passpoint configuration to be removed

Throws
IllegalArgumentException if no configuration is associated with the given FQDN.
UnsupportedOperationException if Passpoint is not enabled on the device.

saveConfiguration

added in API level 1
boolean saveConfiguration ()

This method was deprecated in API level 26.
There is no need to call this method - addNetwork(WifiConfiguration), updateNetwork(WifiConfiguration) and removeNetwork(int) already persist the configurations automatically.

Tell the device 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
boolean false Will always return true.

setTdlsEnabled

added in API level 19
void setTdlsEnabled (InetAddress remoteIPAddress, boolean enable)

Enable/Disable TDLS on a specific local route.

TDLS enables two wireless endpoints to talk to each other directly without going through the access point that is managing the local network. It saves bandwidth and improves quality of the link.

This API enables/disables the option of using TDLS. If enabled, the underlying hardware is free to use TDLS or a hop through the access point. If disabled, existing TDLS session is torn down and hardware is restricted to use access point for transferring wireless packets. Default value for all routes is 'disabled', meaning restricted to use access point for transferring packets.

Parameters
remoteIPAddress InetAddress: IP address of the endpoint to setup TDLS with

enable boolean: true = setup and false = tear down TDLS

setTdlsEnabledWithMacAddress

added in API level 19
void setTdlsEnabledWithMacAddress (String remoteMacAddress, boolean enable)

Similar to setTdlsEnabled(InetAddress, boolean), except this version allows you to specify remote endpoint with a MAC address.

Parameters
remoteMacAddress String: MAC address of the remote endpoint such as 00:00:0c:9f:f2:ab

enable boolean: true = setup and false = tear down TDLS

setWifiEnabled

added in API level 1
boolean setWifiEnabled (boolean enabled)

Enable or disable Wi-Fi.

Applications must have the CHANGE_WIFI_STATE permission to toggle wifi.

Parameters
enabled boolean: true to enable, false to disable.

Returns
boolean false if the request cannot be satisfied; true indicates that wifi is either already in the requested state, or in progress toward the requested state.

Throws
java.lang.SecurityException} if the caller is missing required permissions.

startLocalOnlyHotspot

added in API level 26
void startLocalOnlyHotspot (WifiManager.LocalOnlyHotspotCallback callback, Handler handler)

Request a local only hotspot that an application can use to communicate between co-located devices connected to the created WiFi hotspot. The network created by this method will not have Internet access. Each application can make a single request for the hotspot, but multiple applications could be requesting the hotspot at the same time. When multiple applications have successfully registered concurrently, they will be sharing the underlying hotspot. onStarted(LocalOnlyHotspotReservation) is called when the hotspot is ready for use by the application.

Each application can make a single active call to this method. The onStarted(LocalOnlyHotspotReservation) callback supplies the requestor with a WifiManager.LocalOnlyHotspotReservation that contains a WifiConfiguration with the SSID, security type and credentials needed to connect to the hotspot. Communicating this information is up to the application.

If the LocalOnlyHotspot cannot be created, the onFailed(int) method will be called. Example failures include errors bringing up the network or if there is an incompatible operating mode. For example, if the user is currently using Wifi Tethering to provide an upstream to another device, LocalOnlyHotspot will not start due to an incompatible mode. The possible error codes include: ERROR_NO_CHANNEL, ERROR_GENERIC, ERROR_INCOMPATIBLE_MODE and ERROR_TETHERING_DISALLOWED.

Internally, requests will be tracked to prevent the hotspot from being torn down while apps are still using it. The WifiManager.LocalOnlyHotspotReservation object passed in the onStarted(LocalOnlyHotspotReservation) call should be closed when the LocalOnlyHotspot is no longer needed using close(). Since the hotspot may be shared among multiple applications, removing the final registered application request will trigger the hotspot teardown. This means that applications should not listen to broadcasts containing wifi state to determine if the hotspot was stopped after they are done using it. Additionally, once close() is called, applications will not receive callbacks of any kind.

Applications should be aware that the user may also stop the LocalOnlyHotspot through the Settings UI; it is not guaranteed to stay up as long as there is a requesting application. The requestors will be notified of this case via onStopped(). Other cases may arise where the hotspot is torn down (Emergency mode, etc). Application developers should be aware that it can stop unexpectedly, but they will receive a notification if they have properly registered.

Applications should also be aware that this network will be shared with other applications. Applications are responsible for protecting their data on this network (e.g., TLS).

Applications need to have the following permissions to start LocalOnlyHotspot: CHANGE_WIFI_STATE and ACCESS_COARSE_LOCATION. Callers without the permissions will trigger a SecurityException.

Parameters
callback WifiManager.LocalOnlyHotspotCallback: LocalOnlyHotspotCallback for the application to receive updates about operating status.

handler Handler: Handler to be used for callbacks. If the caller passes a null Handler, the main thread will be used.

startScan

added in API level 1
boolean startScan ()

This method was deprecated in API level P.
The ability for apps to trigger scan requests will be removed in a future release.

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.

To initiate a Wi-Fi scan, declare the CHANGE_WIFI_STATE permission in the manifest, and perform these steps:

  1. Invoke the following method: ((WifiManager) getSystemService(WIFI_SERVICE)).startScan()
  2. Register a BroadcastReceiver to listen to SCAN_RESULTS_AVAILABLE_ACTION.
  3. When a broadcast is received, call: ((WifiManager) getSystemService(WIFI_SERVICE)).getScanResults()

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

startWps

added in API level 21
void startWps (WpsInfo config, WifiManager.WpsCallback listener)

This method was deprecated in API level P.
This API is deprecated

WPS suport has been deprecated from Client mode and this method will immediately trigger onFailed(int) with a generic error.

Parameters
config WpsInfo: WPS configuration (does not support LABEL)

listener WifiManager.WpsCallback: for callbacks on success or failure. Can be null.

Throws
IllegalStateException if the WifiManager instance needs to be initialized again

updateNetwork

added in API level 1
int updateNetwork (WifiConfiguration config)

Update the network description of an existing configured network.

Parameters
config WifiConfiguration: 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. If the WifiConfiguration has an Http Proxy set the calling app must be System, or be provisioned as the Profile or Device Owner.

Returns
int 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.

Protected methods

finalize

added in API level 1
void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the Java™ virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
Throwable