标签:style blog io ar color sp for on div
1 public class WifiConnect { 2 3 WifiManager wifiManager; 4 5 // 构造函数 6 public WifiConnect(WifiManager wifiManager) { 7 this.wifiManager = wifiManager; 8 } 9 10 // 提供一个外部接口,传入要连接的无线网 11 public boolean Connect(String SSID, String Password, int Type) { 12 13 WifiConfiguration wifiConfig = this 14 .CreateWifiInfo(SSID, Password, Type); 15 16 WifiConfiguration tempConfig = this.IsExsits(SSID); 17 if (tempConfig != null) { 18 wifiManager.removeNetwork(tempConfig.networkId); 19 } 20 21 int netID = wifiManager.addNetwork(wifiConfig); 22 boolean flag = wifiManager.enableNetwork(netID, true); 23 return flag; 24 } 25 26 // 查看以前是否也配置过这个网络 27 private WifiConfiguration IsExsits(String SSID) { 28 List<WifiConfiguration> existingConfigs = wifiManager 29 .getConfiguredNetworks(); 30 for (WifiConfiguration existingConfig : existingConfigs) { 31 if (existingConfig.SSID.equals("\"" + SSID + "\"")) { 32 return existingConfig; 33 } 34 } 35 return null; 36 } 37 38 // 配置WifiConfiguration 39 public WifiConfiguration CreateWifiInfo(String SSID, String Password, 40 int Type) { 41 WifiConfiguration config = new WifiConfiguration(); 42 config.allowedAuthAlgorithms.clear(); 43 config.allowedGroupCiphers.clear(); 44 config.allowedKeyManagement.clear(); 45 config.allowedPairwiseCiphers.clear(); 46 config.allowedProtocols.clear(); 47 config.SSID = "\"" + SSID + "\""; 48 49 WifiConfiguration tempConfig = this.IsExsits(SSID); 50 if (tempConfig != null) { 51 wifiManager.removeNetwork(tempConfig.networkId); 52 } 53 54 if (Type == 1) // WIFICIPHER_NOPASS 55 { 56 config.wepKeys[0] = ""; 57 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 58 config.wepTxKeyIndex = 0; 59 } 60 if (Type == 2) // WIFICIPHER_WEP 61 { 62 config.hiddenSSID = true; 63 config.wepKeys[0] = "\"" + Password + "\""; 64 config.allowedAuthAlgorithms 65 .set(WifiConfiguration.AuthAlgorithm.SHARED); 66 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 67 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 68 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 69 config.allowedGroupCiphers 70 .set(WifiConfiguration.GroupCipher.WEP104); 71 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 72 config.wepTxKeyIndex = 0; 73 } 74 if (Type == 3) // WIFICIPHER_WPA 75 { 76 config.preSharedKey = "\"" + Password + "\""; 77 config.hiddenSSID = true; 78 config.allowedAuthAlgorithms 79 .set(WifiConfiguration.AuthAlgorithm.OPEN); 80 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 81 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 82 config.allowedPairwiseCiphers 83 .set(WifiConfiguration.PairwiseCipher.TKIP); 84 // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 85 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 86 config.allowedPairwiseCiphers 87 .set(WifiConfiguration.PairwiseCipher.CCMP); 88 config.status = WifiConfiguration.Status.ENABLED; 89 } 90 return config; 91 } 92 93 }
标签:style blog io ar color sp for on div
原文地址:http://www.cnblogs.com/yangleda/p/4149430.html