?? Android SDK in Android.net.wi

Source: Internet
Author: User

A lot is listed here, but it can be broadly divided into four main classes Scanresult wificonfiguration wifiinfo Wifimanager
(1) Scanresult, mainly through the WiFi hardware scan to get some of the surrounding WiFi hotspot information.
When we do WiFi search, generally will search this information, the first is the access point name, the strength of Access point information, as well as the use of Access point security mode, is WPA, WPE.
Open this class, we can see the following information
BSSID access point address, here is mainly refers to a small range of wireless devices connected to the address obtained, for example, two laptops through the wireless network card connection, the two sides of the wireless network card assigned address.
The name of the SSID network, when we search for a network, it is this to distinguish between each of the different network access points.
Capabilities Network access performance, here is mainly to determine the network encryption method and so on.
Frequency frequency, the number of MHz each channel interacts with.
level, primarily to determine the priority of the network connection.
Only one method is provided here, which is to get the information programming string ToString ().
(2) Wificonfiguration when we connect to a WiFi access point, we need to get some information. You can compare it with our wired device.
The data here is relatively more complex.
Six sub-classes
Wificonfiguration.authalgorthm is used to determine the encryption method.
Wificonfiguration.groupcipher Gets the method that uses Groupcipher to encrypt.
Wificonfiguration.keymgmt get used with KEYMGMT.
Wificonfiguration.pairwisecipher gets the encryption using WPA mode.
Wificonfiguration.protocol gets which protocol is used for encryption.
Wificonfiguration.status gets the status of the current network.
For those who are interested in encryption, you can search for relevant content on the Internet.
(3) Wifiinfo after our WiFi has been connected, you can get some connected WiFi connection information through this class to get the information of the current link, here the information is relatively simple, here is a brief introduction to the method here:
Getbssid () Get Bssid
Getdetailedstateof () Get the connectivity of the client
Gethiddenssid () gets whether the SSID is hidden
Getipaddress () Get IP address
Getlinkspeed () Get the speed of the connection
Getmacaddress () Get MAC Address
Getrssi () Get signal from 802.11n network
Getssid () Get SSID
Getsupplicanstate () returns information about the specific client state
(4) Wifimanager This needless to say, is used to manage our WiFi connection, here has defined some classes, can be used by us. It's relatively complex here, with more content, but we can get a lot of relevant information by literal means. Many constants are predefined in this class, and we can use them directly without having to create them again.
Here is a brief introduction to the method here:
Addnetwork (wificonfiguration config) to add a network by acquiring the link state information to the network
Calculatesignallevel (int rssi, int numlevels) calculates the rank of the signal
Comparesignallevel (int rssia, int rssib) vs. connection A and connection B
Createwifilock (int lockType, String tag) creates a WiFi lock that locks the current WiFi connection
disablenetwork (int netId) to invalidate a network connection
Disconnect () disconnect
Enablenetwork (int netId, Boolean disableothers) connects a connection
Getconfigurednetworks () Get the status of a network connection
Getconnectioninfo () Get information about the current connection
Getdhcpinfo () Getting DHCP information
Getscanresulats () Gets the results of the scan test
Getwifistate () Get a WiFi access point is valid
Iswifienabled () determine if a WiFi connection is valid
Pingsupplicant () Ping a connection to determine if it can connect
Ressociate () Even if the connection is not ready, it must be connected
Reconnect () If the connection is ready, connect
Removenetwork () Removing a network
Saveconfiguration () Keep a configuration message
Setwifienabled () make a connection valid
Startscan () Start scanning
Updatenetwork (wificonfiguration config) update information for a network connection
In addition, Wifimanaer also provides an internal subclass, that is, the role of Wifimanagerlock,wifimanagerlock, in the normal state, if our WiFi status is idle, then the network connectivity, will be temporarily interrupted. However, if we lock the current network status, then WiFi connectivity will remain in a certain state, of course, after the contact lock, will return to normal.

A wifiadmin management class is attached:
Package youcan.text;

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 {
Defining Wifimanager Objects
Private Wifimanager Mwifimanager;
Defining Wifiinfo Objects
Private Wifiinfo Mwifiinfo;
List of scanned network connections
Private list<scanresult> mwifilist;
List of network connections
Private list<wificonfiguration> mwificonfiguration;
Define a Wifilock
Wifilock Mwifilock;

Constructors
Public Wifiadmin (Context context) {
Get Wifimanager Object
Mwifimanager = (Wifimanager) context
. Getsystemservice (Context.wifi_service);
Get Wifiinfo Object
Mwifiinfo = Mwifimanager.getconnectioninfo ();
}

Get Wifimanager Object
Public Wifimanager Getwifimanager () {
return mwifimanager;
}

Open wifi
public void Openwifi () {
if (!mwifimanager.iswifienabled ()) {
Mwifimanager.setwifienabled (TRUE);

}
}

Turn off WiFi
public void Closewifi () {
if (!mwifimanager.iswifienabled ()) {
Mwifimanager.setwifienabled (FALSE);
}
}

Lock Wifilock
public void Acquirewifilock () {
Mwifilock.acquire ();
}

Unlock Wifilock
public void Releasewifilock () {
Determine when to lock
if (Mwifilock.isheld ()) {
Mwifilock.acquire ();
}
}

Create a Wifilock
public void Creatwifilock () {
Mwifilock = Mwifimanager.createwifilock ("Test");
}

Get a configured network
Public list<wificonfiguration> GetConfiguration () {
return mwificonfiguration;
}

Specify a configured network to connect
public void connectconfiguration (int index) {
Index is larger than the configured network index returns
if (Index > Mwificonfiguration.size ()) {
Return
}
Connect the configured network with the specified ID
Mwifimanager.enablenetwork (Mwificonfiguration.get (index). Networkid,
true);
}

public void Startscan () {
Mwifimanager.startscan ();
Get scan Results
Mwifilist = Mwifimanager.getscanresults ();
Get a configured network connection
Mwificonfiguration = Mwifimanager.getconfigurednetworks ();
}

Get a list of networks
Public list<scanresult> getwifilist () {
return mwifilist;
}

View Scan Results
Public StringBuilder Lookupscan () {
StringBuilder StringBuilder = new StringBuilder ();
for (int i = 0; i < mwifilist.size (); i++) {
StringBuilder
. Append ("index_" + new Integer (i + 1). ToString () + ":");
Converting Scanresult information into a string package
These include: BSSID, SSID, capabilities, frequency, level
Stringbuilder.append ((Mwifilist.get (i)). ToString ());
Stringbuilder.append ("\ n");
}
return stringBuilder;
}

Get MAC Address
Public String getmacaddress () {
return (Mwifiinfo = = null)? "NULL": mwifiinfo.getmacaddress ();
}

Get the bssid of the access point
Public String Getbssid () {
return (Mwifiinfo = = null)? "NULL": Mwifiinfo.getbssid ();
}

Get IP Address
public int getipaddress () {
return (Mwifiinfo = = null)? 0:mwifiinfo.getipaddress ();
}

Get the ID of the connection
public int Getnetworkid () {
return (Mwifiinfo = = null)? 0:mwifiinfo.getnetworkid ();
}

Get all the Wifiinfo packets
Public String Getwifiinfo () {
return (Mwifiinfo = = null)? "NULL": mwifiinfo.tostring ();
}

Add a network and connect
public void Addnetwork (Wificonfiguration WCG) {
int wcgid = Mwifimanager.addnetwork (WCG);
Mwifimanager.enablenetwork (Wcgid, true);
}

Disconnect the network with the specified ID
public void Disconnectwifi (int netId) {
Mwifimanager.disablenetwork (NETID);
Mwifimanager.disconnect ();
}
}

?? Android SDK in Android.net.wi

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.