WiFi learning for Android (2) -- connecting to wifi

Source: Internet
Author: User

Previous articles have briefly explained the basic knowledge of wifi. For more information, see WiFi learning for Android (1 ). On this basis, this article further improves program functions, the main functions are to enable the wireless network card, disable the wireless network card, check the network card status, scan the network, scan the results, connect to wifi, disconnect WiFi, WiFi connection status.
Run the following command:

Connecting to wifi is a headache for me. You need to connect in the program. There are two cases. First, WiFi can be directly connected without a password. Second, WiFi has a password, and a password is provided in the program, and then connected. First, I can check the relevant information. Second, I did not find it, and I did not know how to do it. Finally, I came up with a way to directly jump the program to the Wi-Fi page set by the system, and then let people set it manually. I have observed the QQ version of Android. Believe it or not, look:

Click set network to go to the system settings page:

So I can also set up the network here, but it is still a bit uncomfortable. Maybe I am not very familiar with WiFi. I will continue to read the relevant API documentation and share it with you later, paste the source code below:
Home page:

Package Org. sunchao; import android. app. activity; import android. content. intent; 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; public class main extends activity implements onclicklistener {// Private scrollview sview; private button opennetcard; private button closenetcard; private button checknetcardstate; private button scan; private button getscanresult; private button connect; private button disconnect; private button checknetworkstate; private textview scanresult; private string mscanresult; private wifiadmin mwifiadmin;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mwifiadmin = new wifiadmin (main. this); Init () ;}/ *** button and other controls */Public void Init () {sview = (scrollview) findviewbyid (R. id. mscrollview); opennetcard = (button) findviewbyid (R. id. opennetcard); closenetcard = (button) findviewbyid (R. id. closenetcard); checknetcardstate = (button) findviewbyid (R. id. checknetcardstate); scan = (button) findviewbyid (R. id. scan); getscanresult = (button) findviewbyid (R. id. getscanresult); scanresult = (textview) findviewbyid (R. id. scanresult); Connect = (button) findviewbyid (R. id. connect); disconnect = (button) findviewbyid (R. id. disconnect); checknetworkstate = (button) findviewbyid (R. id. checknetworkstate); opennetcard. setonclicklistener (main. this); closenetcard. setonclicklistener (main. this); checknetcardstate. setonclicklistener (main. this); scan. setonclicklistener (main. this); getscanresult. setonclicklistener (main. this); Connect. setonclicklistener (main. this); disconnect. setonclicklistener (main. this); checknetworkstate. setonclicklistener (main. this);}/*** wifi_state_disabling 0 wifi_state_disabled 1 wifi_state_enabling 2 * wifi_state_enabled 3 */Public void opennetcard () {mwifiadmin. opennetcard ();} public void closenetcard () {mwifiadmin. closenetcard ();} public void checknetcardstate () {mwifiadmin. checknetcardstate ();} public void scan () {mwifiadmin. scan ();} public void getscanresult () {mscanresult = mwifiadmin. getscanresult (); scanresult. settext (mscanresult);} public void connect () {mwifiadmin. connect (); // startactivityforresult (new intent (// android. provider. settings. action_wifi_settings), 0); startactivity (new intent (Android. provider. settings. action_wifi_settings);} public void disconnect () {mwifiadmin. disconnectwifi ();} public void checknetworkstate () {mwifiadmin. checknetworkstate () ;}@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. opennetcard: opennetcard (); break; case R. id. closenetcard: closenetcard (); break; case R. id. checknetcardstate: checknetcardstate (); break; case R. id. SCAN: scan (); break; case R. id. getscanresult: getscanresult (); break; case R. id. connect: connect (); break; case R. id. disconnect: disconnect (); break; case R. id. checknetworkstate: checknetworkstate (); break; default: Break ;}}}

WiFi tool:

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; import android. util. log; public class wifiadmin {private final static string tag = "wifiadmin"; private stringbuffer mstringbuffer = new St Ringbuffer (); Private list <scanresult> listresult; private scanresult mscanresult; // defines the wifimanager object private wifimanager mwifimanager; // defines the wifiinfo object private wifiinfo mwifiinfo; // network connection list private list <wificonfiguration> mwificonfiguration; // defines a wifilockwifilock mwifilock;/*** constructor */Public wifiadmin (context) {mwifimanager = (wifimanager) context. getsystemservice (context. wifi_service); mwifiinfo = Mwifimanager. getconnectioninfo ();}/*** enable WiFi Nic */Public void opennetcard () {If (! Mwifimanager. iswifienabled () {mwifimanager. setwifienabled (true) ;}}/*** disable WiFi Nic */Public void closenetcard () {If (mwifimanager. iswifienabled () {mwifimanager. setwifienabled (false) ;}}/*** check the current WiFi Nic status */Public void checknetcardstate () {If (mwifimanager. getwifistate () = 0) {log. I (TAG, "Nic is disabled");} else if (mwifimanager. getwifistate () = 1) {log. I (TAG, "Nic disabled");} else if (mwifimanager. getwifistate () = 2) {log. I (TAG, "Nic is on");} else if (mwifimanager. getwifistate () = 3) {log. I (TAG, "Nic enabled");} else {log. I (TAG, "--- _ --- dizzy ...... no status is obtained --- _ --- ") ;}/ *** scan surrounding Network */Public void scan () {mwifimanager. startscan (); listresult = mwifimanager. getscanresults (); If (listresult! = NULL) {log. I (TAG, "wireless network exists in the current region, please view scan results");} else {log. I (TAG, "No wireless network in current region") ;}/ *** get scan result */Public String getscanresult () {// clear the previous scan result if (mstringbuffer! = NULL) {mstringbuffer = new stringbuffer () ;}// start scanning network scan (); listresult = mwifimanager. getscanresults (); If (listresult! = NULL) {for (INT I = 0; I <listresult. size (); I ++) {mscanresult = listresult. get (I); mstringbuffer = mstringbuffer. append ("no. "). append (I + 1 ). append (":"). append (mscanresult. SSID ). append ("-> "). append (mscanresult. bssid ). append ("-> "). append (mscanresult. capabilities ). append ("-> "). append (mscanresult. frequency ). append ("-> "). append (mscanresult. level ). append ("-> "). append (mscanresult. describecontents ()). Append ("\ n") ;}} log. I (TAG, mstringbuffer. tostring (); Return mstringbuffer. tostring ();}/*** connect to the specified Network */Public void connect () {mwifiinfo = mwifimanager. getconnectioninfo ();}/*** disconnect the current network */Public void disconnectwifi () {int netid = getnetworkid (); mwifimanager. disablenetwork (netid); mwifimanager. disconnect (); mwifiinfo = NULL;}/*** check the current network status ** @ return string */Public void checknetworkstate () {If (MW Ifiinfo! = NULL) {log. I (TAG, "network working properly");} else {log. I (TAG, "Network disconnected") ;}/ *** get the connection ID */Public int getnetworkid () {return (mwifiinfo = NULL )? 0: mwifiinfo. getnetworkid ();}/*** get the IP address */Public int getipaddress () {return (mwifiinfo = NULL )? 0: mwifiinfo. getipaddress ();} // lock wifilockpublic void acquirewifilock () {mwifilock. acquire ();} // unlock wifilockpublic void releasewifilock () {// lock if (mwifilock. isheld () {mwifilock. acquire () ;}}// create a wifilockpublic void creatwifilock () {mwifilock = mwifimanager. createwifilock ("test");} // get the configured network public list <wificonfiguration> getconfiguration () {return mwificonfiguration;} // specify the configured network to connect to public v Oid connectconfiguration (INT index) {// If (index> = mwificonfiguration 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);} // 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 () ;}// get all information packages of wifiinfo Public String getwifiinfo () {return (mwifiinfo = NULL )? "Null": mwifiinfo. tostring () ;}// Add a network and connect to the public int addnetwork (wificonfiguration WCG) {int wcgid = mwifimanager. addnetwork (mwificonfiguration. get (3); mwifimanager. enablenetwork (wcgid, true); Return wcgid ;}}

Androidmanifest. xml: (Note the permissions to be added)

<? 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>

Layout file is not pasted, I want to see the interface you can write it out, if necessary, here you can download the source code: http://download.csdn.net/source/3449837

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.