Android development-get wifi list
Recently, bloggers are learning the frangment framework, so they want to get the wifi list through listfragment.
Good! Now I will not talk nonsense.
1. Basic knowledge of wifi
The official Android documentation defines the following five states:
WIFI_STATE_DISABLING WIFI Nic is shutting down 0
WIFI_STATE_DISABLED WIFI Nic unavailable 1
WIFI_STATE_ENABLING WIFI network card is on 2
WIFI_STATE_ENABLED WIFI Nic available 3
WIFI_STATE_UNKNOWN WIFI Nic status unknown 4
To obtain Wi-Fi information, you need a wifimanager object.
The wifimanager object must useContext. GetSystemService (Context. WIFI_SERVICE) to obtain the WifiManager object.
Here, we need to know that activities inherit fromContextSo you can use MainActivity. this. getSystemService (Context. WIFI_SERVICE)
Get object. If you want to get a list of nearby Wi-Fi hotspots, you can use WifiManager. getScanResults () to return a ScanResult list.
The following are the basic Wi-Fi knowledge reposted from other bloggers:
(1) ScanResult
It mainly uses wifi hardware scanning to obtain information about nearby Wi-Fi hotspots. When we perform a Wi-Fi search, we usually find this information. The first is the Access Point name, the strength of the Access Point information, and the security mode used by the access point, which is WPA and WPE. Open this class, we can see the following information about the address of the BSSID access point, which mainly refers to the address obtained by connecting several wireless devices in a small range. For example, two laptops are connected through a wireless network card, addresses allocated by both wireless network adapters.
The name of the SSID network. When we search for a network, we rely on this to distinguish different network access points.
Capabilities network access performance, mainly used to determine the network encryption mode.
Frequency, the number of MHz of interaction between each channel.
Level, mainly used to determine the priority of network connections.
Here, only one method is provided, that is, to obtain the information programming string toString ().
(2) wifiConfiguration
Some information we need to obtain when connecting to a Wi-Fi access point. You can compare it with our wired devices.
The data here is relatively complicated.
Six subclasses
WifiConfiguration. AuthAlgorthm is used to determine the encryption method.
WifiConfiguration. GroupCipher obtains the GroupCipher Method for encryption.
WifiConfiguration. KeyMgmt is used to obtain and use KeyMgmt.
WifiConfiguration. PairwiseCipher obtains the WPA-based encryption.
WifiConfiguration. Protocol gets the Protocol used for encryption.
WifiConfiguration. Status to obtain the Status of the current network.
If you are interested in the preceding encryption, you can search for related content on the Internet.
Set the properties of WifiConfiguration:
WifiConfiguration. AuthAlgorthm sets the encryption method.
Optional parameter: LEAP is only used for leap,
OPEN is required by wpa/wpa2,
SHARED requires a static wep key.
WifiConfiguration. GroupCipher uses the GroupCipher Method for encryption.
Optional parameters: CCMP, TKIP, WEP104, and WEP40
WifiConfiguration. KeyMgmt Key Management Mechanism (keymanagerment), using KeyMgmt.
Optional parameters: IEEE8021X, NONE, WPA_EAP, and WPA_PSK
WifiConfiguration. PairwiseCipher sets the encryption mode.
Optional CCMP, NONE, TKIP
WifiConfiguration. Protocol sets a Protocol for encryption.
Optional parameters: RSN, WPA,
WifiConfiguration. Status sets the Status of the current network.
Optional parameters: CURRENT, DISABLED, and ENABLED
(3) WifiInfo
After our wi-fi connection, you can obtain some connected wi-fi connection information through this class to obtain the information of the current link,
The information here is relatively simple. 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.
It is relatively complicated here, and there are a lot of content in it, but we can still get a lot of relevant information through the literal meaning. Many constants are pre-defined in this class, which can be used directly without being created again.
Here is a brief introduction to the methods here:
AddNetwork (WifiConfiguration config) adds a network by obtaining the link status information of the network.
CalculateSignalLevel (int Arg, int numLevels) Calculate the signal level
CompareSignalLevel (int rssiA, int rssiB)
CreateWifiLock (int lockType, String tag) creates a wifi lock to lock the current Wi-Fi connection.
DisableNetwork (int netId) invalidates a network connection
Disconnect () disconnection
EnableNetwork (int netId, Boolean disableOthers) connects to a connection
GetConfiguredNetworks () obtains the network connection status.
GetConnectionInfo () obtains information about the current connection.
GetDhcpInfo () gets DHCP Information
GetScanResulats () obtains scan test results
GetWifiState () to obtain whether a Wi-Fi access point is valid
IsWifiEnabled () determines whether a Wi-Fi connection is valid
PingSupplicant () ping a connection to determine whether it can be connected
Ressociate () connects even if the connection is not ready.
Reconnect () if the connection is ready, connect
RemoveNetwork () removes a network
SaveConfiguration () retains a configuration
SetWifiEnabled () makes a connection valid
StartScan () starts Scanning
UpdateNetwork (WifiConfiguration config) updates the information of a network connection.
In addition, wifiManaer also provides an internal subclass, that is, wifiManagerLock and WifiManagerLock,
In normal conditions, if our wi-fi status is idle, the network connection will be temporarily interrupted.
However, if we lock the current network status, the Wi-Fi connection will remain in a certain state. Of course, after the contact is locked, the connection will return to normal.
Okay. I will paste the code below.
Mainfest code Diagram
Mainactivity
package com.example.wifilisttest;import java.util.List;import android.app.Activity;import android.content.Context;import android.net.wifi.ScanResult;import android.net.wifi.WifiManager;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.widget.Toast;public class MainActivity extends FragmentActivity {private WifiManager wifiManager; List
list; String[] Flist=null; Button but;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Fragment1 f1 = new Fragment1(); FragmentManager manager = getSupportFragmentManager(); manager.beginTransaction().add(R.id.fragments, f1).commit(); }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
Fragment1
Package com. example. wifilisttest; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. content. context; import android.net. wifi. scanResult; import android.net. wifi. wifiManager; import android. OS. bundle; import android. support. v4.app. listFragment; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. listView; import android. widget. simpleAdapter; import android. widget. textView; import android. widget. toast;/*** @ Description: To use ListView in Fragment, you must use ListFragment **/public class Fragment1 extends ListFragment {private String TAG = Fragment1.class. getName (); private ListView list; private SimpleAdapter adapter; List
Llll = null; String [] Flist; private TextView TV; private WifiManager wifiManager; List
Listb;/*** @ description loading layout in onCreateView **/@ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view View = inflater. inflate (R. layout. fragment1, container, false); list = (ListView) view. findViewById (android. r. id. list); TV = (TextView) view. findViewById (R. id. TV); TV. setText ("nullTitle"); Log. I (TAG, "-------- onCreateView"); return view ;}@ Override p Ublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Bundle B = savedInstanceState; Log. I (TAG, "-------- onCreate"); wifiManager = (WifiManager) getActivity (). getSystemService (Context. WIFI_SERVICE); listb = wifiManager. getScanResults (); // array initialization Note: String [] listk = new String [listb. size ()]; if (listb! = Null) {for (int I = 0; I
View = (HashMap
) L. getItemAtPosition (position); System. out. println (view. get ("title "). toString () + "++ title"); Toast. makeText (getActivity (), TAG + l. getItemIdAtPosition (position), Toast. LENGTH_LONG ). show (); System. out. println (v); System. out. println (position);} private List> getData (String [] strs) {List
> List = new ArrayList
> (); For (int I = 0; I <strs. length; I ++) {Map
Map = new HashMap
(); Map. put ("title", strs [I]); list. add (map) ;}return list ;}@ Override public void onActivityCreated (Bundle savedInstanceState) {super. onActivityCreated (savedInstanceState); Log. I (TAG, "-------- onActivityCreated") ;}@ Override public void onAttach (Activity activity) {super. onAttach (activity); Log. I (TAG, "---------- onAttach ");}}
Activity_main.xml
<framelayout android:id="@+id/fragments" android:layout_height="wrap_content" android:layout_width="wrap_content"></framelayout>
Fragment1.xml
Listtest. xml