当前位置:   article > 正文

android自动连接wifi——WifiManager_wifimanager.enablenetwor

wifimanager.enablenetwor
摘要  通过WifiManager可以实现对wifi进行操作,实现wifi自动连接等一些功能。

WifiManager工具类:

android包中自带有WifiManager工具类,专门用于wifi管理:

?
1
import  android.net.wifi.WifiManager;


取得WifiManager对象:
?
1
WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);


打开WIFI:
?
1
2
3
if  (!mWifiManager.isWifiEnabled()) {
     mWifiManager.setWifiEnabled( true );
}


创建一个WIFI信息:
?
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 ));


判断WIFI是否连接成功:
?
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;
         }
     }


关闭WIFI连接:
?
1
2
3
if  (mWifiManager.isWifiEnabled()) {
     mWifiManager.setWifiEnabled( false );
}
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号