android:确定范围内wifi网络的安全类型(不连接它们)

Aid*_*n64 22 security android wifi android-wifi

我可以枚举范围内的所有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();
Run Code Online (Sandbox Code Playgroud)

在我的scanReceiver对象中:

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

再次在我的主要对象中:

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

代码工作,因为scanComplete最终被调用,我可以成功枚举所有附近的无线网络并获得他们的SSID和BSSID,但我无法弄清楚如何确定他们的安全类型.

有没有办法做到这一点?

提前致谢.

小智 35

我想你可以在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;
}
Run Code Online (Sandbox Code Playgroud)

希望这是有帮助的.

  • 如果配置的网络是WPA2_PSK,则此代码将失败,但该方法将失败.该方法将返回SECURITY_NONE,这当然是错误的... Android API甚至隐藏了对KeyMgmt.WPA2_PSK的访问权限,但是config.allowedKeyManagement仍为0,所以几乎没有办法正确检测存储在配置网络下的WPA2网络...... (2认同)

小智 25

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

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

您可以使用 - 或者至少用作示例 - AccessPointState类中可用的静态帮助器方法.


pra*_*esh 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);
    }
}
Run Code Online (Sandbox Code Playgroud)