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 {// defines the WifiManager object private WifiManager mWifiManager; // defines the WifiInfo object private WifiInfo mWifiInfo; // The scanned network connection List is private List <ScanResu Lt> mWifiList; // network connection List private List <WifiConfiguration> mWifiConfiguration; // defines a WifiLock mWifiLock; // The constructor public WifiAdmin (Context 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 if (mWifiLock when judging. I SHeld () {mWifiLock. acquire () ;}}// create a WifiLock public void creatWifiLock () {mWifiLock = mWifiManager. createWifiLock ("Test");} // The configured public List <WifiConfiguration> getConfiguration () {return mWifiConfiguration ;} // specify the configured network to connect to the public void connectConfiguration (int index) {// if (index> mWifiConfiguration is returned if the index is greater than the configured network index. size () {return;} // connect the network mWifiManager with the specified ID. 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 public List of networks <ScanResult> getWifiList () {return mWifiList;} // view the scan result public StringBuilder lookUpScan () {StringBuilder stringBuilder = new StringBuilder (); fo R (int I = 0; I <mWifiList. size (); I ++) {stringBuilder. append ("Index _" + new Integer (I + 1 ). toString () + ":"); // converts ScanResult information into a string package. // This includes 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 Access Point's BSSID 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 () ;}// get all information packages of WifiInfo public String getWifiInfo () {return (mWifiInfo = null )? "NULL": mWifiInfo. toString () ;}// Add a network and connect to the 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 public void disconnectWifi (int netId) {mWifiManager. disableNetwork (netId); mWifiManager. disconnect () ;}// then it is a practical application method that only verifies the password-free situation. Condition: 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 (S SID); 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 ):

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 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:

<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.