如何从Android WifiConfiguration条目获得WiFi安全性(NONE,WEP,WPA,WPA2)?

时间:2015-01-19 10:45:12

标签: android android-wifi wificonfiguration

我需要从WifiConfiguration条目获取安全类型。有可能吗?

String getSecurityType(WifiConfiguration conf) {
}

返回值"NONE", "WEP", "WPA", "WPA2"

对于那些认为这是一个重复问题的人:我想从WiFiConfiguration对象获取信息,而不是从ScanResult对象获取信息。它不一样了!

1 个答案:

答案 0 :(得分:7)

我能做到,只是:

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

所以...

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