【问题标题】:android: Determine security type of wifi networks in range (without connecting to them)android:确定范围内wifi网络的安全类型(不连接它们)
【发布时间】:2011-10-15 12:29:48
【问题描述】:

我可以枚举范围内的所有 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,但我不知道如何确定它们的安全类型。

有没有办法做到这一点?

提前致谢。

【问题讨论】:

    标签: android security wifi android-wifi


    【解决方案1】:

    您需要在 scanComplete 方法中解析 ScanResult 的功能字符串。根据Android developer documentation,:

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

    您也许可以使用——或者至少作为一个例子——AccessPointState 类中可用的静态辅助方法。

    【讨论】:

    • AccessPointState 类有一些很好的代码来解释 ScanResult 中的功能
    • 这些文件可以直接导入还是其他包的一部分?
    【解决方案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 的答案,因为它是一个特殊的回应并且更完整。

    【讨论】:

      【解决方案3】:

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

      我有话要补充。在不扫描网络的情况下,可以得到当前连接的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);
          }
      }
      

      【讨论】:

        【解决方案4】:

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

        首先您应该致电wifiManager.getConfiguredNetworks()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;
        }
        

        希望这有帮助。

        【讨论】:

        • 您不应该同时检查 WPA 吗?
        • 如果配置的网络是 WPA2_PSK,则此代码对于配置的网络将失败 .... 该方法将返回 SECURITY_NONE,这当然是错误的... Android API 甚至隐藏了对 KeyMgmt.WPA2_PSK 的访问,但是 config.allowedKeyManagement 无论如何都是 0,所以几乎没有办法正确检测存储在配置网络下的 WPA2 网络......
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多