通過程序來實現(xiàn)wifi的自動連接。
這兩天對android的wifi功能研究了一下。下面共享出自己封裝的WifiConnect類。(這里參考了ZXing開源項目中wifi模塊)
首先,要了解android關(guān)于wifi的API.
如果你喜歡看English API,這是android關(guān)于wifi的API地址:
http://developer./reference/android/net/wifi/package-summary.html(配置Wifi,還需要設(shè)定權(quán)限,android中關(guān)于wifi的API文檔中說明了要添加什么權(quán)限)
如果你一看英語就頭疼,這里有關(guān)于wifi的api的中文解釋
http://note.sdo.com/u/1500295617/n/prb71~jGAHfpnM0l000bgN
切入正題,這里貼出一個封裝了的類(WifiConnect),在實例化WifiConnect對象時,需要傳入一個WifiManager對象。WifiConnect類向外部提供一個Connect方法,參數(shù)是無線的SSID,password,還有加密類型。
-
-
-
-
- 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;
-
-
- public enum WifiCipherType
- {
- WIFICIPHER_WEP,WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID
- }
-
-
- public WifiConnect(WifiManager wifiManager)
- {
- this.wifiManager = wifiManager;
- }
-
-
- 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;
- }
-
-
- while(wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING )
- {
- try{
-
- 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;
- }
-
- }