Android 设置wifi连接应用开发

来源:互联网 发布:怎样才可以成为程序员 编辑:程序博客网 时间:2024/05/21 04:21

言简意赅的说吧。设置androidwifi连接,

1.new 一个wificonfiguration对象。

2.设置这个对象的一些属性。

[java] view plaincopy
  1. WifiConfiguration wc = new WifiConfiguration();  
  2. wc.SSID = "\""+sr.SSID+"\"";      //<span style="color: rgb(255, 0, 0); ">这个地方一定要注意了。旁边的“是不能够省略的。密码的地方也一样。</span>  
  3. wc.preSharedKey = "\""+etPassword.getText().toString()+"\"";      //该热点的密码  
  4. wc.hiddenSSID = true;  
  5. wc.status = WifiConfiguration.Status.ENABLED;  
  6. wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);  
  7. wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);  
  8. wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);  
  9. wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);  
  10. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);  
  11. wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);  
  12. wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);  


3.判断wifi是否加密:

[java] view plaincopy
  1. public static int getSecurity(ScanResult result) {  
  2.     if (result.capabilities.contains("WEP")) {  
  3.         return 1;  
  4.     } else if (result.capabilities.contains("PSK")) {  
  5.         return 2;  
  6.     } else if (result.capabilities.contains("EAP")) {  
  7.         return 3;  
  8.     }  
  9.     return 0;  
  10. }  

4.连接未加密wifi连接:

[java] view plaincopy
  1. <pre name="code" class="java">WifiConfiguration config = new WifiConfiguration();  
  2. config.SSID = "\"" + sr.SSID + "\"";  
  3. config.allowedKeyManagement.set(KeyMgmt.NONE);  
  4. int networkId = wifiManager.addNetwork(config);  
  5. if(networkId != -1){  
  6.     wifiManager.enableNetwork(networkId, false);  
  7.     wifiManager.saveConfiguration();  
  8. }  


原创粉丝点击