赞
踩
android包中自带有WifiManager工具类,专门用于wifi管理:
1
|
import
android.net.wifi.WifiManager;
|
1
|
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
1
2
3
|
if
(!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(
true
);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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(SSID);
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;
}
|
这里只介绍第三个参数:Type。从代码中可以看出,Type有三个值,分别为1,2,3。WIFI热点是有加密的,加密方式包括:不加密,WEP,WPA三种,1、2、3就分别对应这三种加密方式,方法返回一个WIFI热点信息。
1
2
3
4
5
6
|
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);
}
|
具体的使用方式如下:
1
|
mWifiAdmin.addNetwork(mWifiAdmin.CreateWifiInfo(SSID, password,
3
));
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
int
isWifiContected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
Log.v(TAG,
"isConnectedOrConnecting = "
+ wifiNetworkInfo.isConnectedOrConnecting());
Log.d(TAG,
"wifiNetworkInfo.getDetailedState() = "
+ wifiNetworkInfo.getDetailedState());
if
(wifiNetworkInfo.getDetailedState() == DetailedState.OBTAINING_IPADDR
|| wifiNetworkInfo.getDetailedState() == DetailedState.CONNECTING) {
return
WIFI_CONNECTING;
}
else
if
(wifiNetworkInfo.getDetailedState() == DetailedState.CONNECTED) {
return
WIFI_CONNECTED;
}
else
{
Log.d(TAG,
"getDetailedState() == "
+ wifiNetworkInfo.getDetailedState());
return
WIFI_CONNECT_FAILED;
}
}
|
1
2
3
|
if
(mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(
false
);
}
|
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。