在程序中连接WIFI热点,如果此WIFI热点已配置过,那就非常简单.教科书上都有.但如果是一个新的WIFI网络,那问题就来了.按习惯,先在网上搜了一遍,如果有现成的.那事情就算搞定了.谁知,竟然找不到完整的代码.难道没人连接成功过?
看来只能是自力更生了.看一下类库的说明http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#allowedAuthAlgorithms
1.连接WEP加密方式的WIFI
调试花了不少时间,但还是成功了.附上源码.
View Code
1 WifiManager wifi = (WifiManager) getSystemService(wifi.this.WIFI_SERVICE);
2
3 WifiConfiguration wc = new WifiConfiguration();
4
5 /* ---------------------WEP连接方式---------------------*/
6
7 wc.SSID = "\"SSID\"";
8 wc.hiddenSSID = false;
9
10 wc.status = WifiConfiguration.Status.ENABLED;
11
12 wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
13 wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
14
15 wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
16 wc.wepTxKeyIndex = 0;
17
18 wc.wepKeys[0] = "PSW";
19
20 int res = wifi.addNetwork(wc);
21
22 boolean b = wifi.enableNetwork(res, true);
23
24 /* ---------------------WEP连接方式END---------------------*/
25
26
27
2
3 WifiConfiguration wc = new WifiConfiguration();
4
5 /* ---------------------WEP连接方式---------------------*/
6
7 wc.SSID = "\"SSID\"";
8 wc.hiddenSSID = false;
9
10 wc.status = WifiConfiguration.Status.ENABLED;
11
12 wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
13 wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
14
15 wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
16 wc.wepTxKeyIndex = 0;
17
18 wc.wepKeys[0] = "PSW";
19
20 int res = wifi.addNetwork(wc);
21
22 boolean b = wifi.enableNetwork(res, true);
23
24 /* ---------------------WEP连接方式END---------------------*/
25
26
27
2.连接WPA加密方式的WIFI
View Code
1 WifiConfiguration wc = new WifiConfiguration();
2
3 /*----------------------WPA连接方式------------------------*/
4 wc.SSID = "\"SSID\"";
5 wc.hiddenSSID = false;
6
7 wc.status = WifiConfiguration.Status.ENABLED;
8
9
10 wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
11 wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
12 wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
13 wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
14
15 wc.preSharedKey = "\"PSW\"";
16
17 int res = wifi.addNetwork(wc);
18
19 boolean b = wifi.enableNetwork(res, true);
20 /*----------------------WPA连接方式END------------------------*/
2
3 /*----------------------WPA连接方式------------------------*/
4 wc.SSID = "\"SSID\"";
5 wc.hiddenSSID = false;
6
7 wc.status = WifiConfiguration.Status.ENABLED;
8
9
10 wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
11 wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
12 wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
13 wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
14
15 wc.preSharedKey = "\"PSW\"";
16
17 int res = wifi.addNetwork(wc);
18
19 boolean b = wifi.enableNetwork(res, true);
20 /*----------------------WPA连接方式END------------------------*/
还有附上我的WIFI配置截图.无线路由是TP-LINK,比较常见的一款路由器.
今天遇到一种无密码的情况,反而连接不上.奇怪.继续研究.有连接过无密码的请不惜赐教
经过一个晚上的调试.终于连接上了.无加密WIFI重点在于某几个参数设置,继续附上代码
ConnectionUnConfigForNoPsw
public boolean ConnectionUnConfigForNoPsw(String ssid)
{
try
{
WifiConfiguration wc = new WifiConfiguration();
wc.hiddenSSID = false;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
wc.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
wc.allowedProtocols.set(Protocol.WPA);
wc.allowedProtocols.set(Protocol.RSN);
wc.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
wc.allowedGroupCiphers.set(GroupCipher.WEP40);
wc.allowedGroupCiphers.set(GroupCipher.WEP104);
wc.allowedGroupCiphers.set(GroupCipher.TKIP);
wc.allowedGroupCiphers.set(GroupCipher.CCMP);
wc.SSID = "\"" + ssid + "\"";
wc.preSharedKey = null;
int res = mWifiManager.addNetwork(wc);
return mWifiManager.enableNetwork(res, true);
}
catch(Exception ex)
{
return false;
}
}
{
try
{
WifiConfiguration wc = new WifiConfiguration();
wc.hiddenSSID = false;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
wc.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
wc.allowedProtocols.set(Protocol.WPA);
wc.allowedProtocols.set(Protocol.RSN);
wc.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
wc.allowedGroupCiphers.set(GroupCipher.WEP40);
wc.allowedGroupCiphers.set(GroupCipher.WEP104);
wc.allowedGroupCiphers.set(GroupCipher.TKIP);
wc.allowedGroupCiphers.set(GroupCipher.CCMP);
wc.SSID = "\"" + ssid + "\"";
wc.preSharedKey = null;
int res = mWifiManager.addNetwork(wc);
return mWifiManager.enableNetwork(res, true);
}
catch(Exception ex)
{
return false;
}
}