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