Android--wifimanager

Source: Internet
Author: User
Tags get ip netscan

Mobile devices can not be separated from the network, the Android platform under package Android.net.wifi provides some classes dedicated to manage the WiFi function of the device. There are several main categories under the package:

1, Scanresult: Mainly used to describe the WiFi hardware scan to get the surrounding WiFi hotspot information. Contains the field information and methods such as:

2. Wificonfiguration: This class is used to describe the configuration information of a WiFi network, including security configuration information. The following are provided in this class, mainly for the description of how WiFi is encrypted.

3, Wifiinfo: Used to describe the linked WiFi information, by the following methods, you can obtain relevant information

Getbssid (): Get Bssid, in the form of XX:XX:XX:XX:XX:XX

Getdetailedstateof (supplicantstate suppstate): Gets the status of the network link.

Gethiddenssid (): Whether the SSID is hidden.

Getipaddress (): Get IP Address

Getlinkspeed (): Gets the speed of the link

Getnetworkid (): Get the network number

Getrssi (): Gets the strength.

Getssid (): Get SSID Information

Getsupplicantstate (): Get Client status information

4, Wifimanager: This class is mainly used to manage WiFi, which contains many methods, such as, specific use can see the following cases.

Here we demonstrate the specific use of wifimanager using a case-by-case interface effect such as:

The activity code is as follows

Get Wifimanagerwifimanager = (Wifimanager) getsystemservice (context.wifi_service);/** * Scan network * @param v */public void    Netscan (View v) {//Start scan Wifimanager.startscan ();    Get scan results list<scanresult> mdata= wifimanager.getscanresults ();    Scanreslutadapter adapter = new Scanreslutadapter (mdata); Listview.setadapter (adapter);} /** * Open Network * @param v */public void OpenNet (View v) {if (!wifimanager.iswifienabled ()) {wifimanager.setwifienable    D (TRUE); }}/** * Off network * @param v */public void closenet (View v) {if (wifimanager.iswifienabled ()) {Wifimanager.setwifienab    LED (false);    }}/** * Get network status * @param v */public void getnetstate (View v) {int state= wifimanager.getwifistate ();    String statestr = "";            Switch (state) {case wifimanager.wifi_state_disabled:statestr= "DISABLED";        Break            Case wifimanager.wifi_state_disabling:statestr= "disabling";        Break      Case wifimanager.wifi_state_enabled:      Statestr= "ENABLED";        Break            Case wifimanager.wifi_state_enabling:statestr= "enabling";    Break } netstate.settext ("Network status:" +statestr);} /** * Monitor WiFi signal strength * @param v */public void monitorsignal (View v) {//Get link information for current WiFi wifiinfo wifiinfo= wifimanager.get    ConnectionInfo ();    Get signal strength int level = Wifiinfo.getrssi ();    Get signal strength Value level = Wifiinfo.getrssi ();    Sends a message based on the obtained signal strength String levelstr= "no signal";    If (level <= 0 && level >= -50) {levelstr= "signal best";    } else if (Level < -50 && level >= -70) {levelstr= "signal is good";    } else if (Level < -70 && level >= -80) {levelstr= "Signal general";    } else if (Level < -80 && level >= -100) {levelstr= "signal difference"; } netinfo.settext ("Signal strength:" +levelstr);}

Note that access to WiFi requires the following permissions:

<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>

Android--wifimanager

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.