Android WifiManager and androidwifimanager
Mobile devices are inseparable from the network. android provides some wifi functions dedicated to device management under android.net. Wifi. This package mainly has the following categories:
1. ScanResult: it is mainly used to describe information about nearby Wi-Fi hotspots obtained through Wifi hardware scanning. The fields and methods include:
2. WifiConfiguration: This class describes the configuration information of the Wi-Fi network, including the security configuration information. This class provides the following information, mainly used to describe the encryption method of Wifi.
3. WifiInfo: used to describe the connected Wi-Fi information. You can obtain the relevant information through the following methods:
GetBSSID (): Get BSSID, in the form of XX: XX
GetDetailedStateOf (SupplicantState suppState): gets the status of the network link.
GetHiddenSSID (): whether the SSID is hidden.
GetIpAddress (): Get the IP address
GetLinkSpeed (): Get link speed
GetNetworkId (): Get the network ID
Getarg (): Obtain the strength.
GetSSID (): Get the SSID Information
GetSupplicantState (): obtains the client status information.
4. WifiManager: this class is mainly used to manage Wifi and contains many methods. For example, you can view the following cases for specific use.
The following example shows how to use the WifiManager. The result of the example is as follows:
The Activity code is as follows:
// Obtain WifiManagerwifiManager = (WifiManager) getSystemService (Context. WIFI_SERVICE);/*** scan Network * @ param v */public void netScan (View v) {// start wifiManager. startScan (); // obtain the scan result List <ScanResult> mData = wifiManager. getScanResults (); ScanReslutAdapter adapter = new ScanReslutAdapter (mData); listView. setAdapter (adapter);}/*** open the Network * @ param v */public void openNet (View v) {if (! WifiManager. isWifiEnabled () {wifiManager. setWifiEnabled (true) ;}}/*** disable the Network * @ param v */public void closeNet (View v) {if (wifiManager. isWifiEnabled () {wifiManager. setWifiEnabled (false) ;}}/*** get network status * @ param v */public void getNetState (View v) {int state = wifiManager. getWifiState (); String stateStr = ""; switch (state) {case WifiManager. WIFI_STATE_DISABLED: stateStr = "DISABLED"; break; case WifiManager. WIFI_STATE_DISABLING: stateStr = "DISABLING"; break; case WifiManager. WIFI_STATE_ENABLED: stateStr = "ENABLED"; break; case WifiManager. WIFI_STATE_ENABLING: stateStr = "ENABLING"; break;} netState. setText ("network status:" + stateStr);}/*** monitor Wi-Fi signal strength * @ param v */public void monitorSignal (View v) {// obtain the link information of the current Wifi. WifiInfo wifiInfo = wifiManager. getConnectionInfo (); // obtain the signal strength int level = wifiInfo. getarg (); // obtain the signal strength value level = wifiInfo. getarg (); // send the message String levelStr = "no signal" based on the obtained signal strength; if (level <= 0 & level> =-50) {levelStr = "best signal";} else if (level <-50 & level >=- 70) {levelStr = "Good signal ";} else if (level <-70 & level >=- 80) {levelStr = "Signal Average";} else if (level <-80 & level >=- 100) {levelStr = "signal difference";} netInfo. setText ("signal strength:" + levelStr );}
Note: The following permissions are required to access 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>
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation: