For WiFi operations in Android, Android provides some useful packages under the android.net. WiFi package. A Brief Introduction:
It can be roughly divided into four main classes: scanresult, wificonfiguration, wifiinfo, and wifimanager.
(1) scanresult: scan the Wi-Fi hardware to obtain information about nearby Wi-Fi hotspots.
(2) Some information that needs to be obtained when we connect to a Wi-Fi access point. You can compare it with our wired devices.
(3) After the Wi-Fi connection is established, wifiinfo can obtain the information of the connected wi-fi connection through this class. The information here is relatively simple, here is a brief introduction of the methods here:
Getbssid () Get bssid
Getdetailedstateof () to obtain client connectivity
Gethiddenssid () indicates whether the SSID is hidden.
Getipaddress () Get the IP address
Getlinkspeed () Get the connection speed
Getmacaddress () Get the MAC address
Get the signal of the 802.11n network by getarg ()
Getssid () Get the SSID
Getsupplicanstate () returns information about the specific client status.
(4) wifimanager is used to manage our Wi-Fi connections. Some classes have been defined here for our use. It is relatively complicated here, and there are a lot of content in it, but we can still get a lot of relevant information through the literal meaning. Many constants are pre-defined in this class, which can be used directly without being created again.
The introduction is complete. Start to write a demo. The program has been started as follows:
I won't say much about the code with comments.
Layout file code:
<? XML version = "1.0" encoding = "UTF-8"?> <Scrollview xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/mscrollview" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: scrollbars = "vertical"> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: Id = "@ + ID/allnetwork" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: TEXT = "No Wi-Fi network is scanned currently"/> <button Android: Id = "@ + ID/scan" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "scan network"/> <button Android: Id = "@ + ID/start" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "enable WiFi"/> <button Android: Id = "@ + ID/Stop" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Disable WiFi"/> <button Android: Id = "@ + ID/check" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "WiFi status"/> </linearlayout> </scrollview>
The scrollview in the layout File above is used to display the scroll bar on the right in the activity. If there is too much data, you can slide down to continue displaying undisplayed data.
Homepage code:
Package Org. sunchao; import Java. util. list; import android. app. activity; import android.net. wiFi. scanresult; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. scrollview; import android. widget. textview; import android. widget. toast; public class main extends activity implements onclicklistener {// Private SCR Ollview sview; private textview allnetwork; private button scan; private button start; private button stop; private button check; private wifiadmin mwifiadmin; // scan result list private list <scanresult> list; private scanresult mscanresult; private stringbuffer mstringbuffer = new stringbuffer ();/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreat E (savedinstancestate); setcontentview (R. layout. main); mwifiadmin = new wifiadmin (main. this); Init () ;}// public void Init () {sview = (scrollview) findviewbyid (R. id. mscrollview); allnetwork = (textview) findviewbyid (R. id. allnetwork); scan = (button) findviewbyid (R. id. scan); Start = (button) findviewbyid (R. id. start); stop = (button) findviewbyid (R. id. stop); check = (button) findviewbyid (R. id. check); SCA N. setonclicklistener (main. this); start. setonclicklistener (main. this); stop. setonclicklistener (main. this); check. setonclicklistener (main. this);} // wifi_state_disabling 0 // wifi_state_disabled 1 // wifi_state_enabling 2 // wifi_state_enabled 3 Public void start () {mwifiadmin. openwifi (); toast. maketext (main. this, "the current WiFi Nic status is" + mwifiadmin. checkstate (), toast. length_short ). show ();} public void stop () {mwifiad Min. closewifi (); toast. maketext (main. this, "the current WiFi Nic status is" + mwifiadmin. checkstate (), toast. length_short ). show ();} public void check () {toast. maketext (main. this, "the current WiFi Nic status is" + mwifiadmin. checkstate (), toast. length_short ). show ();} public void getallnetworklist () {// clear the last scan result if (mstringbuffer! = NULL) {mstringbuffer = new stringbuffer () ;}// start scanning the network mwifiadmin. startscan (); List = mwifiadmin. getwifilist (); If (list! = NULL) {for (INT I = 0; I <list. size (); I ++) {mscanresult = List. get (I); // get the network SSID: The network namemstringbuffer = mstringbuffer. append (mscanresult. SSID ). append (""). append (mscanresult. bssid ). append (""). append (mscanresult. capabilities ). append (""). append (mscanresult. frequency ). append (""). append (mscanresult. level ). append (""). append ("\ n");} allnetwork. settext ("all scanned Wi-Fi networks: \ n" + mstringbuffer. tostring () ;}@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. SCAN: getallnetworklist (); break; case R. id. start: Start (); break; case R. id. stop: Stop (); break; case R. id. check: Check (); break; default: break ;}}}
A wifiadmin class is used above, which is a Class I wrote myself. It is used to encapsulate the WiFi operation once. There are many methods in it. We only use several of them in this demo, but I should put it all first, and I will use it later.
Wifiadmin tool code:
Package Org. sunchao; 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 wifiadmin {// defines the wifimanager object private wifimanager mwifimanager; // defines the wifiinfo object private wifiinfo mwifiinfo; // The scanned network connection list is private List <scanresult> mwifilist; // network connection list private list <wificonfiguration> mwificonfiguration; // defines a wifilockwifilock mwifilock; // The constructor public wifiadmin (context) {// obtain the wifimanager object mwifimanager = (wifimanager) context. getsystemservice (context. wifi_service); // obtain the wifiinfo object mwifiinfo = mwifimanager. getconnectioninfo () ;}// open wifipublic void openwifi () {If (! Mwifimanager. iswifienabled () {mwifimanager. setwifienabled (true) ;}/// disable wifipublic void closewifi () {If (mwifimanager. iswifienabled () {mwifimanager. setwifienabled (false) ;}// check the current WiFi status public int checkstate () {return mwifimanager. getwifistate ();} // lock wifilockpublic void acquirewifilock () {mwifilock. acquire ();} // unlock wifilockpublic void releasewifilock () {// lock if (mwifilock. isheld () {mwifilock. A Cquire () ;}}// create a wifilockpublic void creatwifilock () {mwifilock = mwifimanager. createwifilock ("test");} // The configured public list <wificonfiguration> getconfiguration () {return mwificonfiguration ;} // specify the configured network to connect to the public void connectconfiguration (INT index) {// If (index> mwificonfiguration is returned if the index is greater than the configured network index. size () {return;} // connect the network mwifimanager with the specified ID. enablenetwork (mwificonfiguration. get (index ). networkid, True);} public void startscan () {mwifimanager. startscan (); // obtain the scan result mwifilist = mwifimanager. getscanresults (); // obtain the configured network connection mwificonfiguration = mwifimanager. getconfigurednetworks ();} // obtain the public list of networks <scanresult> getwifilist () {return mwifilist;} // view the scan result public stringbuilder lookupscan () {stringbuilder = new stringbuilder (); For (INT I = 0; I <mwifilist. size (); I ++) {stringbuilder. A Ppend ("index _" + new INTEGER (I + 1 ). tostring () + ":"); // converts scanresult information into a string package. // This includes bssid, SSID, capabilities, frequency, and levelstringbuilder. append (mwifilist. get (I )). tostring (); stringbuilder. append ("/N") ;}return stringbuilder ;}// obtain the MAC address public string getmacaddress () {return (mwifiinfo = NULL )? "Null": mwifiinfo. getmacaddress () ;}// obtain the Access Point's bssidpublic string getbssid () {return (mwifiinfo = NULL )? "Null": mwifiinfo. getbssid () ;}// obtain the IP address public int getipaddress () {return (mwifiinfo = NULL )? 0: mwifiinfo. getipaddress () ;}// obtain the connected idpublic int getnetworkid () {return (mwifiinfo = NULL )? 0: mwifiinfo. getnetworkid () ;}// get all information packages of wifiinfo Public String getwifiinfo () {return (mwifiinfo = NULL )? "Null": mwifiinfo. tostring () ;}// Add a network and connect to the public void addnetwork (wificonfiguration WCG) {int wcgid = mwifimanager. addnetwork (WCG); mwifimanager. enablenetwork (wcgid, true);} // disconnect the Network Public void disconnectwifi (INT netid) {mwifimanager. disablenetwork (netid); mwifimanager. disconnect ();}}
Finally, let's take a look at the androidmanifest. xml configuration file code, which has the required permissions. This is important. At the beginning, I forgot to add these permissions, and the program went down as soon as it was started. Then you can see that logcat knows that these permissions are missing.
Androidmanifest. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "org. sunchao "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 8 "/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <activity Android: Name = ". main "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category androi D: Name = "android. Intent. Category. launcher"/> </intent-filter> </activity> </Application> <! -- The following are the permissions required to access the network using WiFi --> <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> </manifest>
Now this demo is complete.