WifiManager自动连接wifi接入点

[java]   view plain copy
<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
  1. /** 
  2.  *  
  3.  * wifi管理类 
  4.  *  
  5.  *  
  6.  */  
  7. public class WifiOperator  
  8. {  
  9.     /** 
  10.      * wifiManager 
  11.      */  
  12.     private WifiManager wm;  
  13.       
  14.     /** 
  15.      * 上下文 
  16.      */  
  17.     private Context mContext;  
  18.       
  19.     /** 
  20.      * 数据库配置信息 
  21.      */  
  22.     private NetworkConfigDbHelper configDBHelper;  
  23.       
  24.     /** 
  25.      * <默认构造函数> 
  26.      * @param context 上下文 
  27.      */  
  28.     public WifiOperator(Context context)  
  29.     {  
  30.         mContext = context;  
  31.         wm = (WifiManager)mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);  
  32.         configDBHelper = new NetworkConfigDbHelper(context);  
  33.     }  
  34.       
  35.     /** 
  36.      * 切换网络 
  37.      * @param type 网络类型(1为中心网络 2 为车载网络) 
  38.      * @return 结果码 -1-本地设置为空;0-连接成功;1-已经为当前连接 
  39.      * @see [类、类#方法、类#成员] 
  40.      */  
  41.     public int access2Wifi(String type)  
  42.     {  
  43.         // 获取本地的网络配置信息  
  44.         NetworkConfigBean setting = configDBHelper.getNetworkConfigBySettingType(type).get(type);  
  45.         if (setting == null)  
  46.         {  
  47.             // 本地设置为空  
  48.             return -1;  
  49.         }  
  50.           
  51.         // 无线未打开时,开启无线  
  52.         if (!wm.isWifiEnabled() && WifiManager.WIFI_STATE_ENABLING != wm.getWifiState())  
  53.         {  
  54.             wm.setWifiEnabled(true);  
  55.         }  
  56.         // 获取本地的配置信息  
  57.         String sSSID = "\"" + setting.getSsid() + "\"";  
  58.         String sKey = "\"" + setting.getPassword() + "\"";  
  59.         int encryptionType = getKeyMgmtType(setting.getEncryptionType());  
  60.           
  61.         List<WifiConfiguration> configurations = wm.getConfiguredNetworks();  
  62.         WifiConfiguration config = null;  
  63.         boolean isExisted = false;  
  64.         int networkId = -1;  
  65.         for (int i = configurations.size() - 1; i >= 0; i--)  
  66.         {  
  67.             config = configurations.get(i);  
  68.             if (config.SSID.equals(sSSID))  
  69.             {  
  70.                 networkId = config.networkId;  
  71.                 isExisted = true;  
  72.                 break;  
  73.             }  
  74.         }  
  75.         if (!isExisted)  
  76.         {  
  77.             // 安全类型 无、WEP(128)、WPA(TKIP)、WPA2(AES)  
  78.             config = new WifiConfiguration();  
  79.             // 名称  
  80.             config.SSID = sSSID;  
  81.             config.allowedKeyManagement.set(encryptionType);  
  82.             if (encryptionType != 0)  
  83.             {  
  84.                 // 密码  
  85.                 config.preSharedKey = sKey;  
  86.             }  
  87.             config.hiddenSSID = false;  
  88.               
  89.             config.priority = 30;  
  90.             config.status = WifiConfiguration.Status.ENABLED;  
  91.             config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);  
  92.               
  93.             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);  
  94.             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);  
  95.             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);  
  96.             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);  
  97.               
  98.             config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);  
  99.             config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);  
  100.             config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);  
  101.               
  102.             config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);  
  103.             // 必须添加,否则无线路由无法连接  
  104.             config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);  
  105.               
  106.             networkId = wm.addNetwork(config);  
  107.             if (networkId != -1)  
  108.             {  
  109.                 wm.saveConfiguration();  
  110.             }  
  111.         }  
  112.         else  
  113.         {  
  114.             // 获取当前的wifi连接  
  115.             WifiInfo curConnection = wm.getConnectionInfo();  
  116.             if (curConnection != null && sSSID.equals(curConnection.getSSID()))  
  117.             {  
  118.                 // 已经是当前连接  
  119.                 return 1;  
  120.             }  
  121.               
  122.             config.allowedKeyManagement.set(encryptionType);  
  123.             if (encryptionType != 0)  
  124.             {  
  125.                 // 密码  
  126.                 config.preSharedKey = sKey;  
  127.             }  
  128.             wm.updateNetwork(config);  
  129.         }  
  130.           
  131.         if (networkId != -1)  
  132.         {  
  133.             wm.disconnect();  
  134.             wm.enableNetwork(networkId, true);  
  135.         }  
  136.         return 0;  
  137.     }  
  138.       
  139.     /** 
  140.      * 获取加密类型 
  141.      * @param type 加密类型 
  142.      * @return 加密类型 
  143.      * @see [类、类#方法、类#成员] 
  144.      */  
  145.     private int getKeyMgmtType(String type)  
  146.     {  
  147.         if (type == null)  
  148.         {  
  149.             return WifiConfiguration.KeyMgmt.NONE;  
  150.         }  
  151.           
  152.         if ("WEP".equals(type))  
  153.         {  
  154.             return WifiConfiguration.KeyMgmt.IEEE8021X;  
  155.         }  
  156.         else if ("WPA-PSK".equals(type))  
  157.         {  
  158.             return WifiConfiguration.KeyMgmt.WPA_PSK;  
  159.         }  
  160.         else if ("WPA2-PSK".equals(type))  
  161.         {  
  162.             return WifiConfiguration.KeyMgmt.WPA_PSK;  
  163.         }  
  164.         return WifiConfiguration.KeyMgmt.NONE;  
  165.     }  
  166. }  

转载于:https://my.oschina.net/u/1777508/blog/305360

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值