Android's Wifimanager

Source: Internet
Author: User
Tags get ip

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 *@paramv*/ Public voidNetscan (View v) {//Start ScanWifimanager.startscan (); //Get scan ResultsList<scanresult> Mdata=wifimanager.getscanresults (); Scanreslutadapter Adapter=NewScanreslutadapter (Mdata); Listview.setadapter (adapter);}/*** Open Network *@paramv*/ Public voidOpenNet (View v) {if(!wifimanager.iswifienabled ()) {wifimanager.setwifienabled (true); }}/*** Close the network *@paramv*/ Public voidclosenet (View v) {if(wifimanager.iswifienabled ()) {wifimanager.setwifienabled (false); }}/*** Get network status *@paramv*/ Public voidgetnetstate (View v) {intState=wifimanager.getwifistate (); String Statestr= ""; Switch(state) { CaseWifiManager.WIFI_STATE_DISABLED:stateStr= "DISABLED";  Break;  CaseWifiManager.WIFI_STATE_DISABLING:stateStr= "Disabling";  Break;  CaseWifiManager.WIFI_STATE_ENABLED:stateStr= "ENABLED";  Break;  CaseWifiManager.WIFI_STATE_ENABLING:stateStr= "Enabling";  Break; } netstate.settext ("Network Status:" +statestr);}/*** Monitor the WiFi signal strength *@paramv*/ Public voidmonitorsignal (View v) {//get link information for current WiFiWifiinfo wifiinfo=Wifimanager.getconnectioninfo (); //Get signal Strength    intLevel =Wifiinfo.getrssi (); //Get signal Strength valueLevel =Wifiinfo.getrssi (); //send information based on the signal strength obtainedString levelstr= "no Signal"; if(Level <= 0 && level >=-50) {Levelstr= "Best Signal"; } Else if(Level < -50 && level >=-70) {Levelstr= "Good signal"; } 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-permissionAndroid:name= "Android.permission.CHANGE_NETWORK_STATE"></uses-permission><uses-permissionAndroid:name= "Android.permission.CHANGE_WIFI_STATE"></uses-permission><uses-permissionAndroid:name= "Android.permission.ACCESS_NETWORK_STATE"></uses-permission><uses-permissionAndroid:name= "Android.permission.ACCESS_WIFI_STATE"></uses-permission>

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSection Technology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:

Android's Wifimanager

Related Keywords:

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.