WifiManager自动连接wifi接入点

  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 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. }  

你可能感兴趣的:(WifiManager自动连接wifi接入点)