Build 1.0_r1(from source)

android.net.wifi
Class WifiManager

java.lang.Object
  extended by android.net.wifi.WifiManager

public class WifiManager
extends Object

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:

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.


Nested Class Summary
 class WifiManager.WifiLock
          Allows an application to keep the Wi-Fi radio awake.
 
Field Summary
static String ACTION_PICK_WIFI_NETWORK
          Activity Action: Pick a Wi-Fi network to connect to.
static int ERROR_AUTHENTICATING
          The error code if there was a problem authenticating.
static String EXTRA_BSSID
          The lookup key for a String giving the BSSID of the access point to which we are connected.
static String EXTRA_NETWORK_INFO
          The lookup key for a NetworkInfo object associated with the Wi-Fi network.
static String EXTRA_NEW_RSSI
          The lookup key for an int giving the new RSSI in dBm.
static String EXTRA_NEW_STATE
          The lookup key for a SupplicantState describing the new state Retrieve with Intent.getParcelableExtra(String).
static String EXTRA_PREVIOUS_WIFI_STATE
          The previous Wi-Fi state.
static String EXTRA_SUPPLICANT_CONNECTED
          The lookup key for a boolean that indicates whether a connection to the supplicant daemon has been gained or lost.
static String EXTRA_SUPPLICANT_ERROR
          The lookup key for a SupplicantState describing the supplicant error code if any Retrieve with Intent.getIntExtra(String, int).
static String EXTRA_WIFI_STATE
          The lookup key for an int that indicates whether Wi-Fi is enabled, disabled, enabling, disabling, or unknown.
(package private)  Handler mHandler
           
(package private)  IWifiManager mService
           
static String NETWORK_IDS_CHANGED_ACTION
          The network IDs of the configured networks could have changed.
static String NETWORK_STATE_CHANGED_ACTION
          Broadcast intent action indicating that the state of Wi-Fi connectivity has changed.
static String RSSI_CHANGED_ACTION
          The RSSI (signal strength) has changed.
static String SCAN_RESULTS_AVAILABLE_ACTION
          An access point scan has completed, and results are available from the supplicant.
static 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.
static 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.
static String WIFI_STATE_CHANGED_ACTION
          Broadcast intent action indicating that Wi-Fi has been enabled, disabled, enabling, disabling, or unknown.
static int WIFI_STATE_DISABLED
          Wi-Fi is disabled.
static int WIFI_STATE_DISABLING
          Wi-Fi is currently being disabled.
static int WIFI_STATE_ENABLED
          Wi-Fi is enabled.
static int WIFI_STATE_ENABLING
          Wi-Fi is currently being enabled.
static int WIFI_STATE_UNKNOWN
          Wi-Fi is in an unknown state.
 
Constructor Summary
WifiManager(IWifiManager service, Handler handler)
          Create a new WifiManager instance.
 
Method Summary
 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.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ERROR_AUTHENTICATING

public static final int ERROR_AUTHENTICATING
The error code if there was a problem authenticating.

See Also:
Constant Field Values

WIFI_STATE_CHANGED_ACTION

public static final 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.

See Also:
EXTRA_WIFI_STATE, EXTRA_PREVIOUS_WIFI_STATE, Constant Field Values

EXTRA_WIFI_STATE

public static final 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 Intent.getIntExtra(String,int).

See Also:
WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLED, WIFI_STATE_ENABLING, WIFI_STATE_UNKNOWN, Constant Field Values

EXTRA_PREVIOUS_WIFI_STATE

public static final String EXTRA_PREVIOUS_WIFI_STATE
The previous Wi-Fi state.

See Also:
EXTRA_WIFI_STATE, Constant Field Values

WIFI_STATE_DISABLING

public static final int WIFI_STATE_DISABLING
Wi-Fi is currently being disabled. The state will change to WIFI_STATE_DISABLED if it finishes successfully.

See Also:
WIFI_STATE_CHANGED_ACTION, getWifiState(), Constant Field Values

WIFI_STATE_DISABLED

public static final int WIFI_STATE_DISABLED
Wi-Fi is disabled.

See Also:
WIFI_STATE_CHANGED_ACTION, getWifiState(), Constant Field Values

WIFI_STATE_ENABLING

public static final int WIFI_STATE_ENABLING
Wi-Fi is currently being enabled. The state will change to WIFI_STATE_ENABLED if it finishes successfully.

See Also:
WIFI_STATE_CHANGED_ACTION, getWifiState(), Constant Field Values

WIFI_STATE_ENABLED

public static final int WIFI_STATE_ENABLED
Wi-Fi is enabled.

See Also:
WIFI_STATE_CHANGED_ACTION, getWifiState(), Constant Field Values

WIFI_STATE_UNKNOWN

public static final int WIFI_STATE_UNKNOWN
Wi-Fi is in an unknown state. This state will occur when an error happens while enabling or disabling.

See Also:
WIFI_STATE_CHANGED_ACTION, getWifiState(), Constant Field Values

SUPPLICANT_CONNECTION_CHANGE_ACTION

public static final 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. One extra provides the connection state as a boolean, where true means CONNECTED.

See Also:
EXTRA_SUPPLICANT_CONNECTED, Constant Field Values

EXTRA_SUPPLICANT_CONNECTED

public static final String EXTRA_SUPPLICANT_CONNECTED
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 Intent.getBooleanExtra(String,boolean).

See Also:
Constant Field Values

NETWORK_STATE_CHANGED_ACTION

public static final String NETWORK_STATE_CHANGED_ACTION
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.

See Also:
EXTRA_NETWORK_INFO, EXTRA_BSSID, Constant Field Values

EXTRA_NETWORK_INFO

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

See Also:
Constant Field Values

EXTRA_BSSID

public static final String EXTRA_BSSID
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 Intent.getStringExtra(String).

See Also:
Constant Field Values

SUPPLICANT_STATE_CHANGED_ACTION

public static final 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. 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.

See Also:
EXTRA_NEW_STATE, EXTRA_SUPPLICANT_ERROR, Constant Field Values

EXTRA_NEW_STATE

public static final String EXTRA_NEW_STATE
The lookup key for a SupplicantState describing the new state Retrieve with Intent.getParcelableExtra(String).

See Also:
Constant Field Values

EXTRA_SUPPLICANT_ERROR

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

See Also:
ERROR_AUTHENTICATING, Constant Field Values

SCAN_RESULTS_AVAILABLE_ACTION

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

See Also:
Constant Field Values

RSSI_CHANGED_ACTION

public static final String RSSI_CHANGED_ACTION
The RSSI (signal strength) has changed.

See Also:
EXTRA_NEW_RSSI, Constant Field Values

EXTRA_NEW_RSSI

public static final String EXTRA_NEW_RSSI
The lookup key for an int giving the new RSSI in dBm.

See Also:
Constant Field Values

NETWORK_IDS_CHANGED_ACTION

public static final String NETWORK_IDS_CHANGED_ACTION
The network IDs of the configured networks could have changed.

See Also:
Constant Field Values

ACTION_PICK_WIFI_NETWORK

public static final String ACTION_PICK_WIFI_NETWORK
Activity Action: Pick a Wi-Fi network to connect to.

Input: Nothing.

Output: Nothing.

See Also:
Constant Field Values

mService

IWifiManager mService

mHandler

Handler mHandler
Constructor Detail

WifiManager

public WifiManager(IWifiManager service,
                   Handler handler)
Create a new WifiManager instance. Applications will almost always want to use Context.getSystemService() to retrieve the standard Context.WIFI_SERVICE.

Parameters:
service - the Binder interface
handler - target for messages - hide this because it takes in a parameter of type IWifiManager, which is a system private class.
Method Detail

getConfiguredNetworks

public List<WifiConfiguration> getConfiguredNetworks()
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:

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

addNetwork

public 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 - 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.

updateNetwork

public int updateNetwork(WifiConfiguration config)
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.

removeNetwork

public 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.

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

enableNetwork

public boolean enableNetwork(int netId,
                             boolean disableOthers)
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

disableNetwork

public 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.

Parameters:
netId - the ID of the network as returned by addNetwork(android.net.wifi.WifiConfiguration).
Returns:
true if the operation succeeded

disconnect

public boolean disconnect()
Disassociate from the currently active access point. This may result in the asynchronous delivery of state change events.

Returns:
true if the operation succeeded

reconnect

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

reassociate

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

pingSupplicant

public boolean pingSupplicant()
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.

startScan

public boolean startScan()
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

getConnectionInfo

public WifiInfo getConnectionInfo()
Return dynamic information about the current Wi-Fi connection, if any is active.

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

getScanResults

public List<ScanResult> getScanResults()
Return the results of the latest access point scan.

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

saveConfiguration

public boolean saveConfiguration()
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

getDhcpInfo

public DhcpInfo getDhcpInfo()
Return the DHCP-assigned addresses from the last successful DHCP request, if any.

Returns:
the DHCP information

setWifiEnabled

public boolean setWifiEnabled(boolean enabled)
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).

getWifiState

public int getWifiState()
Gets the Wi-Fi enabled state.

Returns:
One of WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLED, WIFI_STATE_ENABLING, WIFI_STATE_UNKNOWN
See Also:
isWifiEnabled()

isWifiEnabled

public boolean isWifiEnabled()
Return whether Wi-Fi is enabled or disabled.

Returns:
true if Wi-Fi is enabled
See Also:
getWifiState()

calculateSignalLevel

public static 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 - 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).

compareSignalLevel

public static int compareSignalLevel(int rssiA,
                                     int rssiB)
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.

createWifiLock

public WifiManager.WifiLock createWifiLock(String tag)
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.
See Also:
WifiManager.WifiLock

Build 1.0_r1(from source)

Please submit a feedback, bug or feature