25

我可以枚举范围内的所有 wifi 网络(使用 startScan + SCAN_RESULTS_AVAILABLE_ACTION + getScanResults)并获取它们的 SSID 和 BSSID 值,但我不知道如何确定每个网络的安全类型。

在我的主要对象中:

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

在我的 scanReceiver 对象中:

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

再次在我的主要对象中:

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

代码在 scanComplete 最终被调用的范围内工作,我可以成功枚举附近的所有 wifi 网络并获取它们的 SSID 和 BSSID,但我不知道如何确定它们的安全类型。

有没有办法做到这一点?

提前致谢。

4

4 回答 4

37

我想你可以在 Settings.apk 的源代码中找到它。

首先你应该调用wifiManager.getConfiguredNetworks()or wifiManager.getScanResults(),然后使用以下两种方法:(在 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;
}

希望这会有所帮助。

于 2013-10-24T13:31:15.957 回答
25

您需要在 scanComplete 方法中解析 ScanResult 的功能字符串。根据Android 开发者文档,:

ScanResult.capabilities 描述了接入点支持的身份验证、密钥管理和加密方案。

您可能能够使用——或者至少作为示例使用——AccessPointState 类中可用的静态帮助方法。

于 2011-08-10T23:52:41.360 回答
7

非常感谢,...你让我很开心...

我有一些东西要在这里补充。在不扫描网络的情况下,可以得到当前连接的 wifi 配置信息(特别是加密和密钥管理),如下所示,

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
List<ScanResult> networkList = wifi.getScanResults();
if (networkList != null) {
    for (ScanResult network : networkList)
    {
        String Capabilities =  network.capabilities;        
        Log.w (TAG, network.SSID + " capabilities : " + Capabilities);
    }
}
于 2011-09-05T08:21:20.010 回答
2

每个网络的安全类型在扫描结果的 Capabilities 列中。

因此,要为您获取安全类型,请将其添加到您的代码部分。

public void scanComplete()
{
    List<ScanResult> networkList = ((WifiManager)getSystemService.(Context.WIFI_SERVICE)).getScanResults();
    for (ScanResult network : networkList)
    {
        String Capabilities =  network.capabilities;

        //Then you could add some code to check for a specific security type. 
        if(Capabilities.contains("WPA"))
     {
          // We know there is WPA encryption
         }
        else if(Capabilities.contains("WEP"))
         {
         // We know there is WEP encryption
         }
        else
         { 
         // Another type of security scheme, open wifi, captive portal, etc..
         }

    }
}

无论如何,这里有一些快速的源代码。不过,我会完全推荐 Ayj 的答案,因为它是一个特殊的回应并且更完整。

于 2011-08-23T04:05:32.847 回答