Android连接到EAP-AKA WiFi

时间:2016-03-23 20:20:28

标签: android wifi wifimanager

我需要一个简单的代码,如何从移动应用程序连接到安全的WIFI(EAP-AKA)。我有下面的代码,但我不知道是否正确,并且不知道如何使用它。如果有人有示例代码,那将会很棒。

  // Initialize the WifiConfiguration object
WifiConfiguration wifi = new WifiConfiguration();


WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
wifi = new WifiConfiguration();
wifi.SSID = ssid;
wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
enterpriseConfig.setIdentity(userName);
enterpriseConfig.setPassword(passWord);
enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.AKA);
wifi.enterpriseConfig = enterpriseConfig;

1 个答案:

答案 0 :(得分:2)

我找到了答案,并决定说出来,可能对任何人都有帮助

示例代码应用程序可以在我的存储库中找到 https://github.com/malah-code/Android-Eap-Aka-Sample

代码

// Initialize the WifiConfiguration object
        logThis("attemp to connect\n");
        mProgressView.refreshDrawableState();
        WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
        WifiConfiguration wifi = new WifiConfiguration();
        wifi.SSID = mSSIDView.getText().toString();
        wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        wifi.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
        enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.AKA);
        wifi.enterpriseConfig = enterpriseConfig;

        logThis("finding saved WiFi\n");
        wifi.networkId = ssidToNetworkId(wifi.SSID);

        if (wifi.networkId == -1) {
            logThis("WiFi not found - adding it.\n");
            wifiManager.addNetwork(wifi);
        } else {
            logThis("WiFi found - updating it.\n");
            wifiManager.updateNetwork(wifi);
        }
        logThis("saving config.\n");
        wifiManager.saveConfiguration();

        wifi.networkId = ssidToNetworkId(wifi.SSID);
        logThis("wifi ID in device = " + wifi.networkId + "\n");

        SupplicantState supState;
        int networkIdToConnect = wifi.networkId;
        if (networkIdToConnect >= 0) {
            logThis("Start connecting...\n");

            // We disable the network before connecting, because if this was the last connection before
            // a disconnect(), this will not reconnect.
            wifiManager.disableNetwork(networkIdToConnect);
            wifiManager.enableNetwork(networkIdToConnect, true);


            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            supState = wifiInfo.getSupplicantState();
            logThis("WifiWizard: Done connect to network : status =  " + supState.toString());
        } else {
            logThis("WifiWizard: cannot connect to network");
        }