[Android] wifi development, androidwifi

Source: Internet
Author: User
Tags bssid

[Android] wifi development, androidwifi

WIFI is a wireless networking technology, commonly used by wireless routers. In this case, Wi-Fi connections can be used within the signal coverage of the wireless router. If a wireless router is connected to an ADSL line or other Internet connection lines, it is also called a "hot spot ".

For Wifi operations in Android, android provides some useful packages under the android.net. wifi package. It mainly includes the following classes and interfaces:

1. ScanResult

It is mainly used to describe the detected access point, including the Access Point address, Access Point name, identity authentication, frequency, and signal strength.

2. WifiConfiguration

Wi-Fi network configuration, including security settings.

3. 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. 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

Needless to say, it is used to manage our Wi-Fi connections. Some classes have been defined here for our use.

Obtain the status of the Wi-Fi network card
The status of a Wi-Fi network card is represented by a series of plastic constants.
1. WIFI_STATE_DISABLED: WIFI Nic unavailable (1)
2. WIFI_STATE_DISABLING: the WIFI Nic is being disabled (0)
3. WIFI_STATE_ENABLED: WIFI Nic available (3)
4. WIFI_STATE_ENABLING: the WIFI network is on (2) (it takes some time to start the WIFI)

5. WIFI_STATE_UNKNOWN: Unknown Nic status

Write an instance of wifi first:

First, take a look at the following main. xml:

 

<? Xml version = "1.0" encoding = "UTF-8"?> <ScrollView xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <LinearLayout android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <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"/> <TextView android: id = "@ + id/allNetWork" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "No Wi-Fi network is scanned currently"/> </LinearLayout> </ScrollView>

 


Next, we will encapsulate wifi-related operations in a WifiAdmin class. You can directly call the relevant methods of this class to enable or disable such operations in the future:

 

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 a WifiManager object private WifiManager mWifiManager; // defines a WifiInfo object private WifiInfo mWifiInfo; // The List of Network Connections scanned for private List <ScanResult> mWifiList; // network connection List private List <WifiConfiguration> mWifiConfigurations; WifiLock mWifiLock; public WifiAdmin (Context context) {// obtain the WifiManager object mWifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE); // obtain the WifiInfo object mWifiInfo = mWifiManager. getConnectionInfo () ;}// enable wifi public void openWifi () {if (! MWifiManager. isWifiEnabled () {mWifiManager. setWifiEnabled (true) ;}// disable wifi public void closeWifi () {if (! MWifiManager. isWifiEnabled () {mWifiManager. setWifiEnabled (false) ;}// check the current wifi status public int checkState () {return mWifiManager. getWifiState ();} // lock wifiLock public void acquireWifiLock () {mWifiLock. acquire ();} // unlock wifiLock public void releaseWifiLock () {// determine whether to lock if (mWifiLock. isHeld () {mWifiLock. acquire () ;}}// create a wifiLock public void createWifiLock () {mWifiLock = mWifiManager. createWifiLock ("Test") ;}// get the configured network public List <WifiConfiguration> getConfiguration () {return mWifiConfigurations;} // specify the configured network to connect to the public void connetionConfiguration (int index) {if (index> mWifiConfigurations. size () {return;} // connect the network mWifiManager with the specified ID configured. enableNetwork (mWifiConfigurations. get (index ). networkId, true);} public void startScan () {mWifiManager. startScan (); // obtain the scan result mWifiList = mWifiManager. getScanRes Ults (); // The configured network connection mWifiConfigurations = mWifiManager. getConfiguredNetworks ();} // obtain the public List of networks <ScanResult> getWifiList () {return mWifiList;} // view the scan result public StringBuffer lookUpScan () {StringBuffer sb = new StringBuffer (); for (int I = 0; I <mWifiList. size (); I ++) {sb. append ("Index _" + new Integer (I + 1 ). toString () + ":"); // converts ScanResult information into a string package. // This includes BSSID, SSID, capabilities, frequency, and level s. B. append (mWifiList. get (I )). toString ()). append ("\ n");} return sb;} public String getMacAddress () {return (mWifiInfo = null )? "NULL": mWifiInfo. getMacAddress ();} public String getBSSID () {return (mWifiInfo = null )? "NULL": mWifiInfo. getBSSID ();} public int getIpAddress () {return (mWifiInfo = null )? 0: mWifiInfo. getIpAddress () ;}// obtain the connection ID public int getNetWordId () {return (mWifiInfo = null )? 0: mWifiInfo. getNetworkId () ;}// get all information of wifiInfo public String getWifiInfo () {return (mWifiInfo = null )? "NULL": mWifiInfo. toString () ;}// Add a network and connect to the public void addNetWork (WifiConfiguration configuration) {int wcgId = mWifiManager. addNetwork (configuration); mWifiManager. enableNetwork (wcgId, true);} // disconnect the Network public void disConnectionWifi (int netId) {mWifiManager. disableNetwork (netId); mWifiManager. disconnect ();}}

 

 

Finally, let's look at the following WifiActivity class:

 

 

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. textView; import android. widget. toast; public class WifiActivity extends Activity {/** Called when the activity is first created. */private TextView allNetWork; priv Ate 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 sb = new StringBuffer (); @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mWifiAdmin = new WifiAdmin (WifiActivity. this ); Init ();} public void init () {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); scan. setOnClickListener (new MyListener (); start. setOnClickListener (new MyListener (); stop. setOnClickListener (new MyListener (); check. setOnCli CkListener (new MyListener ();} private class MyListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {case R. id. scan: // scan the network getAllNetWorkList (); break; case R. id. start: // enable Wifi mWifiAdmin. openWifi (); Toast. makeText (WifiActivity. this, "Current wifi status:" + mWifiAdmin. checkState (), 1 ). show (); break; case R. id. stop: // disable Wifi mWifiAd Min. closeWifi (); Toast. makeText (WifiActivity. this, "Current wifi status:" + mWifiAdmin. checkState (), 1 ). show (); break; case R. id. check: // Wifi status Toast. makeText (WifiActivity. this, "Current wifi status:" + mWifiAdmin. checkState (), 1 ). show (); break; default: break; }}} public void getAllNetWorkList () {// clear the last scan result if (sb! = Null) {sb = new StringBuffer () ;}// start scanning the network mWifiAdmin. startScan (); list = mWifiAdmin. getWifiList (); if (list! = Null) {for (int I = 0; I <list. size (); I ++) {// The scan result is mScanResult = list. get (I); sb = sb. append (mScanResult. BSSID + ""). append (mScanResult. SSID + ""). append (mScanResult. capabilities + ""). append (mScanResult. frequency + ""). append (mScanResult. level + "\ n");} allNetWork. setText ("scanned Wi-Fi network: \ n" + sb. toString ());}}}

 


Do not forget to add permissions:

 

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

 

 

Finally, let's see the following demo:

 

Click to open wifi:

If the status is 3, the Wi-Fi network card is available (3 ). Click to scan the network:

These are the scanned Wi-Fi. For detailed parameters, see the above description ..


WIFI control developed by Android

WifiConfigurations is empty. GetConfiguredNetworks () Description

Upon failure to fetch or when Wi-Fi is turned off, it can be null.
If retrieval fails or wifi is disabled, null is returned.

Permission for Android development to be added using wifi

ACCESS_WIFI_STATE permission can be used to obtain WLAN wireless networks such as Wi-Fi. Add android. permission. ACCESS_WIFI_STATE.

CHANGE_WIFI_STATE is the function for changing the WLAN status. If you enable or disable Wi-Fi, you must add the android. permission. CHANGE_WIFI_STATE statement.

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.