Android 获取wifi的加密方式

Android 获取当前wifi的加密方式

由于有需求是搜索附近的网络,通过账号和密码连接wifi,但是连接的时候需要知道连接的wifi加密方式

这篇博客 也是我写的,由于忘记账号和密码了,所以换了账号,重新整理了一下
原文地址:
android获取当前隐藏和未隐藏wifi的加密方式(安全性)


1.一般WIFI加密有几种方式

  • WPA-PSK/WPA2-PSK(目前最安全家用加密)
  • WPA/WPA2(较不安全)
  • WEP(安全较差)
  • AP(迄今最安全的)

    Android 获取wifi的加密方式_第1张图片

2.获取WiFi加密方式一(不推荐)

 public void getCipherType(Context context, String ssid) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        List<ScanResult> list = wifiManager.getScanResults();

        for (ScanResult scResult : list) {

            if (!TextUtils.isEmpty(scResult.SSID) && scResult.SSID.equals(ssid)) {
                String capabilities = scResult.capabilities;
                Log.i("river","capabilities=" + capabilities);

                if (!TextUtils.isEmpty(capabilities)) {

                    if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
                        Log.i("river", "wpa");

                    } else if (capabilities.contains("WEP") || capabilities.contains("wep")) {
                        Log.i("river", "wep");
                    } else {
                        Log.i("river", "no");
                    }
                }
            }
        }
    }

注意:此方法获取不到隐藏的网络加密方式

3.获取WiFi加密方式二(推荐)

于是查看系统设置源码中查看,系统是如何获取到隐藏网络的加密方式

    /** 
     * These values are matched in string arrays -- changes must be kept in sync 
     */  
    static final int SECURITY_NONE = 0;  
    static final int SECURITY_WEP = 1;  
    static final int SECURITY_PSK = 2;  
    static final int SECURITY_EAP = 3;  

    static int getSecurity(WifiConfiguration config) {  
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {  
            return SECURITY_PSK;  
        }  
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {  
            return SECURITY_EAP;  
        }  
        return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;  
    }  

测试代码

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = mWifiManager.getConnectionInfo();


        // 得到配置好的网络连接
        List wifiConfigList = mWifiManager.getConfiguredNetworks();

        for (WifiConfiguration wifiConfiguration : wifiConfigList) {
            //配置过的SSID
            String configSSid = wifiConfiguration.SSID;
            configSSid = configSSid.replace("\"", "");

            //当前连接SSID
            String currentSSid =info.getSSID();
            currentSSid = currentSSid.replace("\"", "");

            //比较networkId,防止配置网络保存相同的SSID
            if (currentSSid.equals(configSSid)&&info.getNetworkId()==wifiConfiguration.networkId) {
                Log.e("river", "当前网络安全性:" + getSecurity(wifiConfiguration));
            }
        }
    }

    /**
     * These values are matched in string arrays -- changes must be kept in sync
     */
    static final int SECURITY_NONE = 0;
    static final int SECURITY_WEP = 1;
    static final int SECURITY_PSK = 2;
    static final int SECURITY_EAP = 3;

    static int getSecurity(WifiConfiguration config) {
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
            return SECURITY_PSK;
        }
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
            return SECURITY_EAP;
        }
        return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
    }

你可能感兴趣的:(Android开发总结)