Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android: Determine security type of wifi networks in range (without connecting to them)

I can enumerate all wifi networks in range (using startScan + SCAN_RESULTS_AVAILABLE_ACTION + getScanResults) and get their SSID and BSSID values, but I can't figure out how to determine the security type of each network.

In my main object:

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
    registerReceiver(scanReceiver, intentFilter);
    ((WifiManager)getSystemService(Context.WIFI_SERVICE)).startScan();

In my scanReceiver object:

public void onReceive(Context c, Intent intent) {
    if (WifiManager.SCAN_RESULTS_AVAILABLE_ACTION.equals(intent.getAction())){
        mainObject.scanComplete();
    }
}

And again in my main object:

public void scanComplete()
{
    List<ScanResult> networkList = ((WifiManager)getSystemService.(Context.WIFI_SERVICE)).getScanResults();
    for (ScanResult network : networkList)
    {
        <do stuff>
    }
}

The code works insofar that scanComplete eventually gets called, and I can successfully enumerate all nearby wifi networks and get their SSID and BSSID, but I can't figure out how to determine their security type.

Is there a way to do this?

Thanks in advance.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you can find it in the source code of Settings.apk.

First you should call wifiManager.getConfiguredNetworks() or wifiManager.getScanResults(), then use the two methods below: (find them in AccessPoint class "com.android.settings.wifi"):

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

static int getSecurity(ScanResult result) {
    if (result.capabilities.contains("WEP")) {
        return SECURITY_WEP;
    } else if (result.capabilities.contains("PSK")) {
        return SECURITY_PSK;
    } else if (result.capabilities.contains("EAP")) {
        return SECURITY_EAP;
    }
    return SECURITY_NONE;
}

Hope this is helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...