From the user's point of view, Android Wi-Fi module from bottom up can be viewed as Layer 5: Hardware driver, Wpa_suppplicant,jni,wifi api,wifisettings application.
1.wpa_supplicant is an open source library, which is the basis for the Android to implement Wi-Fi functionality, and it communicates with the hardware driver through the socket after receiving commands from the upper layer, operating the hardware to complete the required operations.
2.JNI (Java Native Interface) enables the interaction of Java code with other code, allowing Java code running in a Java virtual machine to interact with applications and libraries written in other languages, in Android, JNI allows Java programs to invoke C programs.
The 3.wi-fi API enables applications to use Wi-Fi functionality.
The 4.wi-fi Settings app is an app that comes with Android, choosing the phone's settings->wireless & Networks->wi-fi, allowing users to manually turn Wi-Fi on or off. When the user turns on the Wi-Fi feature, it automatically searches around the wireless network and displays it as a list for the user to select. The default is to connect to the wireless network for the user's last successful connection.
Some class management device Wi-Fi features are available in the Android.net.wifi package. Mainly includes Scanresult,wificonfiguration,wifiinfo,wifimanager.
1.ScanResult
Scanresult is primarily a scan of Wi-Fi hardware to get information about some of the surrounding Wi-Fi hotspots. The class consists of the following five domains:
return type |
Domain name |
Explain |
Public String |
BSSID |
Access point Address |
Public String |
Ssid |
Name of the network |
Public String |
Capabilities |
Network performance, including authentication of intervention point support, Key management, encryption mechanism, etc. |
public int |
Frequency |
Frequency of access in MHz |
public int |
Level |
Signal strength in dbm |
The class also provides a method, ToString (), that converts the result to a concise, easy-to-read string form.
2.wifiConfiguration class
This class allows you to obtain a network configuration for a Wi-Fi network, including security configuration. The class consists of six subclasses, as follows:
Sub-class |
Explain |
Wificonfiguration.authalgorthm |
Get the encryption method for IEEE802.11 |
Wificonfiguration.groupcipher |
Get Group Key |
Wificonfiguration.keymgmt |
Get password management mechanism |
Wificonfiguration.pairwisecipher |
Get a paired key for WPA mode |
Wificonfiguration.protocol |
Get a secret agreement |
Wificonfiguration.status |
Get Current network status |
Each subclass gives the relevant configuration information in constant form, and some include fields and methods.
3.WifiInfo class
This class enables you to obtain status information for a Wi-Fi network that is already established or is active. This class provides many methods, some of which are commonly used as follows:
Method |
Explain |
Getbssis () |
Get the Bssid of the current access point |
Getipaddress () |
Get IP Address |
Getlinkspeed () |
Gets the speed of the current connection |
Getmacaddress () |
Get MAC Address |
Getrssi () |
Get signal strength indication for 802.11n networks |
Getssid () |
Get Network SSID |
Getsupplicanstate () |
Returns information about the status of the client |
4.wifiManager class
This class is used to manage Wi-Fi connections, which defines 26 constants and 23 methods. Common methods are listed below:
Method |
Explain |
Addnetwork (wificonfiguration config) |
Add a new network to a set up network |
Calculatesignallevel (int rssi,int numlevels) |
Calculate the level of the signal |
Comparesignallevel (int rssia,int rssib) |
Compare the strength of two signals |
Createwifilock (int locktype,string tag) |
Create a Wi-Fi lock to lock the current Wi-Fi connection |
disablenetwork (int netId) |
Make a network connection fail |
Disconnect () |
Disconnect from current access point |
enablenetwork (int netid,boolean disableothers) |
Establish a connection to a previously provisioned network |
Getconfigurednetworks () |
Client Gets network connection status |
Getconnectioninfo () |
Get Current connection Information |
Getdhcpinfo () |
Get DHCP information |
Getscanresult () |
Get scan test Results |
Getwifistate () |
Gets whether a Wi-Fi access point is valid |
Iswifienabled () |
Determine if a Wi-Fi connection is valid |
Pingsupplicant () |
See if the client daemon responds to requests |
Ressociate () |
Reconnect to the access point, even if it is already connected |
Reconnect () |
If there is no connection at this time, re-connect |
removenetwork (int netId) |
Move out of a particular network |
Saveconfiguration () |
Keep a configuration message |
Setwifienabled (Boolean enabled) |
Make a connection valid or invalid |
Startscan () |
Start scanning |
Updatenetwork (wificonfiguration config) |
Update a network connection information |
It should be noted that the status of the Wi-Fi network card is represented by a series of integer constants.
The Wifi_state_disabled:wi-fi NIC is not available and is represented by an integer constant of 1.
The WIFI_STATE_DISABLING:WI-FI network adapter is shutting down and is represented by an integer constant of 0.
The Wifi_state_enabled:wi-fi network card is available, expressed as Integer constant 3.
The WIFI_STATE_ENABLING:WI-FI network card is open, it takes a while to start, and is represented by the integer constant 2.
Wifi_state_unknown: Unknown NIC status, denoted by integer constant 4.
In addition, Wifimanager also provides a subclass of the role of Wifimanagerlock.wifimanagerlock: In a normal state, if the state of Wi-Fi is idle, then the network will be temporarily interrupted, but if the current network status is locked, then Wi-Fi connectivity will be guaranteed Hold in a certain state, after the end of the lock, will return to normal.
Here's a brief description of how some methods are called
Private Wifiinfo Mwifiinfo;
Private Wifimanager Mwifimanager;
1. Get Wifimanager System Services
Mwifimanager= (Wifimanager) Context.getsystemservice (Context.wifi_service);
2. Regain connection information for current Wi-Fi
public void Againgetwifiinfo () {
Mwifiinfo=mwifimanager.getconnectioninfo ();
}
3. Determine if the user is turning on the Wi-Fi network
public Boolean isnetcardfriendly () {
return mwifimanager.iswifienabled ();
}
4. Determine whether Wi-Fi is currently connected
Private state State;
To create a connection manager
Private Connectivitymanager Connmanager;
public Boolean isconnectioning () {
State=connmanager.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();
if (state.connecting==state) {
return true;
}else{
return false;
}
}
5. Get the current network connection status
Public State getcurrentstate () {
State=connmanager.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();
return state;
}
6. Set the configured network (network with password and configure password), specify the
List of network connections
Private list<wificonfiguration> wificonfiglist=new arraylist<wificonfiguration> ();
Wi-Fi Configuration list
Private list<wificonfiguration> wificonfigedspecifiedlist=new arraylist<wificonfiguration> ();
public void Setwificonfigedspecifiedlist (String SSID) {
Wificonfigedspecifiedlist.clear ();
if (wificonfiglist!=null) {
for (Wificonfiguration item:wificonfiglist) {
If it is a specified network, join the list
if (item. Ssid.equalsignorecase ("\" "+ssid+" \ "") && Item.presharedkey!=null) {
Add to List
Wificonfigedspecifiedlist.add (item);
}
}
}
}
7. Return to the Wi-Fi settings list
Public list<wificonfiguration> getwificonfigedspecifiedlist () {
return wificonfigedspecifiedlist;
}
8. Turn on the Wi-Fi card
public void Opennetcard () {
if (!mwifimanager.iswifienabled ()) {
Mwifimanager.setwifienabled (TRUE);
}
}
9. Turn off the Wi-Fi connection
public void Closenetcard () {
if (mwifimanager.iswifienabled ()) {
Mwifimanager.setwifienabled (FALSE);
}
}
10. Check the current Wi-Fi card status
public void Checknetcardstate () {
if (Mwifimanager.getwifistate () ==0) {
LOG.I ("", "Nic is shutting down");
}else if (mwifimanager.getwifistate () ==1) {
LOG.I ("", "Nic has been turned off");
}else if (mwifimanager.getwifistate () ==2) {
LOG.I ("", "network card is open");
}else if (mwifimanager.getwifistate () ==3) {
LOG.I ("", "Nic has been turned on");
}else{
LOG.I ("", "not acquired to State");
}
}
11. Scan the perimeter network
Save a list of scan results
Public list<scanresult> listresult=new arraylist<scanresult> ();
public void Scan () {
Start scanning
Mwifimanager.startscan ();
Get scan Results
Listresult=mwifimanager.getscanresults ();
Scan Configuration List
Wificonfiglist=mwifimanager.getconfigurednetworks ();
if (listresult!=null) {
There is currently a wireless network
}else{
No wireless network in the current region
}
}
12. Return scan Results
Public list<scanresult> Getlistresult () {
return listresult;
}
13. Get scan Results
Store scan Results
Private StringBuffer mstringbuffer=new StringBuffer ();
Private Scanresult Mscanresult;
Public String Getscanresult () {
Clear the previous scan results before each click of the scan
if (mstringbuffer!=null) {
Mstringbuffer=new StringBuffer ();
}
method to begin scanning call 11
Scan ();
Listresult=mwifimanager.getscanresults ();
if (listresult!=null) {
for (int i=0;i<listresult.size (); i++) {
Mscanresult=listresult.get (i);
Connect the required properties to the string
Mstringbuffer.append ("NO."). Append (i+1). Append (":"). Append (Mscanresult.ssid). Append (". Append"). Mscanresult.bssid ("-"). Append (mscanresult.capabilities). Append (","). Append (mscanresult.frequency). Append ("-"). Append ( Mscanresult.level). Append ("-"). Append (Mscanresult.describecontents ()). Append ("\ n");
}
}
return mstringbuffer.tostring ();
}
14. Disconnect the currently connected network
public void Disconnectwifi () {
Get network ID
int Newid=getnetworkid ();
Setting up a network is not available
Mwifimanager.disablenetwork (NETID);
Disconnect Network
Mwifimanager.disconnect ();
Set Wi-Fi information to NULL
Mwifiinfo=null;
}
15. Check the current network status
Public Boolean checknetworkstate () {
if (mwifiinfo!=null) {
return true;
}else{
return false;
}
}
16. Get the ID of the connection
public int Getnetworkid () {
Return (mwifiinfo==null)? 0:mwifiinfo.getnetworkid ();
}
17. Get the IP address
public void getipaddress () {
Return (mwifiinfo==null)? 0:mwifiinfo.getipaddress ();
}
18. Lock Wifilock
Winfilock Mwifilock;
public void Acquirewifilock () {
Mwifilock.acquire ();
}
19. Unlocking the Wifilock
public void Releasewifilock () {
if (Mwifilock.isheld ()) {
Mwifilock.acquire ();
}
}
20. Create a Wifilock
public void Creatwifilock () {
Mwifilock=mwifimanager.createwifilock ("test");
}
21. Get the contents of the configured network wpa_supplicant.conf, regardless of the configuration password
Public list<wificonfiguration> GetConfiguration () {
return wificonfiglist;
}
22. Specify a configured network connection
Public Boolean connectconfiguration (int index) () {
is more than the configured network return
if (Index>=wificonfiglist.size ()) {
return false;
}
Return Mwifimanager.enablenetwork (Wificonfigedspecifiedlist.get (index). networkid,true);
}
23. Get Max Address
Public String getmacaddress () {
Return (Mwifiinfo=null)? ": Mwifiinfo.getmacaddress ();
}
24. Get access Point Bssid
Public String Getbssid () {
Return (mwifiinfo=null)? " NULL ": Mwifiinfo.getbssid ();
}
25. Get all the Wifiinfo information packets
Public String Getwifiinfo () {
Return (mwifiinfo=null)? " NULL ": mwifiinfo.tostring ();
}
26. Add a network and connect
public int addnetwork (Wificonfiguration g) {
Add Network
int Wcgid=mwifimanager.addnetwork (g);
Set up the added network available
Mwifimanager.enablenetwork (wcgid,true);
return wcgid;
}
Android Wi-Fi basic operation