Android WiFi attributes

Source: Internet
Author: User
Tags bssid

Package com. example. wifitest;

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 {
// Define the wifimanager object
Private wifimanager mwifimanager;
// Define the wifiinfo object
Private wifiinfo mwifiinfo;
// List of scanned Network Connections
Private list <scanresult> mwifilist;
// Network connection list
Private list <wificonfiguration> mwificonfiguration;
// Define a wifilock
Wifilock mwifilock;

// Constructor
Public wifiadmin (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 (){
// Lock upon judgment
If (mwifilock. isheld ()){
Mwifilock. Acquire ();
}
}

// Create a wifilock
Public void creatwifilock (){
Mwifilock = mwifimanager. createwifilock ("test ");
}

// Obtain the configured network
Public list <wificonfiguration> getconfiguration (){
Return mwificonfiguration;
}

// Specify the configured network for connection
Public void connectconfiguration (INT index ){
// Returns an index larger than the configured network index
If (index> mwificonfiguration. Size ()){
Return;
}
// Connect the network with the specified ID
Mwifimanager. enablenetwork (mwificonfiguration. Get (INDEX). networkid,
True );
}

Public void startscan (){
Mwifimanager. startscan ();
// Obtain the scan result
Mwifilist = mwifimanager. getscanresults ();
// Obtain the configured network connection
Mwificonfiguration = mwifimanager. getconfigurednetworks ();
}

// Obtain the Network List
Public list <scanresult> getwifilist (){
Return mwifilist;
}

// View scan results
Public stringbuilder lookupscan (){
Stringbuilder = new stringbuilder ();
For (INT I = 0; I <mwifilist. Size (); I ++ ){
Stringbuilder
. Append ("index _" + new INTEGER (I + 1). tostring () + ":");
// Convert scanresult information into a string package
// Include bssid, SSID, capabilities, frequency, and level
Stringbuilder. append (mwifilist. Get (I). tostring ());
Stringbuilder. append ("/N ");
}
Return stringbuilder;
}

// Obtain the MAC address
Public String getmacaddress (){
Return (mwifiinfo = NULL )? "Null": mwifiinfo. getmacaddress ();
}

// Obtain the bssid of the Access Point
Public String getbssid (){
Return (mwifiinfo = NULL )? "Null": mwifiinfo. getbssid ();
}

// Obtain the IP address
Public int getipaddress (){
Return (mwifiinfo = NULL )? 0: mwifiinfo. getipaddress ();
}

// Obtain the connection ID
Public int getnetworkid (){
Return (mwifiinfo = NULL )? 0: mwifiinfo. getnetworkid ();
}

// Obtain all information packages of wifiinfo
Public String getwifiinfo (){
Return (mwifiinfo = NULL )? "Null": mwifiinfo. tostring ();
}

// Add a network and connect
Public void addnetwork (wificonfiguration WCG ){
Int wcgid = mwifimanager. addnetwork (WCG );
Boolean B = mwifimanager. enablenetwork (wcgid, true );
System. Out. println ("A --" + wcgid );
System. Out. println ("B --" + B );
}

// Disconnect the network with the specified ID
Public void disconnectwifi (INT netid ){
Mwifimanager. disablenetwork (netid );
Mwifimanager. Disconnect ();
}

// Then there is a practical application method, which only verifies that there is no password:

Public wificonfiguration createwifiinfo (string SSID, string password,
Int type ){
Wificonfiguration Config = new wificonfiguration ();
Config. allowedauthalgorithms. Clear ();
Config. allowedgroupciphers. Clear ();
Config. allowedkeymanagement. Clear ();
Config. allowedpairwiseciphers. Clear ();
Config. allowedprotocols. Clear ();
Config. SSID = "\" "+ SSID + "\"";

Wificonfiguration tempconfig = This. isexsits (SSID );
If (tempconfig! = NULL ){
Mwifimanager. removenetwork (tempconfig. networkid );
}

If (type = 1) // wificipher_nopass
{
Config. wepkeys [0] = "";
Config. allowedkeymanagement. Set (wificonfiguration. keymgmt. None );
Config. weptxkeyindex = 0;
}
If (type = 2) // wificipher_wep
{
Config. hiddenssid = true;
Config. wepkeys [0] = "\" "+ password + "\"";
Config. allowedauthalgorithms
. Set (wificonfiguration. authalgorithm. Shared );
Config. allowedgroupciphers. Set (wificonfiguration. groupcipher. CCMP );
Config. allowedgroupciphers. Set (wificonfiguration. groupcipher. TKIP );
Config. allowedgroupciphers. Set (wificonfiguration. groupcipher. wep40 );
Config. allowedgroupciphers
. Set (wificonfiguration. groupcipher. wep104 );
Config. allowedkeymanagement. Set (wificonfiguration. keymgmt. None );
Config. weptxkeyindex = 0;
}
If (type = 3) // wificipher_wpa
{
Config. presharedkey = "\" "+ password + "\"";
Config. hiddenssid = true;
Config. allowedauthalgorithms
. Set (wificonfiguration. authalgorithm. Open );
Config. allowedgroupciphers. Set (wificonfiguration. groupcipher. TKIP );
Config. allowedkeymanagement. Set (wificonfiguration. keymgmt. wpa_psk );
Config. allowedpairwiseciphers
. Set (wificonfiguration. pairwisecipher. TKIP );
// Config. allowedprotocols. Set (wificonfiguration. Protocol. WPA );
Config. allowedgroupciphers. Set (wificonfiguration. groupcipher. CCMP );
Config. allowedpairwiseciphers
. Set (wificonfiguration. pairwisecipher. CCMP );
Config. Status = wificonfiguration. Status. enabled;
}
Return config;
}

Private wificonfiguration isexsits (string SSID ){
List <wificonfiguration> existingconfigs = mwifimanager
. Getconfigurednetworks ();
For (wificonfiguration existingconfig: existingconfigs ){
If (existingconfig. SSID. Equals ("\" "+ SSID + "\"")){
Return existingconfig;
}
}
Return NULL;
}

}

}
// There are three scenarios: 1 No Password 2 WEP encryption 3 WPA Encryption

The changes are mainly concentrated in the createwifiinfo method, and a private method is added:

(1) Change the variables related to the third parameter of the method to the int type, or use the original enumerated type (bugs need to be corrected ), however, enumeration may encounter some difficulties in subsequent development;

(2) Comment out "config. allowedprotocols. Set (wificonfiguration. Protocol. WPA);" in if (type = 3) and add"

Config. allowedgroupciphers. set (wificonfiguration. groupcipher. CCMP); config. allowedpairwiseciphers. set (wificonfiguration. pairwisecipher. CCMP); "these two statements, otherwise, you cannot join the network when a password is required for a Wi-Fi hotspot.

(3) The method isexsits is added at the end of the code because if the specified WiFi is successfully added according to the method described on the Internet, A network with the specified SSID will be added to the WiFi list of the terminal. Therefore, each time a program is run, an SSID with the same name will be added to the list. This method is to check whether there are any WIFI hotspots in the WiFi list named as input parameters. If yes, remove the WiFi hotspot before the createwifiinfo method starts to configure the WiFi network to avoid repeated SSID:

Wificonfiguration tempconfig = This. isexsits (SSID );

If (tempconfig! = NULL ){

Mwifimanager. removenetwork (tempconfig. networkid );

}

The above is the establishment of the WiFi tool class, and then you can instantiate this class in other parts, call the method to complete the addition of the WiFi hotspot of the specified SSID, or go to the code first, create a file named test_wifiactivity.java (same as above, it does not contain the package declaration and Import Statement ):

[Java]
Public class test_wifiactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Wifiadmin = new wifiadmin (this );
Wifiadmin. openwifi ();
Wifiadmin. addnetwork (wifiadmin. createwifiinfo ("XXX", "XXX", 3 ));
}
}

It is very simple. For access to wifi, it generally only involves openwifi (enable WiFi), createwifiinfo (configure WiFi network information), and addnetwork (ADD and connect the configured network ), brief description of createwifiinfo: the first parameter is the name of the SSID, and the second parameter is the password of the specified SSID network. If you do not need the password, leave it blank (""); the third parameter is the hotspot type: 1-password-free/2-wep password verification (not tested)/3-wap or wap2 PSK password verification.
Finally, add the corresponding permissions to manifest:

[Java]
<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>

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.