Android WIFI operation

Source: Internet
Author: User
Tags get ip

Code Packagecom.wifitest;Importjava.util.List;ImportAndroid.content.Context;ImportAndroid.net.wifi.ScanResult;Importandroid.net.wifi.WifiConfiguration;ImportAndroid.net.wifi.WifiInfo;ImportAndroid.net.wifi.WifiManager;ImportAndroid.net.wifi.WifiManager.WifiLock; Public classWifimanageclass {PrivateWifimanager Wifimanager;//declaring Management Objects    PrivateWifiinfo Wifiinfo;//WiFi Information    PrivateList<scanresult> scanresultlist;//List of scanned network Connections    PrivateList<wificonfiguration> wificonfiglist;//Network Configuration list    PrivateWifilock Wifilock;//WiFi Lock     PublicWifimanageclass (Context context) { This. Wifimanager =(Wifimanager) context. Getsystemservice (context.wifi_service);//Get WiFi service//Get WiFi Info         This. Wifiinfo = Wifimanager.getconnectioninfo ();//Get connection Information    }         Public BooleanGetwifistatus () {returnwifimanager.iswifienabled (); }    //turn WiFi on/off     Public BooleanOpenwifi () {if(!wifimanager.iswifienabled ()) {            returnWifimanager.setwifienabled (true); } Else {            return false; }    }     Public BooleanClosewifi () {if(!wifimanager.iswifienabled ()) {            return true; } Else {            returnWifimanager.setwifienabled (false); }    }    //lock/unlock WiFi//in fact, lock WiFi is to determine whether WiFi is established success, here is the use of held, handshake meaning acquire get!      Public voidLockwifi () {wifilock.acquire (); }     Public voidUnlockwifi () {if(!Wifilock.isheld ()) {wifilock.release ();//Freeing Resources        }    }    //I was writing in the constructor, but considering not always using the WiFi lock, so simply build a method! Call when needed, build on OK     Public voidCreatewifilock () {Wifilock= Wifimanager.createwifilock ("FlyFly");//Create a flag for a lock    }    //Scan Network     Public voidStartscan () {Wifimanager.startscan (); Scanresultlist= Wifimanager.getscanresults ();//Scan returns list of resultswificonfiglist= Wifimanager.getconfigurednetworks ();//Scan Configuration List    }     PublicList<scanresult>getwifilist () {returnscanresultlist; }     PublicList<wificonfiguration>getwificonfiglist () {returnwificonfiglist; }    //Get scan list     PublicStringBuilder Lookupscan () {StringBuilder Scanbuilder=NewStringBuilder ();  for(inti = 0; I < scanresultlist.size (); i++) {scanbuilder.append ("No.:" + (i + 1));  Scanbuilder.append (Scanresultlist.get (i). toString ()); //All informationScanbuilder.append ("\ n"); }        returnScanbuilder; }    //gets the intensity of the specified signal     Public intGetlevel (intNetId) {        returnScanresultlist.get (NetId). level; }    //get the native MAC address     PublicString Getmac () {return(Wifiinfo = =NULL) ? "": Wifiinfo.getmacaddress (); }     PublicString Getbssid () {return(Wifiinfo = =NULL) ?NULL: Wifiinfo.getbssid (); }     PublicString Getssid () {return(Wifiinfo = =NULL) ?NULL: Wifiinfo.getssid (); }    //returns the ID of the network that is currently connected     Public intGetcurrentnetid () {return(Wifiinfo = =NULL) ?NULL: Wifiinfo.getnetworkid (); }    //Return all information     PublicString Getwifiinfo () {return(Wifiinfo = =NULL) ?NULL: Wifiinfo.tostring (); }    //Get IP Address     Public intGetIP () {return(Wifiinfo = =NULL) ?NULL: Wifiinfo.getipaddress (); }    //Add a connection     Public Booleanaddnetwordlink (wificonfiguration config) {intNetId =wifimanager.addnetwork (config); returnWifimanager.enablenetwork (NetId,true); }    //Disable a link     Public BooleanDisablenetwordlick (intNetId)        {wifimanager.disablenetwork (NetId); returnWifimanager.disconnect (); }    //Remove a link     Public BooleanRemovenetworklink (intNetId) {        returnwifimanager.removenetwork (NetId); }    //do not display SSID     Public voidHiddenssid (intNetId) {Wificonfiglist.get (NetId). Hiddenssid=true; }    //Show SSID     Public voidDisplayssid (intNetId) {Wificonfiglist.get (NetId). Hiddenssid=false; }    }

Android WIFI operation

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.