android 获取当前隐藏和未隐藏wifi的加密方式(安全性)

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

    (1).WPA-PSK/WPA2-PSK(目前最安全家用加密)
    (2).WPA/WPA2(较不安全)
    (3).WEP(安全较差)
    (4).EAP(迄今最安全的)

android 获取当前隐藏和未隐藏wifi的加密方式(安全性)_第1张图片

2.一般点击系统当前网络都会有弹出详细信息

android 获取当前隐藏和未隐藏wifi的加密方式(安全性)_第2张图片

那么这个安全性是怎么获取的呢?通过查阅系统源码可知:

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("hefeng", "当前网络安全性:" + 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;
    }

这样就可以获取到了。这样做可以避免你的wifi ssid被隐藏了还可以获取到加密方式


3.一般获取加密方式是这样的,但是一旦你在路由器上面设置ssid隐藏了,你就搜索不到的

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

        List list = wifiManager.getScanResults();

        for (ScanResult scResult : list) {

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

                if (!TextUtils.isEmpty(capabilities)) {

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

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



你可能感兴趣的:(android开发之路)