WiFi in Android

Source: Internet
Author: User
Tags bssid

Android provides the android.net. wifi package for us to operate on WiFi. First, we will introduce several categories: WifiManager: provides most of the APIs for managing Wi-Fi connections ScanResult: detected access points (including the Access Point address, name, identity authentication, frequency, and signal strength) wifiConfiguration: network configuration for Wi-Fi connection (including security configuration) WifiInfo: description of Wi-Fi wireless connection (including access point, network connection status, hidden access point, IP address, connection speed, MAC address, network ID, and signal strength) WifiManager. wifiLock: allows applications to use WiFI wireless networks all the time. Generally, when users do not perform any operation for a period of time, the Wi-Fi network is automatically disabled. We use WifiLock to lock the Wi-Fi network and keep it connected until the lock is released. Multiple applications may have multiple locks. When the locks of multiple applications are released, WiFi is disabled. The following code describes how to use WiFI: Get the WifiManager object through the following code (Wifi is a service of the system) [java] WifiManager wifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); WifiManager wifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); Enable WiFI [java] view plaincopyprint? Public void openWifi () {if (! MWifiManager. isWifiEnabled () {mWifiManager. setWifiEnabled (true) ;}} public void openWifi () {if (! MWifiManager. isWifiEnabled () {mWifiManager. setWifiEnabled (true) ;}} disable WiFi [java] view plaincopyprint? Public void closeWifi () {if (! MWifiManager. isWifiEnabled () {mWifiManager. setWifiEnabled (false) ;}} public void closeWifi () {if (! MWifiManager. isWifiEnabled () {mWifiManager. setWifiEnabled (false) ;}} create a WifiLock [java] view plaincopyprint? Public void creatWifiLock () {mWifiLock = mWifiManager. createWifiLock ("Test");} public void creatWifiLock () {mWifiLock = mWifiManager. createWifiLock ("Test");} Lock WifiLock [java] view plaincopyprint? Public void acquireWifiLock () {mWifiLock. acquire ();} public void acquireWifiLock () {mWifiLock. acquire ();} release WifiLock [java] view plaincopyprint? Public void releaseWifiLock () {if (mWifiLock. isHeld () {mWifiLock. release () ;}} public void releaseWifiLock () {if (mWifiLock. isHeld () {mWifiLock. release () ;}} Add a WiFi and connect to [java] view plaincopyprint? Public void addNetworkAndConn (WifiConfiguration wcg) {int netId = mWifiManager. addNetwork (wcg); mWifiManager. enableNetwork (netId, true);} public void addNetworkAndConn (WifiConfiguration wcg) {int netId = mWifiManager. addNetwork (wcg); mWifiManager. enableNetwork (netId, true);} disconnect a WiFi [java] view plaincopyprint with the specified ID? Public void disconnectWifi (int netId) {mWifiManager. disableNetwork (netId); mWifiManager. disconnect ();} public void disconnectWifi (int netId) {mWifiManager. disableNetwork (netId); mWifiManager. disconnect ();} connects to a configured WiFi [html] view plaincopyprint? Public void connectConfiguration (int index) {// if (index> mWifiConfiguration. size () {return;} mWifiManager. enableNetwork (mWifiConfiguration. get (index ). networkId, true);} public void connectConfiguration (int index) {// if (index> mWifiConfiguration. size () {return;} mWifiManager. enableNetwork (mWifiConfiguration. get (index ). networkId, true);} scans accessible WiFi [java] public v Oid startScan () {mWifiManager. startScan (); // obtain the scan result List <ScanResult> wifiList = mWifiManager. getScanResults (); // obtain the configured network connection List <WifiConfiguration> wifiConfigList = mWifiManager. getConfiguredNetworks ();} public void startScan () {mWifiManager. startScan (); // obtain the scan result List <ScanResult> wifiList = mWifiManager. getScanResults (); // obtain the configured network connection List <WifiConfiguration> wifiConfigList = mWifiManager. getConfi GuredNetworks ();} View scan results [java] public StringBuilder lookUpScan () {StringBuilder stringBuilder = new StringBuilder (); for (int I = 0; I <mWifiList. size (); I ++) {stringBuilder. append ("Index _" + new Integer (I + 1 ). toString () + ":"); // converts ScanResult information into a string package. // This includes BSSID, SSID, capabilities, frequency, and level stringBuilder. append (mWifiList. get (I )). toString (); stringBuilder. append ("\ n");} return st RingBuilder;} public StringBuilder lookUpScan () {StringBuilder stringBuilder = new StringBuilder (); for (int I = 0; I <mWifiList. size (); I ++) {stringBuilder. append ("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 ;}get some WiFi information [Java] view plaincopyprint? WifiInfo mifiInfo = mWifiManager. getConnectionInfo (); // obtain the MAC address www.2cto. compublic String GetMacAddress () {return (mWifiInfo = null )? "NULL": mWifiInfo. getMacAddress () ;}// obtain the Access Point's BSSID public String GetBSSID () {return (mWifiInfo = null )? "NULL": mWifiInfo. getBSSID () ;}// obtain the IP address public int GetIPAddress () {return (mWifiInfo = null )? 0: mWifiInfo. getIpAddress () ;}// obtain the connection ID public int GetNetworkId () {return (mWifiInfo = null )? 0: mWifiInfo. getNetworkId () ;}// get all information packages of WifiInfo public String GetWifiInfo () {return (mWifiInfo = null )? "NULL": mWifiInfo. toString ();} WifiInfo mifiInfo = mWifiManager. getConnectionInfo (); // 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 ();} Finally, the required permission to use WiFi [html] <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_CHECKIN_PROPERTIES "> </uses-permission> <uses-permission android: name =" android. permission. WAKE_LOCK "> </uses-permission> <uses-permission android: name =" android. permission. INTERNET "> </uses-permission> <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "> </uses-permission> <uses-permission android: name =" android. permission. MODIFY_PHONE_STATE "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_WIFI_STATE "> </uses-permission> <uses-permission android: name =" android. permission. ACCESS_CHECKIN_PROPERTIES "> </uses-permission> <uses-permission android: name =" android. permission. WAKE_LOCK "> </uses-permission> <uses-permission android: name =" android. permission. INTERNET "> </uses-permission> <uses-permission android: name =" android. permission. CHANGE_WIFI_STATE "> </uses-permission> <uses-permission android: name =" android. permission. MODIFY_PHONE_STATE "> </uses-permission>

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.