Android--wifi

Source: Internet
Author: User
Tags get ip bssid

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
  1. <uses-permission android:name="Android.permission.CHANGE_NETWORK_STATE"></ Uses-permission><!--allow programs to change network link status --
  2. <uses-permission android:name="Android.permission.ACCESS_WIFI_STATE"></ Uses-permission><!--allow program access to WiFi network status information -
  3. <uses-permission android:name="Android.permission.CHANGE_WIFI_STATE"></ Uses-permission><!--allow program to change WiFi link status --
  4. <uses-permission android:name="Android.permission.INTERNET"></ Uses-permission>



1. Turn on WiFi

[Java]View Plaincopy
    1. Turn on WiFi
    2. Public void Wifiopen () {
    3. if (!localwifimanager.iswifienabled ()) {
    4. Localwifimanager.setwifienabled (true);
    5. }
    6. }



2. Scan WiFi signal

[Java]View Plaincopy
    1. Scan WiFi
    2. Public void Wifistartscan () {
    3. Localwifimanager.startscan ();
    4. }


3. Get scanned WiFi results

[Java]View Plaincopy
    1. Get scan Results
    2. Public list<scanresult> Getscanresults () {
    3. return Localwifimanager.getscanresults (); //Get scan results
    4. }



4, get WiFi configuration good information, including the configured password

[Java]View Plaincopy
  1. Get WiFi configuration good information
  2. Public void GetConfiguration () {
  3. Wificonfiglist = Localwifimanager.getconfigurednetworks (); //Get configured network information
  4. For (int i =0;i<wificonfiglist.size (); i++) {
  5. LOG.I ("GetConfiguration", Wificonfiglist.get (i).  SSID);
  6. LOG.I ("GetConfiguration", string.valueof (Wificonfiglist.get (i). Networkid));
  7. }
  8. }



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
  1. Determine if the specified WiFi has been configured, based on the address of the WiFi Bssid, return NetID
  2. public int isconfiguration (String SSID) {
  3. LOG.I ("Isconfiguration", String.valueof (Wificonfiglist.size ()));
  4. For (int i = 0; i < wificonfiglist.size (); i++) {
  5. LOG.I (Wificonfiglist.get (i). Ssid,string.valueof (Wificonfiglist.get (i). Networkid));
  6. if (Wificonfiglist.get (i). Ssid.equals (SSID)) {//address same
  7. return Wificonfiglist.get (i). Networkid;
  8. }
  9. }
  10. return-1;
  11. }



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
  1. Add configuration information for the specified WiFi, this SSID does not exist in the original list
  2. public int Addwificonfig (list<scanresult> wifilist,string ssid,string pwd) {
  3. int wifiid =-1;
  4. For (int i = 0;i < wifilist.size (); i++) {
  5. Scanresult WiFi = wifilist.get (i);
  6. if (WiFi. Ssid.equals (SSID)) {
  7. LOG.I ("Addwificonfig","equals");
  8. Wificonfiguration Wificong = new Wificonfiguration ();
  9. Wificong.ssid = "\" "+wifi. ssid+"\" "; \ "Escape character, representing"
  10. Wificong.presharedkey = "\" "+pwd+" \ ""; WPA-PSK Password
  11. Wificong.hiddenssid = false;
  12. Wificong.status = WifiConfiguration.Status.ENABLED;
  13. 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
  14. if (wifiid! =-1) {
  15. return wifiid;
  16. }
  17. }
  18. }
  19. return wifiid;
  20. }

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
  1. Connect WiFi with the specified ID
  2. Public boolean Connectwifi (int wifiid) {
  3. For (int i = 0; i < wificonfiglist.size (); i++) {
  4. Wificonfiguration WiFi = wificonfiglist.get (i);
  5. if (Wifi.networkid = = wifiid) {
  6. While (!) ( Localwifimanager.enablenetwork (Wifiid, True))) {//Activates the ID, establishes the connection
  7. //status:0--is connected, the 2--can be connected
  8. LOG.I ("Connectwifi", String.valueof (Wificonfiglist.get (wifiid). status));
  9. }
  10. return true;
  11. }
  12. }
  13. return false;
  14. }



This is just a simple application.

Project Source:http://download.csdn.net/detail/liuhui_8989/7154671

Android--wifi

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.