public class WifiConnectUtil函數
{測試
private WifiManager wifiManager;this
// 定義幾種加密方式,一種是WEP,一種是WPA,還有沒有密碼的狀況加密
public enum WifiCipherTypespa
{.net
WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID接口
}ip
// 構造函數rem
public WifiConnectUtil(WifiManager wifiManager)get
{
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 (!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)
{
ie.printStackTrace();
}
}
WifiConfiguration wifiConfig = CreateWifiInfo(SSID, Password, Type);
//
if (wifiConfig == null)
{
return false;
}
WifiConfiguration tempConfig = 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;
}