Android WiFi Methods

Source: Internet
Author: User
Tags bssid

The first thing to emphasize is that WiFi can only be tested on a real machine!

In Android, all WiFi operations are performed in android.net. wiFi bag, And the commonly used classes only use a few. For general developers, the commonly used operations are also limited and do not need to be deliberately remembered, when I use it, I can check it now. The following is an example of WiFi in the secrets of Android Application Development. I personally feel that it is quite complete. I 'd like to share it with you!

 

Package COM. yarin. android. examples_08_08; <br/> Import Java. util. list; <br/> Import android. content. context; <br/> Import android.net. wiFi. scanresult; <br/> Import android.net. wiFi. wificonfiguration; <br/> Import android.net. wiFi. wifiinfo; <br/> Import android.net. wiFi. wifimanager; <br/> Import android.net. wiFi. wifimanager. wifilock; <br/> public class wifiadmin <br/>{< br/> // defines the wifimanager object <br/> private Wifimanager mwifimanager; <br/> // defines the wifiinfo object <br/> private wifiinfo mwifiinfo; <br/> // list of scanned Network Connections <br/> private list <scanresult> mwifilist; <br/> // network connection list <br/> private list <wificonfiguration> mwificonfiguration; <br/> // define a wifilock <br/> wifilock mwifilock; <br/> // constructor <br/> Public wifiadmin (context) <br/>{< br/> // obtain the wifimanager object <br/> mwifimanager = (wifimanager) context. getsystemse Rvice (context. wifi_service); <br/> // obtain the wifiinfo object <br/> mwifiinfo = mwifimanager. getconnectioninfo (); <br/>}< br/> // enable WiFi <br/> Public void openwifi () <br/>{< br/> If (! Mwifimanager. iswifienabled () <br/>{< br/> mwifimanager. setwifienabled (true); </P> <p >}< br/> // disable WiFi <br/> Public void closewifi () <br/>{< br/> If (! Mwifimanager. iswifienabled () <br/>{< br/> mwifimanager. setwifienabled (false); <br/>}< br/> // lock wifilock <br/> Public void acquirewifilock () <br/>{< br/> mwifilock. acquire (); <br/>}< br/> // unlock wifilock <br/> Public void releasewifilock () <br/>{< br/> // lock upon judgment <br/> If (mwifilock. isheld () <br/>{< br/> mwifilock. acquire (); <br/>}< br/> // create a wifilock <br/> Public void creatwifi Lock () <br/>{< br/> mwifilock = mwifimanager. createwifilock ("test"); <br/>}< br/> // The configured network <br/> public list <wificonfiguration> getconfiguration () <br/>{< br/> return mwificonfiguration; <br/>}< br/> // specify the configured network for connection <br/> Public void connectconfiguration (INT index) <br/>{< br/> // If (index> mwificonfiguration. size () <br/>{< br/> return; <br/>}< br/> // connect to the network with the specified ID configured <br /> Mwifimanager. enablenetwork (mwificonfiguration. get (index ). networkid, true); <br/>}< br/> Public void startscan () <br/>{< br/> mwifimanager. startscan (); <br/> // obtain the scan result <br/> mwifilist = mwifimanager. getscanresults (); <br/> // obtain the configured network connection <br/> mwificonfiguration = mwifimanager. getconfigurednetworks (); <br/>}< br/> // obtain the Network List <br/> public list <scanresult> getwifilist () <br/>{< br/> return mwifili St; <br/>}< br/> // View scan results <br/> Public stringbuilder lookupscan () <br/>{< br/> stringbuilder = new stringbuilder (); <br/> for (INT I = 0; I <mwifilist. size (); I ++) <br/>{< br/> stringbuilder. append ("index _" + new INTEGER (I + 1 ). tostring () + ":"); <br/> // convert scanresult information into a string package <br/> // includes: bssid, SSID, capabilities, frequency, level <br/> stringbuilder. append (mwifilist. get (I )). tostri Ng (); <br/> stringbuilder. append ("/N"); <br/>}< br/> return stringbuilder; <br/>}< br/> // obtain the MAC address <br/> Public String getmacaddress () <br/>{< br/> return (mwifiinfo = NULL )? "Null": mwifiinfo. getmacaddress (); <br/>}< br/> // obtain the bssid of the Access Point <br/> Public String getbssid () <br/>{< br/> return (mwifiinfo = NULL )? "Null": mwifiinfo. getbssid (); <br/>}< br/> // obtain the IP address <br/> Public int getipaddress () <br/>{< br/> return (mwifiinfo = NULL )? 0: mwifiinfo. getipaddress (); <br/>}< br/> // obtain the connection id <br/> Public int getnetworkid () <br/>{< br/> return (mwifiinfo = NULL )? 0: mwifiinfo. getnetworkid (); <br/>}< br/> // obtain all information packages of wifiinfo. <br/> Public String getwifiinfo () <br/>{< br/> return (mwifiinfo = NULL )? "Null": mwifiinfo. tostring (); <br/>}< br/> // Add a network and connect <br/> Public void addnetwork (wificonfiguration WCG) <br/>{< br/> int wcgid = mwifimanager. addnetwork (WCG); <br/> mwifimanager. enablenetwork (wcgid, true); <br/>}< br/> // disconnect the network with the specified id <br/> Public void disconnectwifi (INT netid) <br/>{< br/> mwifimanager. disablenetwork (netid); <br/> mwifimanager. disconnect (); <br/>}< br/>}

 

I will not explain the detailed description in it. When will it be used? It will be okay when I find it here!

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.