java.lang.Object | |
↳ | android.net.wifi.WifiManager |
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:
ConnectivityManager
.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
WifiManager.MulticastLock | Allows an application to receive Wifi Multicast packets. | |||||||||
|
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. | |||||||||
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 | 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_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 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_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. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Add a new network description to the set of configured networks.
|
||||||||||
|
Calculates the level of the signal.
|
||||||||||
|
Compares two signal strengths.
|
||||||||||
|
Create a new MulticastLock
|
||||||||||
|
Creates a new WifiLock.
|
||||||||||
|
Creates a new WifiLock.
|
||||||||||
|
Disable a configured network.
|
||||||||||
|
Disassociate from the currently active access point.
|
||||||||||
|
Allow a previously configured network to be associated with.
|
||||||||||
|
Return a list of all the networks configured in the supplicant.
|
||||||||||
|
Return dynamic information about the current Wi-Fi connection, if any is active.
|
||||||||||
|
Return the DHCP-assigned addresses from the last successful DHCP request,
if any.
|
||||||||||
|
Return the results of the latest access point scan.
|
||||||||||
|
Gets the Wi-Fi enabled state.
|
||||||||||
|
Check if scanning is always available.
|
||||||||||
|
Return whether Wi-Fi is enabled or disabled.
|
||||||||||
|
Check that the supplicant daemon is responding to requests.
|
||||||||||
|
Reconnect to the currently active access point, even if we are already
connected.
|
||||||||||
|
Reconnect to the currently active access point, if we are currently
disconnected.
|
||||||||||
|
Remove the specified network from the list of configured networks.
|
||||||||||
|
Tell the supplicant to persist the current list of configured networks.
|
||||||||||
|
Enable/Disable TDLS on a specific local route.
|
||||||||||
|
Similar to
setTdlsEnabled(InetAddress, boolean)
, except
this version allows you to specify remote endpoint with a MAC address.
|
||||||||||
|
Enable or disable Wi-Fi.
|
||||||||||
|
Request a scan for access points.
|
||||||||||
|
Update the network description of an existing configured network.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
[Expand]
Inherited Methods
|
|||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Activity Action: Pick a Wi-Fi network to connect to.
Input: Nothing.
Output: Nothing.
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.
The error code if there was a problem authenticating.
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)
.
The lookup key for a
NetworkInfo
object associated with the
Wi-Fi network. Retrieve with
getParcelableExtra(String)
.
The lookup key for an
int
giving the new RSSI in dBm.
The lookup key for a
SupplicantState
describing the new state
Retrieve with
getParcelableExtra(String)
.
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)
.
The lookup key for a
SupplicantState
describing the supplicant
error code if any
Retrieve with
getIntExtra(String, int)
.
The lookup key for a
WifiInfo
object giving the
information about the access point to which we are connected. Only present
when the new state is CONNECTED. Retrieve with
getParcelableExtra(String)
.
The lookup key for an int that indicates whether Wi-Fi is enabled,
disabled, enabling, disabling, or unknown. Retrieve it with
getIntExtra(String, int)
.
The network IDs of the configured networks could have changed.
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, additional extras may provide the BSSID and WifiInfo of
the access point.
as a
String
.
The RSSI (signal strength) has changed.
An access point scan has completed, and results are available from the supplicant.
Call
getScanResults()
to obtain the results.
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.
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.
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.
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
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.
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.
Wi-Fi is currently being disabled. The state will change to
WIFI_STATE_DISABLED
if
it finishes successfully.
Wi-Fi is currently being enabled. The state will change to
WIFI_STATE_ENABLED
if
it finishes successfully.
Wi-Fi is in an unknown state. This state will occur when an error happens while enabling or disabling.
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)
.
config |
the set of variables that describe the configuration,
contained in a
WifiConfiguration
object.
|
---|
-1
on failure.
Calculates the level of the signal. This should be used any time a signal is being shown.
rssi | The power of the signal measured in RSSI. |
---|---|
numLevels | The number of levels to consider in the calculated level. |
Compares two signal strengths.
rssiA | The power of the first signal measured in RSSI. |
---|---|
rssiB | The power of the second signal measured in RSSI. |
Create a new MulticastLock
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. |
---|
Creates a new WifiLock.
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. |
---|
Creates a new WifiLock.
lockType |
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 | 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. |
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.
netId |
the ID of the network as returned by
addNetwork(WifiConfiguration)
.
|
---|
true
if the operation succeeded
Disassociate from the currently active access point. This may result in the asynchronous delivery of state change events.
true
if the operation succeeded
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.
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.
|
true
if the operation succeeded
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:
WifiConfiguration
objects. Upon failure to fetch or
when when Wi-Fi is turned off, it can be null.
Return dynamic information about the current Wi-Fi connection, if any is active.
WifiInfo
.
Return the DHCP-assigned addresses from the last successful DHCP request, if any.
Return the results of the latest access point scan.
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
.
Return whether Wi-Fi is enabled or disabled.
true
if Wi-Fi is enabled
Check that the supplicant daemon is responding to requests.
true
if we were able to communicate with the supplicant and
it returned the expected response to the PING message.
Reconnect to the currently active access point, even if we are already connected. This may result in the asynchronous delivery of state change events.
true
if the operation succeeded
Reconnect to the currently active access point, if we are currently disconnected. This may result in the asynchronous delivery of state change events.
true
if the operation succeeded
Remove the specified network from the list of configured networks. This may result in the asynchronous delivery of state change events.
netId | the integer that identifies the network configuration to the supplicant |
---|
true
if the operation succeeded
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.
true
if the operation succeeded
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.
remoteIPAddress | IP address of the endpoint to setup TDLS with |
---|---|
enable | true = setup and false = tear down TDLS |
Similar to
setTdlsEnabled(InetAddress, boolean)
, except
this version allows you to specify remote endpoint with a MAC address.
remoteMacAddress | MAC address of the remote endpoint such as 00:00:0c:9f:f2:ab |
---|---|
enable | true = setup and false = tear down TDLS |
Enable or disable Wi-Fi.
enabled |
true
to enable,
false
to disable.
|
---|
true
if the operation succeeds (or if the existing state
is the same as the requested state).
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.
true
if the operation succeeded, i.e., the scan was initiated
Update the network description of an existing configured network.
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.
|
---|
networkId
of the supplied
WifiConfiguration
on success.
-1
on failure, including when the
networkId
field of the
WifiConfiguration
does not refer to an
existing network.
Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.
Note that objects that override
finalize
are significantly more expensive than
objects that don't. Finalizers may be run a long time after the object is no longer
reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup.
Note also that finalizers are run on a single VM-wide finalizer thread,
so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary
for a class that has a native peer and needs to call a native method to destroy that peer.
Even then, it's better to provide an explicit
close
method (and implement
Closeable
), and insist that callers manually dispose of instances. This
works well for something like files, but less well for something like a
BigInteger
where typical calling code would have to deal with lots of temporaries. Unfortunately,
code that creates lots of temporaries is the worst kind of code from the point of view of
the single finalizer thread.
If you
must
use finalizers, consider at least providing your own
ReferenceQueue
and having your own thread process that queue.
Unlike constructors, finalizers are not automatically chained. You are responsible for
calling
super.finalize()
yourself.
Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.
Throwable |
---|