Simple use of android.net. WiFi

Source: Internet
Author: User
Tags bssid

Obtain the Wi-Fi control wifimanager.
Wifimanager WM = (wifimanager) getsystemservice (context. wifi_service );
Next, you can operate on WiFi. For example, to enable or disable WiFi, you can use the setwifienabled () method of wifimanager to enable or disable wifi.
To view some information, you can use the getconnectioninfo () method to obtain the wifiinfo class object. The wifiinfo class mainly provides the connection information stored on the mobile phone wifi.
Wifiinfo class method:
1. getbssid ()
Obtain the MAC address (string) of the Access Point)
2. getipaddress ()
Obtain the local IP address (INT)
3. getlinkspeed ()
Get connection speed (not download speed), in Mbps (INT)
4. getmacaddress ()
Obtain the MAC address (string)
5. getnetworkid ()
Obtain the network ID number. Each set network has a unique integer ID, which is used to identify the network ). In short, this number represents a connection point. Mobile phones can connect to many wireless networks through WiFi.
6. getarg ()
Returns the signal strength received, which is a negative number. (It seems that-113 indicates that there is no signal at all, that is, the larger the number, the better the signal)
7. getssid ()
Obtain the name of the wireless signal provider (the name of the network to be connected) (string)
The above class can only find the MAC address and IP address of the Local Machine. To view only the detailed information of the local machine, you can use this class: dhcpinfo df = WM. getdhcpinfo ();
The dhcpinfo class has no methods and provides some fields:
1. dns1 first DNS
2. dns2 as above
3. Gateway
4. IPaddress IP Address
5. netmask Subnet Mask
6. serveraddress server address (in fact, it is the router IP address, which is the same as the gateway)
7. leaseduration and getlinkspeed () above ()
The value of the method is close, which may be the connection speed, but the unit is not Mbps. getlinkspeed ()
72. The value of this field is 7200.

The above IP, gateway, subnet mask, and so on are all int-type values. If you don't understand them directly, attach a method I wrote to convert int to string type.

    private String intToString(int a){    StringBuffer sb=new StringBuffer();    int b=(a>>0)&0xff;    sb.append(b+".");    b=(a>>8)&0xff;    sb.append(b+".");    b=(a>>16)&0xff;    sb.append(b+".");    b=(a>>24)&0xff;    sb.append(b);return sb.toString();    }

The wificonfiguration class provides all the information about a Wi-Fi configuration.
There are the following fields (which are not necessarily correct ):
The MAC address of the bssid access point.
The name of the SSID access point.
Networkid
Presharedkey WPA-PSK password, if there is a will get a * (this is to determine whether there is a password)
Priority priority

Status connection status, which is a field in wificonfiguration. Status.

Wificonfiguration. Status provides three fields to indicate the three statuses of Wi-Fi connections.
1. The current value is 0, indicating that the instance is in the connection status.
2. The value of disabled is 1, indicating that the network is unavailable.
3. The value of enabled is 2, indicating that the Network is available but not connected.

Information of the Access Point scanned by scanresult WiFi
1. bssid
2. SSID
3. Capabilities
4. Frequency
5. Level

Information about changing the status of a received Wi-Fi device to receive a broadcast:

Action: wifimanager. wifi_state_changed_action ("android.net. Wifi. wifi_state_changed") the Wi-Fi status change is triggered twice.

Two messages can be received: (key)

1. wifimanager. extra_previus_wifi_state ("previus_wifi_state") int-type value intent. getintextra ("key", 0); (the second is the default value when the result is not obtained)

2. wifimanager. extra_wifi_state ("wifi_state") int-type value intent. getintextra ("key", 0); (the second is the default value when the result is not obtained)

Action: wifimanager. network_state_changed_action)

Two messages can be received: (key)

1. wifimanager. extra_bssid ("bssid") string type value intent. getstringextra ("key ")

2. wifimanager. extra_network_info ("networkinfo") T getparcelableextra ("key ")

Action: wifimanager. supplicant_connection_change_action ("android.net. Wifi. supplicant. connection_change") determines whether WiFi is enabled. A change is triggered once.

One message can be received: (key)

1. wifimanager. extra_supplicant_connected ("connected") boolean type return value intent. getbooleanextra ("key", true) The second is the default value

Action: wifimanager. supplicant_state_changed_action ("android.net. Wifi. supplicant. state_change") sends information about the Wi-Fi connection process. If an error occurs, the system will receive the error message. The connection is triggered multiple times.

Two messages can be received: (key)

1. wifimanager. extra_new_state ("newstate") intent. getparcelableextra ("key ")

2. wifimanager. extra_supplicant_error ("supplicanterror") int value getintextra ("key", INT)

The status corresponding to the int value of the WiFi status:

Wifimanager. wifi_state_disabled = 1

Wifimanager. wifi_state_disabling = 0

Wifimanager. wifi_state_enabled = 3

Wifimanager. wifi_state_enabling = 2

Wifimanager. wifi_state_unknown = 4

The above is tested in a Wi-Fi environment, but not in a Wi-Fi environment. It is not necessarily accurate, but it is almost the same. The testing model HTC G17.

If you connect to a network address (such as a http://www.baidu.com) while connecting to wifi, you will get an exception where the address cannot be found, in addition, within 8 seconds (a custom machine was tested), the address cannot be connected; otherwise, an exception is reported.

This package does not determine the network connection status, but only determines the various statuses of wifi. If you need to determine the network status, it is in the android.net package. Connectivitymanager.



// Determine whether WiFi is connected
private boolean isConnected(Context context){ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);System.out.println(ni.getState());System.out.println(ni.getDetailedState()+"-----");if(ni.getState()==State.CONNECTED)return true;return false;}

Private Boolean iswificonected (context) {wifimanager WM = (wifimanager) context. getsystemservice (context. wifi_service); List <wificonfiguration> List = WM. getconfigurednetworks (); iterator <wificonfiguration> it = List. iterator (); While (it. hasnext () {wificonfiguration WC = it. next (); If (WC. status = wificonfiguration. status. current) return true;} return false;} // 2012.11.12 this method has always returned false on machine 4.0, I guess I got it wrong. Don't use it.

If wifiinfo is not empty, it does not mean that information is stored in this class. getmacadress may be empty.

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.