Android Management and Operation WiFi Simple instance source code _android

Source: Internet
Author: User
Tags get ip

Because of the need to always get the network problem, today looked at WiFi operation, after finishing, made a class, may be incomplete, but the personal feeling has been fully able to meet the needs, of course, the method inside may be wrong or incomplete, this class I did not carry out a complete test, Only some of these methods have been tested.

In fact, the operation of WiFi is also very simple, mainly using the following several objects or variables:

Private Wifimanager wifimanager;//Declaration Admin Object Openwifi
private wifiinfo wifiinfo;//wifi information
private list< Scanresult> scanresultlist; Scanned list of network Connections
private list<wificonfiguration> wificonfiglist;//network configuration list
private Wifilock wifilock;// WiFi lock

In fact, these can not be written as a separate class is also operable, in order to facilitate the use of this, write a unified class for WiFi operation, through these methods, basically can get all the operation of the data

The following code for everyone, if necessary can be used as a reference:

Package Com.wifitest;import java.util.List;
Import Android.content.Context;
Import Android.net.wifi.ScanResult;
Import android.net.wifi.WifiConfiguration;
Import Android.net.wifi.WifiInfo;
Import Android.net.wifi.WifiManager; Import Android.net.wifi.wifimanager.wifilock;public class Wifimanageclass {private Wifimanager wifimanager;//Declaration Admin Object P Rivate wifiinfo wifiinfo;//wifi information private list<scanresult> scanresultlist;  Scanned list of network connections private list<wificonfiguration> wificonfiglist;//network configuration list private Wifilock wifilock;//WiFi lock public Wifimanageclass {This.wifimanager = (Wifimanager) context. Getsystemservice (Context.wifi_service); 
/get WiFi Information this.wifiinfo = Wifimanager.getconnectioninfo ()//Get Connection Information} public boolean getwifistatus () { 
return wifimanager.iswifienabled (); //Turn on/off WiFi public boolean Openwifi () {if (!wifimanager.iswifienabled ()) {return wifimanager.setwifienabled (true 
); 
else {return false; } public boolEan Closewifi () {if (!wifimanager.iswifienabled ()) {return true; 
else {return wifimanager.setwifienabled (false); 
/lock/unlock WiFi/In fact, locked WiFi is to determine the success of WiFi, where the use of held, the meaning of shaking hands acquire get! 
public void Lockwifi () {wifilock.acquire (); public void Unlockwifi () {if (!wifilock.isheld ()) {wifilock.release ();//Freeing of resources}//I was written in the constructor, but given that it's not always used WiFi lock, so simply build a method yourself! Call when needed, establish on OK public void Createwifilock () {wifilock = Wifimanager.createwifilock ("FlyFly");//Create a lock flag//Scan Network Publ 
IC void Startscan () {Wifimanager.startscan (); Scanresultlist = Wifimanager.getscanresults (); Scan return results list wificonfiglist = Wifimanager.getconfigurednetworks (); 
Scan Configuration List} public list<scanresult> getwifilist () {return scanresultlist; 
Public list<wificonfiguration> getwificonfiglist () {return wificonfiglist; 
//Get scan list public StringBuilder Lookupscan () {StringBuilder scanbuilder = new StringBuilder (); 
for (int i = 0; i < scanresultlist.size (); i++){Scanbuilder.append ("No.:" + (i + 1)); 
Scanbuilder.append (Scanresultlist.get (i). toString ()); 
All information scanbuilder.append ("\ n"); 
return scanbuilder; 
//Gets the strength of the specified signal public int getlevel (int netid) {return scanresultlist.get (NetID). level; //Get native MAC address public String Getmac () {return (Wifiinfo = = null)? 
"": wifiinfo.getmacaddress (); 
Public String Getbssid () {return (Wifiinfo = = null)? Null:wifiInfo.getBSSID (); 
Public String Getssid () {return (Wifiinfo = = null)? Null:wifiInfo.getSSID (); 
//returns the ID public int Getcurrentnetid () of the currently connected network (Wifiinfo = null)? Null:wifiInfo.getNetworkId (); 
//Return all information public String Getwifiinfo () {returns (Wifiinfo = null)? null:wifiInfo.toString (); 
//Get IP address public int getip () {return (Wifiinfo = = null)? Null:wifiInfo.getIpAddress (); 
//Add a connection public boolean addnetwordlink (wificonfiguration config) {int netid = wifimanager.addnetwork (config); 
Return Wifimanager.enablenetwork (NetID, true);//Disable a link public boolean disablenetwordlick (int netid) {wifimanager.disablenetwork (NetID); 
return Wifimanager.disconnect (); 
//Remove a link public boolean removenetworklink (int netid) {return wifimanager.removenetwork (NetID); 
//Do not show SSID public void Hiddenssid (int netid) {wificonfiglist.get (netid). Hiddenssid=true; 
//Show SSID public void Displayssid (int netid) {wificonfiglist.get (netid). Hiddenssid=false; } 
}

Of course, in the operation of WiFi also need the appropriate permissions, personally feel this is the use of the most permissions, of course, the operation of WiFi can not be carried out in the simulator, must be placed on the real computer with WiFi, there are no possible errors for the corresponding capture and processing, I hope the reference to the friend to note this, Otherwise may easily be suddenly out of the error misleading, also can not find the problem, I was in the development of the time encountered such a problem! Therefore, the possible problems must be handled accordingly!

The following are the permissions that are required to manipulate these, depending on the content of the operation, the permissions may be different, and the following permissions are for informational purposes only:

 
 

About Android Management and operation WiFi Simple example source code is introduced here, if you find that the code has errors or incomplete words, hope to send a message to me, to improve! Only mutual communication can progress!

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.