How to get WiFi security (NONE, WEP, WPA, WPA2) from Android WifiConfiguration entry?

你离开我真会死。 提交于 2019-12-09 12:02:47

问题


I need to get security type from WifiConfiguration entry. Is it possible?

String getSecurityType(WifiConfiguration conf) {
}

returning values "NONE", "WEP", "WPA", "WPA2"

For those who think it's a duplicate question: I want to get information from WiFiConfiguration object and not from ScanResult object. It's not the same!


回答1:


I can do it, simply:

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.KeyMgmt;

public static final int SECURITY_NONE = 0;
public static final int SECURITY_WEP = 1;
public static final int SECURITY_PSK = 2;
public static final int SECURITY_EAP = 3;

public 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;
}

so...

public static String getSecurityType(WifiConfiguration config) {
    switch (getSecurity(config)) {
        case SECURITY_WEP:
            return "WEP";
        case SECURITY_PSK:
            if (wifiConfiguration.allowedProtocols.get(WifiConfiguration.Protocol.RSN))
                return "WPA2";
            else
                return "WPA";
        default:
            return "NONE";
    }
}


来源:https://stackoverflow.com/questions/28022809/how-to-get-wifi-security-none-wep-wpa-wpa2-from-android-wificonfiguration-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!