經過程序來實現wifi的自動鏈接。html
這兩天對android的wifi功能研究了一下。下面共享出本身封裝的WifiConnect類。(這裏參考了ZXing開源項目中wifi模塊)java
首先,要了解android關於wifi的API.android
若是你喜歡看English API,這是android關於wifi的API地址:c++
http://developer.android.com/reference/android/net/wifi/package-summary.html(配置Wifi,還須要設定權限,android中關於wifi的API文檔中說明了要添加什麼權限)
算法
若是你一看英語就頭疼,這裏有關於wifi的api的中文解釋api
http://note.sdo.com/u/1500295617/n/prb71~jGAHfpnM0l000bgN
網絡
切入正題,這裏貼出一個封裝了的類(WifiConnect),在實例化WifiConnect對象時,須要傳入一個WifiManager對象。WifiConnect類向外部提供一個Connect方法,參數是無線的SSID,password,還有加密類型。app
- /*
- * WifiConnect.java
- * Author: cscmaker
- */
- package com.wifi.connect;
-
- import java.util.List;
-
- import android.net.wifi.WifiConfiguration;
- import android.net.wifi.WifiManager;
- import android.util.Log;
-
- public class WifiConnect {
-
- WifiManager wifiManager;
-
- //定義幾種加密方式,一種是WEP,一種是WPA,還有沒有密碼的狀況
- public enum WifiCipherType
- {
- WIFICIPHER_WEP,WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID
- }
-
- //構造函數
- public WifiConnect(WifiManager wifiManager)
- {
- this.wifiManager = wifiManager;
- }
-
- //打開wifi功能
- private boolean OpenWifi()
- {
- boolean bRet = true;
- if (!wifiManager.isWifiEnabled())
- {
- bRet = wifiManager.setWifiEnabled(true);
- }
- return bRet;
- }
-
- //提供一個外部接口,傳入要鏈接的無線網
- public boolean Connect(String SSID, String Password, WifiCipherType Type)
- {
- if(!this.OpenWifi())
- {
- return false;
- }
- //開啓wifi功能須要一段時間(我在手機上測試通常須要1-3秒左右),因此要等到wifi
- //狀態變成WIFI_STATE_ENABLED的時候才能執行下面的語句
- while(wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING )
- {
- try{
- //爲了不程序一直while循環,讓它睡個100毫秒在檢測……
- Thread.currentThread();
- Thread.sleep(100);
- }
- catch(InterruptedException ie){
- }
- }
-
- WifiConfiguration wifiConfig = this.CreateWifiInfo(SSID, Password, Type);
- //
- if(wifiConfig == null)
- {
- return false;
- }
-
- WifiConfiguration tempConfig = this.IsExsits(SSID);
-
- if(tempConfig != null)
- {
- wifiManager.removeNetwork(tempConfig.networkId);
- }
-
- int netID = wifiManager.addNetwork(wifiConfig);
- boolean bRet = wifiManager.enableNetwork(netID, false);
- return bRet;
- }
-
- //查看之前是否也配置過這個網絡
- private WifiConfiguration IsExsits(String SSID)
- {
- List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
- for (WifiConfiguration existingConfig : existingConfigs)
- {
- if (existingConfig.SSID.equals("\""+SSID+"\""))
- {
- return existingConfig;
- }
- }
- return null;
- }
-
- private WifiConfiguration CreateWifiInfo(String SSID, String Password, WifiCipherType Type)
- {
- WifiConfiguration config = new WifiConfiguration();
- config.allowedAuthAlgorithms.clear();
- config.allowedGroupCiphers.clear();
- config.allowedKeyManagement.clear();
- config.allowedPairwiseCiphers.clear();
- config.allowedProtocols.clear();
- config.SSID = "\"" + SSID + "\"";
- if(Type == WifiCipherType.WIFICIPHER_NOPASS)
- {
- config.wepKeys[0] = "";
- config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
- config.wepTxKeyIndex = 0;
- }
- if(Type == WifiCipherType.WIFICIPHER_WEP)
- {
- config.preSharedKey = "\""+Password+"\"";
- config.hiddenSSID = true;
- 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 == WifiCipherType.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.status = WifiConfiguration.Status.ENABLED;
- }
- else
- {
- return null;
- }
- return config;
- }
-
- }