Wi-Fi network access (1), Wi-Fi network access

Source: Internet
Author: User

Wi-Fi network access (1), Wi-Fi network access

I. What are the statuses of the Wi-Fi network card?

WIFI has the following five statuses, which are actually plastic constants:

1. WIFI_STATE_DISABLED: WIFI is unavailable,The value is: 1 .

2. WIFI_STATE_DISABLING: the WIFI is being disabled. Because the WIFI is being disabled, this status indicates that the WIFI is being disabled,The value is: 0 .

3. WIFI_STATE_ENABLED: WIFI is available,The value is: 3 .

4. WIFI_STATE_ENABLING: WIFI is being enabled. The same principle is true for WIFI_STATE_DISABLING,The value is: 2 .

5. WIFI_STATE_UNKNOWN: Unknown Nic status. This status occurs when the Wi-Fi becomes unavailable due to a mobile phone or program error,The value is: 4 .

 

2. permissions required to operate on WIFI

To obtain the permission to operate on WIFI, you must know what permissions are available for WIFI. The main operation permissions of WIFI are as follows:

CHANGE_NETWORK_STATE: Permission for modifying the network status.

CHANGE_WIFI_STATE: allows you to modify the Wi-Fi status.

ACCESS_NETWORK_STATE: permission to access the network status.

ACCESS_WIFI_STATE: permission to access the WIFI status.

 

The permission to declare the WIFI operation is declared in the AndroidManifest. xml file! Join:

  • <! -- Get the permissions required for WIFI access to the network -->
  • <Uses-permission android: name = "android. permission. CHANGE_NETWORK_STATE"> </uses-permission>
  • <Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"> </uses-permission>
  • <Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission>
  • <Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"> </uses-permission>

3. Change the WIFI status

Operations on the WIFI status mainly involve three aspects:

1. Run m_wifiManager = (WifiManager) this. getSystemService (Context. WIFI_SERVICE );WifiManagerThis object is the basis for us to operate the Wi-Fi network card, whether it is to change the WIFI status, or get the WIFI status, all through this object.

2.Changes the status of a Wi-Fi network adapter.WifiManagerMethod SEtWifiEnabledYou can enable or disable WIFI by setting parameters in this method:True Indicates opening, False Disabled.

3. obtain the status of the Wi-Fi network card, involvingWifiManagerMethodGetWifiState,In this way, you can get the current status of WIFI, that is, one of the five statuses of WIFI in the first point.

 

Note:

1. m_wifiManager = (WifiManager) this. getSystemService (Service.WIFI_SERVICE); Or (WifiManager) this. getSystemService (this.WIFI_SERVICE) To obtainWifiManagerObject. Because both Service and Activity are child classes of Context, the WIFI_SERVICE of Service and Activity actually inherits from Context, so it is no problem to write Context. WIFI_SERVICE.

Note: You can also use the following method to obtain the wifi status.

// Obtain whether wifi is enabled
Public boolean isWifiActive ()
{
// Obtain the Connection Manager
ConnectivityManager connMng = (ConnectivityManager) this
. GetSystemService (Context. CONNECTIVITY_SERVICE );
If (connMng! = Null)
{
NetworkInfo [] netInfor = connMng. getAllNetworkInfo ();
For (NetworkInfo I: netInfor)
{
If (ConnectivityManager. TYPE_WIFI = I. getType ()
& I. isConnected ())
{
Return true; // wifi enabled
}
}
}
Return false;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.