Basic structure of WiFi
The following five statuses are defined in the official Android documentation:
Wifi_state_disabling WiFi NIC is shutting down 0
Wifi_state_disabled WiFi NIC is not available 1
Wifi_state_enabling WiFi card is opening 2
Wifi_state_enabled WiFi Card Available 3
Wifi_state_unknown WiFi card Status unknown 4 to operate a WiFi device, you need to use Context.getsystemservice (Context.wifi_service) to get the Wifimanager object, and use this object to manage our WiFi devices. If you want to get a list of WiFi hotspots around you can use Wifimanager.getscanresults () to return a list of Scanresult
A lot is listed here, but it can be broadly divided into four main classes: Scanresult, wificonfiguration, Wifiinfo Wifimanager
(1) Scanresult
Mainly through the WiFi hardware scan to get some of the surrounding WiFi hotspot information. In our WiFi search, the general will search this information, the first is the access point name, the strength of the access point information, as well as the security mode used by the access point, is WPA, WPE; Open this class, we can see the following information BSSID the address of the access point, Here mainly refers to a small range of wireless devices connected to obtain the address, for example, two laptops through the wireless network connection, both sides of the wireless network card assigned address.
The name of the SSID network, when we search for a network, it is this to distinguish between each of the different network access points.
Capabilities Network access performance, here is mainly to determine the network encryption method and so on.
Frequency frequency, the number of MHz each channel interacts with.
level, primarily to determine the priority of the network connection.
Only one method is provided here, which is to get the information programming string ToString ().
(2) Wificonfiguration
When we connect to a WiFi access point, we need to get some information. You can compare it with our wired device.
The data here is relatively more complex.
Six sub-classes
Wificonfiguration.authalgorthm is used to determine the encryption method.
Wificonfiguration.groupcipher Gets the method that uses Groupcipher to encrypt.
Wificonfiguration.keymgmt get used with KEYMGMT.
Wificonfiguration.pairwisecipher gets the encryption using WPA mode.
Wificonfiguration.protocol gets which protocol is used for encryption.
Wificonfiguration.status gets the status of the current network.
For those who are interested in encryption, you can search for relevant content on the Internet.
To set the properties of a wificonfiguration:
Wificonfiguration.authalgorthm set the encryption method.
Optional parameters: Leap only for Leap,
OPEN is WPA/WPA2 need,
GKFX requires a static WEP key
Wificonfiguration.groupcipher uses the Groupcipher method to encrypt.
Optional Parameters: Ccmp,tkip,wep104,wep40
WIFICONFIGURATION.KEYMGMT Key Management mechanism (keymanagerment), using KEYMGMT.
Optional parameter Ieee8021x,none,wpa_eap,wpa_psk
Wificonfiguration.pairwisecipher set the encryption method.
Optional parameter Ccmp,none,tkip
Wificonfiguration.protocol sets a protocol for encryption.
Optional parameter RSN,WPA,
Wificonfiguration.status sets the status of the current network.
Optional parameter current,disabled,enabled
(3) Wifiinfo
After our WiFi has been connected, you can obtain information about the current link through this class for some connected WiFi connections.
Here the information is relatively simple, here is a brief introduction to the method here:
Getbssid () Get Bssid
Getdetailedstateof () Get the connectivity of the client
Gethiddenssid () gets whether the SSID is hidden
Getipaddress () Get IP address
Getlinkspeed () Get the speed of the connection
Getmacaddress () Get MAC Address
Getrssi () Get signal from 802.11n network
Getssid () Get SSID
Getsupplicanstate () returns information about the specific client state
(4) Wifimanager
This needless to say, is used to manage our WiFi connection, here has already defined some classes, can let us use.
It's relatively complex here, with more content, but we can get a lot of relevant information by literal means. Many constants are predefined in this class, and we can use them directly without having to create them again.
Here is a brief introduction to the method here:
Addnetwork (wificonfiguration config) to add a network by acquiring the link state information to the network
Calculatesignallevel (int rssi, int numlevels) calculates the rank of the signal
Comparesignallevel (int rssia, int rssib) vs. connection A and connection B
Createwifilock (int lockType, String tag) creates a WiFi lock that locks the current WiFi connection
disablenetwork (int netId) to invalidate a network connection
Disconnect () disconnect
Enablenetwork (int netId, Boolean disableothers) connects a connection
Getconfigurednetworks () Get the status of a network connection
Getconnectioninfo () Get information about the current connection
Getdhcpinfo () Getting DHCP information
Getscanresulats () Gets the results of the scan test
Getwifistate () Get a WiFi access point is valid
Iswifienabled () determine if a WiFi connection is valid
Pingsupplicant () Ping a connection to determine if it can connect
Ressociate () Even if the connection is not ready, it must be connected
Reconnect () If the connection is ready, connect
Removenetwork () Removing a network
Saveconfiguration () Keep a configuration message
Setwifienabled () make a connection valid
Startscan () Start scanning
Updatenetwork (wificonfiguration config) update information for a network connection
In addition, Wifimanaer also provides an internal subclass, that is, the role of Wifimanagerlock,wifimanagerlock,
In the normal state, if our WiFi status is idle, then the network connectivity, will be temporarily interrupted.
However, if we lock the current network status, then WiFi connectivity will remain in a certain state, of course, after the contact lock, will return to normal
achieve your goal: search for WiFi, manually enter your password and save it, connect to WiFi. The second time to connect the WiFi signal does not need to enter a password
Start the response permission in Androidmanifest.xml
[HTML]View Plaincopy
- <uses-permission android:name="Android.permission.CHANGE_NETWORK_STATE"></ Uses-permission><!--allow programs to change network link status --
- <uses-permission android:name="Android.permission.ACCESS_WIFI_STATE"></ Uses-permission><!--allow program access to WiFi network status information -
- <uses-permission android:name="Android.permission.CHANGE_WIFI_STATE"></ Uses-permission><!--allow program to change WiFi link status --
- <uses-permission android:name="Android.permission.INTERNET"></ Uses-permission>
1. Turn on WiFi
[Java]View Plaincopy
- Turn on WiFi
- Public void Wifiopen () {
- if (!localwifimanager.iswifienabled ()) {
- Localwifimanager.setwifienabled (true);
- }
- }
2. Scan WiFi signal
[Java]View Plaincopy
- Scan WiFi
- Public void Wifistartscan () {
- Localwifimanager.startscan ();
- }
3. Get scanned WiFi results
[Java]View Plaincopy
- Get scan Results
- Public list<scanresult> Getscanresults () {
- return Localwifimanager.getscanresults (); //Get scan results
- }
4, get WiFi configuration good information, including the configured password
[Java]View Plaincopy
- Get WiFi configuration good information
- Public void GetConfiguration () {
- Wificonfiglist = Localwifimanager.getconfigurednetworks (); //Get configured network information
- For (int i =0;i<wificonfiglist.size (); i++) {
- LOG.I ("GetConfiguration", Wificonfiglist.get (i). SSID);
- LOG.I ("GetConfiguration", string.valueof (Wificonfiglist.get (i). Networkid));
- }
- }
5, according to the name of the SSID of the WiFi to determine whether the WiFi has been configured, configured to return its networkid, for the connection. The BSSID address was not successful before, so you can only use the SSID
[Java]View Plaincopy
- Determine if the specified WiFi has been configured, based on the address of the WiFi Bssid, return NetID
- public int isconfiguration (String SSID) {
- LOG.I ("Isconfiguration", String.valueof (Wificonfiglist.size ()));
- For (int i = 0; i < wificonfiglist.size (); i++) {
- LOG.I (Wificonfiglist.get (i). Ssid,string.valueof (Wificonfiglist.get (i). Networkid));
- if (Wificonfiglist.get (i). Ssid.equals (SSID)) {//address same
- return Wificonfiglist.get (i). Networkid;
- }
- }
- return-1;
- }
6, if the need to connect WiFi is not configured, that is, the password is not saved. Add password information to the WiFi for the specified name SSID, and add the Networid that is returned to its assigned
[Java]View Plaincopy
- Add configuration information for the specified WiFi, this SSID does not exist in the original list
- public int Addwificonfig (list<scanresult> wifilist,string ssid,string pwd) {
- int wifiid =-1;
- For (int i = 0;i < wifilist.size (); i++) {
- Scanresult WiFi = wifilist.get (i);
- if (WiFi. Ssid.equals (SSID)) {
- LOG.I ("Addwificonfig","equals");
- Wificonfiguration Wificong = new Wificonfiguration ();
- Wificong.ssid = "\" "+wifi. ssid+"\" "; \ "Escape character, representing"
- Wificong.presharedkey = "\" "+pwd+" \ ""; WPA-PSK Password
- Wificong.hiddenssid = false;
- Wificong.status = WifiConfiguration.Status.ENABLED;
- Wifiid = Localwifimanager.addnetwork (Wificong); //will be configured to add specific WiFi password information, add the default is not activated after completion, the successful return ID, otherwise-1
- if (wifiid! =-1) {
- return wifiid;
- }
- }
- }
- return wifiid;
- }
7, according to step 6 configured to connect the WiFi password information, the following through the Networkid connection to specify WiFi. Before you connect a WiFi signal that has just been added to the configuration information in step 6, you need to re-execute the next step 4 to get a list of the new configuration good information .
[Java]View Plaincopy
- Connect WiFi with the specified ID
- Public boolean Connectwifi (int wifiid) {
- For (int i = 0; i < wificonfiglist.size (); i++) {
- Wificonfiguration WiFi = wificonfiglist.get (i);
- if (Wifi.networkid = = wifiid) {
- While (!) ( Localwifimanager.enablenetwork (Wifiid, True))) {//Activates the ID, establishes the connection
- //status:0--is connected, the 2--can be connected
- LOG.I ("Connectwifi", String.valueof (Wificonfiglist.get (wifiid). status));
- }
- return true;
- }
- }
- return false;
- }
This is just a simple application.
Project Source:http://download.csdn.net/detail/liuhui_8989/7154671
Android--wifi