1 public class ConnectWifi { 2 private WifiManager wifiManager; 3 4 public ConnectWifi(Context context) { 5 wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 6 } 7 8 public void connectWifiDHCP(String ssid, String pswd, int security) { 9 WifiConfiguration mWifiConfig = getConfig(ssid, pswd, security); 10 int networkId = wifiManager.addNetwork(mWifiConfig); 11 wifiManager.enableNetwork(networkId, true); 12 } 13 public void connectWifiSTATIC() throws UnknownHostException { 14 //得到连接中的WifiConfiguration. 15 int wifiState = wifiManager.getWifiState(); 16 WifiInfo connectionInfo = null; 17 WifiConfiguration connectionConfig = null; 18 if(wifiState== WifiManager.WIFI_STATE_ENABLED){ 19 connectionInfo = wifiManager.getConnectionInfo(); 20 } 21 List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks(); 22 for(WifiConfiguration config:configuredNetworks){ 23 if(config.networkId==connectionInfo.getNetworkId()){ 24 connectionConfig = config; 25 } 26 } 27 Log.e("lvxingang", "connectionConfig.SSID = "+connectionConfig.SSID); 28 29 wifiManager.disableNetwork(connectionInfo.getNetworkId()); 30 31 32 33 //设置已经连接的ip,sub,gateway,dns. 34 connectionConfig.ipAssignment = IpAssignment.STATIC;//设置静态 35 connectionConfig.preSharedKey = null;//设置加密方式. 36 //设置linkProperties 37 LinkProperties linkProperties = new LinkProperties(); 38 InetAddress ip = InetAddress.getByName("192.168.1.104"); 39 InetAddress gateway = InetAddress.getByName("192.168.1.1"); 40 InetAddress dns = InetAddress.getByName("8.8.8.8"); 41 42 linkProperties.addLinkAddress(new LinkAddress(ip, 24)); 43 linkProperties.addRoute(new RouteInfo(gateway)); 44 linkProperties.addDns(dns); 45 connectionConfig.linkProperties = linkProperties; 46 int addNetwork = wifiManager.addNetwork(connectionConfig); 47 wifiManager.enableNetwork(addNetwork, true); 48 49 } 50 51 private WifiConfiguration getConfig(String ssid, String pswd, int security) { 52 WifiConfiguration config = new WifiConfiguration(); 53 config.SSID = AccessPoint.convertToQuotedString(ssid); 54 config.hiddenSSID = true; 55 switch (security) { 56 case AccessPoint.SECURITY_NONE: 57 config.allowedKeyManagement.set(KeyMgmt.NONE); 58 break; 59 case AccessPoint.SECURITY_WEP: 60 config.allowedKeyManagement.set(KeyMgmt.NONE); 61 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 62 config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); 63 if (pswd != null && !pswd.equals("")) { 64 int length = pswd.length(); 65 String password = pswd; 66 // WEP-40, WEP-104, and 256-bit WEP (WEP-232?) 67 if ((length == 10 || length == 26 || length == 58) && password.matches("[0-9A-Fa-f]*")) { 68 config.wepKeys[0] = password; 69 } else { 70 config.wepKeys[0] = '"' + password + '"'; 71 } 72 } 73 break; 74 case AccessPoint.SECURITY_PSK: 75 // config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 76 config.allowedKeyManagement.set(KeyMgmt.WPA_PSK); 77 if (pswd != null && !pswd.equals("")) { 78 String password = pswd; 79 if (password.matches("[0-9A-Fa-f]{64}")) { 80 config.preSharedKey = password; 81 } else { 82 config.preSharedKey = '"' + password + '"'; 83 } 84 } 85 break; 86 case AccessPoint.SECURITY_EAP: 87 config.allowedKeyManagement.set(KeyMgmt.WPA_EAP); 88 config.allowedKeyManagement.set(KeyMgmt.IEEE8021X); 89 break; 90 default: 91 return null; 92 } 93 config.proxySettings = ProxySettings.NONE;// 代理设置 94 config.priority = 2; 95 config.allowedProtocols.set(Protocol.WPA); 96 config.allowedProtocols.set(Protocol.RSN); 97 return config; 98 } 99 }