Android WiFi Module Learning

Source: Internet
Author: User
Tags get ip

Transferred from: http://www.th7.cn/Program/Android/201303/130087.shtml

About the WiFi module

Android itself has provided a package to handle the operation of WiFi in the Android environment, the location is Android.net.wifi below. Calling the WiFi operation requires that the corresponding permissions be added to the mainfest.

Main function class

The official offer can be broadly divided into four main classes: Wifimanager,scanresult,wificonfiguration,wifiinfo, Wifilock,multicastlock, etc.

Wifimanager

Provide WiFi management of a variety of major APIs, mainly including WiFi scanning, establish connection, configuration, etc.

Http://developer.android.com/reference/android/net/wifi/WifiManager.html

Scanresult

Describes the information that opens a scanned WiFi hotspot. Contains SSID, capabilities, frequency, level (signal strength), and so on.

Http://developer.android.com/reference/android/net/wifi/ScanResult.html

Wificonfiguration

Describes the connection information for WiFi, including settings for SSID, SSID hide, password, and so on.

Http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html

Wifiinfo

Describes the WiFi information after a connection has been established. Containsinformation such as IP, MAC address, connection speed, and so on. The concept of difference and scanresult.

Http://developer. android.com/reference/android/net/wifi/wifiinfo.html

Common function code get Wifimanager
Wifimanager wm = (Wifimanager) this.getsystemservice (Context.wifi_service);

Set WiFi function to open

BRet = wifimanager.iswifienabled ();//Determine if Open

BRet = Wifimanager.setwifienabled (TRUE);//set Turn on off

Note that after calling setwifienabled, it takes a certain amount of time for the system to open the WiFi module, at which point the state obtained by Wifimanager.getwifistate () determines whether it is complete.

WifiManager.WIFI_STATE_DISABLED:WIFI NIC not available (1)

WifiManager.WIFI_STATE_DISABLING:WIFI NIC is shutting down (0)

WifiManager.WIFI_STATE_ENABLED:WIFI Nic available (3)

WifiManager.WIFI_STATE_ENABLING:WIFI Network is opening (2) (WIFI boot takes some time)

Wifimanager.wifi_state_unknown: Unknown nic status

while (wifimanager.getwifistate () = = wifimanager.wifi_state_enabling) {                            
try {
Thread.CurrentThread ();
Thread.Sleep (100);
} catch (Interruptedexception IE) {
}
}

Scan near Access point AP
Wifimanager.startscan ()) List = Wifimanager.getscanresults ();

Get the AP's signal strength, frequency, etc.
Scanresult.level  //Signal strength scanresult.frequency  //frequency

Get connection information for a saved configuration in your phone
Wifimanager.getconfigurednetworks ();//Get Saved configuration information

Determine if the AP for an SSID has saved configuration information
for (Wificonfiguration existingconfig:existingconfigs) {         
if (ExistingConfig.SSID.equals ("/" "+ SSID +"/"")) {
return existingconfig;
}
}

Turn off WiFi connection
Wifimanager.disablenetwork (Existingconfig.networkid);

Removing the WiFi connection configuration
Wifimanager.removenetwork (Networkid);

Note the difference from the above disablenetwork. The disable is simply disconnected, and the saved SSID and password are not cleared.

Remove removes (forgets) the connection configuration from the phone to erase the saved password

In the normal connection state of the network, you can also call remove directly in place of disable to interrupt the network, but it clears the password information.

Create a connection Configuration
Public wificonfiguration Createwificonfig (string SSID, String Password, Wificiphertype Type) {
wificonfiguration config = new wificonfiguration ();
Config.allowedAuthAlgorithms.clear ();
Config.allowedGroupCiphers.clear ();
Config.allowedKeyManagement.clear ();
Config.allowedPairwiseCiphers.clear ();
Config.allowedProtocols.clear ();
Config. SSID = "/" "+ SSID +"/"";

No password
if (Type = = Wificiphertype.wificipher_nopass) {
Config.wepkeys[0] = "";
Config.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.NONE);
Config.weptxkeyindex = 0;
}

WEP encryption
if (Type = = Wificiphertype.wificipher_wep) {
Config.presharedkey = "/" "+ Password +"/"";
Config.hiddenssid = true;
Config.allowedAuthAlgorithms.set (WifiConfiguration.AuthAlgorithm.SHARED);
Config.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.CCMP);
Config.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.TKIP);
Config.allowedGroupCiphers.set (WIFICONFIGURATION.GROUPCIPHER.WEP40);
Config.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.WEP104);
Config.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.NONE);
Config.weptxkeyindex = 0;
}//WPA Encryption
if (Type = = WIFICIPHERTYPE.WIFICIPHER_WPA) {
Config.presharedkey = "/" "+ Password +"/"";
Config.hiddenssid = true;
Config.allowedAuthAlgorithms.set (WifiConfiguration.AuthAlgorithm.OPEN);
Config.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.TKIP);
Config.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.WPA_PSK);
Config.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.TKIP);
Config.allowedProtocols.set (WifiConfiguration.Protocol.WPA);
Config.status = WifiConfiguration.Status.ENABLED;
} else {
return null;
}
return config;
}

Add, turn on a WiFi
public boolean Addnewwifi (Wificonfiguration newconfig) {                   
int NetID = Wifimanager.addnetwork (newconfig);//Add
Boolean bRet = Wifimanager.enablenetwork (NetID, false);//Start
return bRet;
}
Get the current wifiinfo (IP, Mac, etc.)

Winfo.getbssid ();//Get Bssid
Winfo.gethiddenssid ();//Get SSID hidden
Winfo.getipaddress ();//Get IP Address
Winfo.getlinkspeed ();//Get the speed of the connection
Winfo.getmacaddress ();//Get MAC Address
Winfo.getrssi ();//Get signals from 802.11n networks
Winfo.getssid ();//Get SSID
Supplicantstate suppstate = Winfo.getsupplicantstate ();//Returns information about the specific client state
Winfo.getdetailedstateof (suppstate);//Get

Related permissions
Permissions to modify network status



Android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>

Android WiFi Module Learning

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.