如何在Android中以编程方式创建和读​​取WEP / EAP WiFi配置?


111

如何以编程方式WEP/EAP WiFi configurations在Android中创建和阅读?

我已经在许多论坛和整个社区中看到许多人在为这个问题而苦苦挣扎。我知道这不是直截了当的(尤其是EAP),因为当我想实现相同目标时,我也很费劲。终于能够实现目标。所有功劳归功于开源项目及其开发人员的数量。

我想与所有人分享这些知识,因为SO 鼓励这样做:“只要假装自己处于危险之中,问问并回答自己的问题也很好,以问题的形式表达出来。”

第1部分: 以编程方式创建WEP WiFi配置。

第2部分: 以编程方式阅读WEP WiFi配置。

第3部分: 以编程方式阅读EAP WiFi配置。

第4部分: 以编程方式保存EAP WiFi配置。


我建议您将其格式化为问题,然后自己回答。很好地格式化它,我们将提供高质量的问答。
Octavian A. Damiean 2010年

@Octavian Damiean:感谢您的注意。我试图确保格式正确。欢迎任何意见!
Alok保存

看起来很棒!感谢分享!在SO Android聊天室访问我们。
Octavian A. Damiean 2010年

Android在API 18中添加了WifiEnterpriseConfig以支持EAP wifi
ospider'5

这非常有用。真的想知道有关此主题的文档在哪里?Android的文档什么都没告诉我:(
Charlesjean

Answers:


107

第1部分:以编程方式创建WEP WiFi配置

这非常简单,WifiConfiguration公开接口以创建相同的接口。这是示例代码:

void saveWepConfig()
{
    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration wc = new WifiConfiguration(); 
    wc.SSID = "\"SSID_NAME\""; //IMP! This should be in Quotes!!
    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.DISABLED;     
    wc.priority = 40;
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 
    wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
    wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

    wc.wepKeys[0] = "\"aaabbb1234\""; //This is the WEP Password
    wc.wepTxKeyIndex = 0;

    WifiManager  wifiManag = (WifiManager) this.getSystemService(WIFI_SERVICE);
    boolean res1 = wifiManag.setWifiEnabled(true);
    int res = wifi.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res );
    boolean es = wifi.saveConfiguration();
    Log.d("WifiPreference", "saveConfiguration returned " + es );
    boolean b = wifi.enableNetwork(res, true);   
    Log.d("WifiPreference", "enableNetwork returned " + b );  

}

遵循AndroidManifest.xml中所需的权限

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
    </uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
    </uses-permission>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
    </uses-permission>

第2部分:
再次以编程方式直接读取WEP WiFi配置。这是示例代码:

    void readWepConfig()
    { 
        WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
        List<WifiConfiguration> item = wifi.getConfiguredNetworks();
        int i = item.size();
        Log.d("WifiPreference", "NO OF CONFIG " + i );
        Iterator<WifiConfiguration> iter =  item.iterator();
        WifiConfiguration config = item.get(0);
        Log.d("WifiPreference", "SSID" + config.SSID);
        Log.d("WifiPreference", "PASSWORD" + config.preSharedKey);
        Log.d("WifiPreference", "ALLOWED ALGORITHMS");
        Log.d("WifiPreference", "LEAP" + config.allowedAuthAlgorithms.get(AuthAlgorithm.LEAP));
        Log.d("WifiPreference", "OPEN" + config.allowedAuthAlgorithms.get(AuthAlgorithm.OPEN));
        Log.d("WifiPreference", "SHARED" + config.allowedAuthAlgorithms.get(AuthAlgorithm.SHARED));
        Log.d("WifiPreference", "GROUP CIPHERS");
        Log.d("WifiPreference", "CCMP" + config.allowedGroupCiphers.get(GroupCipher.CCMP));
        Log.d("WifiPreference", "TKIP" + config.allowedGroupCiphers.get(GroupCipher.TKIP));
        Log.d("WifiPreference", "WEP104" + config.allowedGroupCiphers.get(GroupCipher.WEP104));
        Log.d("WifiPreference", "WEP40" + config.allowedGroupCiphers.get(GroupCipher.WEP40));
        Log.d("WifiPreference", "KEYMGMT");
        Log.d("WifiPreference", "IEEE8021X" + config.allowedKeyManagement.get(KeyMgmt.IEEE8021X));
        Log.d("WifiPreference", "NONE" + config.allowedKeyManagement.get(KeyMgmt.NONE));
        Log.d("WifiPreference", "WPA_EAP" + config.allowedKeyManagement.get(KeyMgmt.WPA_EAP));
        Log.d("WifiPreference", "WPA_PSK" + config.allowedKeyManagement.get(KeyMgmt.WPA_PSK));
        Log.d("WifiPreference", "PairWiseCipher");
        Log.d("WifiPreference", "CCMP" + config.allowedPairwiseCiphers.get(PairwiseCipher.CCMP));
        Log.d("WifiPreference", "NONE" + config.allowedPairwiseCiphers.get(PairwiseCipher.NONE));
        Log.d("WifiPreference", "TKIP" + config.allowedPairwiseCiphers.get(PairwiseCipher.TKIP));
        Log.d("WifiPreference", "Protocols");
        Log.d("WifiPreference", "RSN" + config.allowedProtocols.get(Protocol.RSN));
        Log.d("WifiPreference", "WPA" + config.allowedProtocols.get(Protocol.WPA));
        Log.d("WifiPreference", "WEP Key Strings");
        String[] wepKeys = config.wepKeys;
        Log.d("WifiPreference", "WEP KEY 0" + wepKeys[0]);
        Log.d("WifiPreference", "WEP KEY 1" + wepKeys[1]);
        Log.d("WifiPreference", "WEP KEY 2" + wepKeys[2]);
        Log.d("WifiPreference", "WEP KEY 3" + wepKeys[3]);
    }

第3部分:以编程方式阅读EAP WiFi配置
现在这很棘手。您可以在 WifiDialog.java中找到通过香草Android UI保存EAP WiFi配置的代码。好容易,我们可以在应用程序中使用相同的代码,好吧!如果你碰巧试试这个,你会得到错误说找不到符号eapphaseclient_cert等等。进行了一些详细的调查后,我们了解到类内部的 EnterpriseField,我们找不到的所有符号都是该类型的。好吧,我们遇到了一个障碍,我们需要这些字段来读取/保存EAP配置,但是我们无法通过编程方式访问它们! is privateWiFiConfigurationEnterpriseField

Java Reflection API救援 好吧,我不是一个Java专家,所以我不会在越来越反射API的细节,例如,你可以谷歌教程或获得更多的信息在这里。为了简短易懂,Reflection API允许您在运行时检查类,接口,字段和方法,而无需在编译时知道类,方法等的名称。还可以使用反射实例化新对象,调用方法并获取/设置字段值。而且,重要的是,反射可以帮助您访问类内的私有数据成员,这不是我们所需要的吗?:)

现在,让我们检查代码示例,该示例显示如何使用Reflection Api读取EAP WiFi配置。作为奖励,代码段会将配置记录到文件中,并将其保存在SD卡上.... pretty slick ..eh;)反射Api的概述,我相信掌握以下代码很容易。

    private static final String INT_PRIVATE_KEY = "private_key";
    private static final String INT_PHASE2 = "phase2";
    private static final String INT_PASSWORD = "password";
    private static final String INT_IDENTITY = "identity";
    private static final String INT_EAP = "eap";
    private static final String INT_CLIENT_CERT = "client_cert";
    private static final String INT_CA_CERT = "ca_cert";
    private static final String INT_ANONYMOUS_IDENTITY = "anonymous_identity";
    final String INT_ENTERPRISEFIELD_NAME = "android.net.wifi.WifiConfiguration$EnterpriseField";

这是在调用readEapConfig()函数之前在SD卡上创建日志文件的代码。

        BufferedWriter out = null;
        try 
        {
            File root = Environment.getExternalStorageDirectory();
            Toast toast = Toast.makeText(this, "SD CARD mounted and writable? " + root.canWrite(), 5000);
            toast.show();
            if (root.canWrite())
            {
                File gpxfile = new File(root, "ReadConfigLog.txt");
                FileWriter gpxwriter = new FileWriter(gpxfile);
                out = new BufferedWriter(gpxwriter);
                out.write("Hello world");
                //out.close();
            }
        } catch (IOException e) 
        {
            Toast toast = Toast.makeText(this, "Problem reading SD CARD", 3000);
            Toast toast2 = Toast.makeText(this, "Please take logs using Logcat", 5000);
            Log.e("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "Could not write file " + e.getMessage());
        }

现在readEapConfig()函数本身:

    void readEapConfig(BufferedWriter out)
    {
        /*Get the WifiService */        
        WifiManager wifi = (WifiManager)getSystemService(WIFI_SERVICE);
        /*Get All WIfi configurations*/
        List<WifiConfiguration> configList = wifi.getConfiguredNetworks();
        /*Now we need to search appropriate configuration i.e. with name SSID_Name*/
        for(int i = 0;i<configList.size();i++)
        {
            if(configList.get(i).SSID.contentEquals("\"SSID_NAME\""))
            {
                /*We found the appropriate config now read all config details*/
                Iterator<WifiConfiguration> iter =  configList.iterator();
                WifiConfiguration config = configList.get(i);

                /*I dont think these fields have anything to do with EAP config but still will
                 * print these to be on safe side*/
                try {
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[SSID]" + config.SSID);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[SSID]" + config.SSID);
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[BSSID]" + config.BSSID);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" +"[BSSID]" + config.BSSID);
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[HIDDEN SSID]" + config.hiddenSSID);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[HIDDEN SSID]" + config.hiddenSSID);
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[PASSWORD]" + config.preSharedKey);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>"+ "[PASSWORD]" + config.preSharedKey);
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[ALLOWED ALGORITHMS]");
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>"+ "[ALLOWED ALGORITHMS]");
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[LEAP]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.LEAP));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[LEAP]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.LEAP));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[OPEN]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.OPEN));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[OPEN]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.OPEN));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[SHARED]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.SHARED));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[SHARED]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.SHARED));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[GROUP CIPHERS]");
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[GROUP CIPHERS]");
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[CCMP]" + config.allowedGroupCiphers.get(GroupCipher.CCMP));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[CCMP]" + config.allowedGroupCiphers.get(GroupCipher.CCMP));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" , "[TKIP]" + config.allowedGroupCiphers.get(GroupCipher.TKIP));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>"+ "[TKIP]" + config.allowedGroupCiphers.get(GroupCipher.TKIP));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP104]" + config.allowedGroupCiphers.get(GroupCipher.WEP104));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP104]" + config.allowedGroupCiphers.get(GroupCipher.WEP104));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP40]" + config.allowedGroupCiphers.get(GroupCipher.WEP40));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP40]" + config.allowedGroupCiphers.get(GroupCipher.WEP40));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[KEYMGMT]");
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[KEYMGMT]");
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[IEEE8021X]" + config.allowedKeyManagement.get(KeyMgmt.IEEE8021X));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>"+ "[IEEE8021X]" + config.allowedKeyManagement.get(KeyMgmt.IEEE8021X));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[NONE]" + config.allowedKeyManagement.get(KeyMgmt.NONE));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[NONE]" + config.allowedKeyManagement.get(KeyMgmt.NONE));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WPA_EAP]" + config.allowedKeyManagement.get(KeyMgmt.WPA_EAP));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WPA_EAP]" + config.allowedKeyManagement.get(KeyMgmt.WPA_EAP));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WPA_PSK]" + config.allowedKeyManagement.get(KeyMgmt.WPA_PSK));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WPA_PSK]" + config.allowedKeyManagement.get(KeyMgmt.WPA_PSK));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[PairWiseCipher]");
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[PairWiseCipher]");
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[CCMP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.CCMP));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[CCMP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.CCMP));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[NONE]" + config.allowedPairwiseCiphers.get(PairwiseCipher.NONE));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[NONE]" + config.allowedPairwiseCiphers.get(PairwiseCipher.NONE));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[TKIP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.TKIP));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[TKIP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.TKIP));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[Protocols]");
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[Protocols]");
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[RSN]" + config.allowedProtocols.get(Protocol.RSN));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[RSN]" + config.allowedProtocols.get(Protocol.RSN));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WPA]" + config.allowedProtocols.get(Protocol.WPA));
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WPA]" + config.allowedProtocols.get(Protocol.WPA));
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[PRE_SHARED_KEY]" + config.preSharedKey);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[PRE_SHARED_KEY]" + config.preSharedKey);
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP Key Strings]");
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP Key Strings]");
                String[] wepKeys = config.wepKeys;
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP KEY 0]" + wepKeys[0]);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP KEY 0]" + wepKeys[0]);
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP KEY 1]" + wepKeys[1]);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP KEY 1]" + wepKeys[1]);
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP KEY 2]" + wepKeys[2]);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP KEY 2]" + wepKeys[2]);
                Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[WEP KEY 3]" + wepKeys[3]);
                out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[WEP KEY 3]" + wepKeys[3]);

                }
                catch(IOException e) 
                {
                    Toast toast1 = Toast.makeText(this, "Failed to write Logs to ReadConfigLog.txt", 3000);
                    Toast toast2 = Toast.makeText(this, "Please take logs using Logcat", 5000);
                    Log.e("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "Could not write to ReadConfigLog.txt" + e.getMessage());
                }
                /*reflection magic*/
                /*These are the fields we are really interested in*/
                try 
                {
                    // Let the magic start
                    Class[] wcClasses = WifiConfiguration.class.getClasses();
                    // null for overzealous java compiler
                    Class wcEnterpriseField = null;

                    for (Class wcClass : wcClasses)
                        if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME)) 
                        {
                            wcEnterpriseField = wcClass;
                            break;
                        }
                    boolean noEnterpriseFieldType = false; 
                    if(wcEnterpriseField == null)
                        noEnterpriseFieldType = true; // Cupcake/Donut access enterprise settings directly

                    Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null;
                    Field[] wcefFields = WifiConfiguration.class.getFields();
                    // Dispatching Field vars
                    for (Field wcefField : wcefFields) 
                    {
                        if (wcefField.getName().trim().equals(INT_ANONYMOUS_IDENTITY))
                            wcefAnonymousId = wcefField;
                        else if (wcefField.getName().trim().equals(INT_CA_CERT))
                            wcefCaCert = wcefField;
                        else if (wcefField.getName().trim().equals(INT_CLIENT_CERT))
                            wcefClientCert = wcefField;
                        else if (wcefField.getName().trim().equals(INT_EAP))
                            wcefEap = wcefField;
                        else if (wcefField.getName().trim().equals(INT_IDENTITY))
                            wcefIdentity = wcefField;
                        else if (wcefField.getName().trim().equals(INT_PASSWORD))
                            wcefPassword = wcefField;
                        else if (wcefField.getName().trim().equals(INT_PHASE2))
                            wcefPhase2 = wcefField;
                        else if (wcefField.getName().trim().equals(INT_PRIVATE_KEY))
                            wcefPrivateKey = wcefField;
                    }
                Method wcefValue = null;
                if(!noEnterpriseFieldType)
                {
                for(Method m: wcEnterpriseField.getMethods())
                //System.out.println(m.getName());
                if(m.getName().trim().equals("value")){
                    wcefValue = m;
                    break;
                }
                }

                /*EAP Method*/
                String result = null;
                Object obj = null;
                if(!noEnterpriseFieldType)
                {
                    obj = wcefValue.invoke(wcefEap.get(config), null);
                    String retval = (String)obj;
                    Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP METHOD]" + retval);
                    out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP METHOD]" + retval);
                }
                else
                {
                    obj = wcefEap.get(config);
                    String retval = (String)obj;                        
                }

                /*phase 2*/
                if(!noEnterpriseFieldType)
                {
                    result = (String) wcefValue.invoke(wcefPhase2.get(config), null);
                    Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP PHASE 2 AUTHENTICATION]" + result);
                    out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP PHASE 2 AUTHENTICATION]" + result);
                }
                else
                {
                    result = (String) wcefPhase2.get(config);
                }

                /*Anonymous Identity*/
                if(!noEnterpriseFieldType)
                {
                    result = (String) wcefValue.invoke(wcefAnonymousId.get(config),null);
                    Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP ANONYMOUS IDENTITY]" + result);
                    out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP ANONYMOUS IDENTITY]" + result);
                }
                else
                {
                    result = (String) wcefAnonymousId.get(config);
                }

                /*CA certificate*/
                if(!noEnterpriseFieldType)
                {
                    result = (String) wcefValue.invoke(wcefCaCert.get(config), null);
                    Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP CA CERTIFICATE]" + result);
                    out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP CA CERTIFICATE]" + result);
                }
                else
                {
                    result = (String)wcefCaCert.get(config);

                }

                /*private key*/
                if(!noEnterpriseFieldType)
                {
                    result = (String) wcefValue.invoke(wcefPrivateKey.get(config),null);
                    Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP PRIVATE KEY]" + result);
                    out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP PRIVATE KEY]" + result);
                }
                else
                {
                    result = (String)wcefPrivateKey.get(config);
                }

                /*Identity*/
                if(!noEnterpriseFieldType)
                {
                    result = (String) wcefValue.invoke(wcefIdentity.get(config), null);
                    Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP IDENTITY]" + result);
                    out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP IDENTITY]" + result);
                }
                else
                {
                    result = (String)wcefIdentity.get(config);
                }

                /*Password*/
                if(!noEnterpriseFieldType)
                {
                    result = (String) wcefValue.invoke(wcefPassword.get(config), null);
                    Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP PASSWORD]" + result);
                    out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP PASSWORD]" + result);
                }
                else
                {
                    result = (String)wcefPassword.get(config);
                }

                /*client certificate*/
                if(!noEnterpriseFieldType)
                {
                    result = (String) wcefValue.invoke(wcefClientCert.get(config), null);
                    Log.d("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "[EAP CLIENT CERT]" + result);
                    out.write("<<<<<<<<<<WifiPreference>>>>>>>>>>>>" + "[EAP CLIENT CERT]" + result);
                    Toast toast1 = Toast.makeText(this, "All config data logged to ReadConfigLog.txt", 3000);
                    Toast toast2 = Toast.makeText(this, "Extract ReadConfigLog.txt from SD CARD", 5000);
                }
                else
                {
                    result = (String)wcefClientCert.get(config);
                }

                out.close();

                }
                catch(IOException e) 
                {
                    Toast toast1 = Toast.makeText(this, "Failed to write Logs to ReadConfigLog.txt", 3000);
                    Toast toast2 = Toast.makeText(this, "Please take logs using Logcat", 5000);
                    Log.e("<<<<<<<<<<WifiPreference>>>>>>>>>>>>", "Could not write to ReadConfigLog.txt" + e.getMessage());
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }

            }
        }
    }

我尝试这样做,但是由于某种原因,它在无线网络中创建了一个新接口(注释说“无法访问”),但是SSID与原始网络完全相同。在这方面有帮助吗?
nithinreddy 2012年

@nithinreddy:恐怕我不再涉足Android(已经很长时间了),所以怀疑是否可以提供任何帮助.AFAIR,您不应有两个具有相同SSID名称的配置,因为无法解决的问题,请检查您的配置参数。我的猜测是某处可能不匹配..手动进行配置,检查其是否连接,然后以编程方式阅读配置参数(请参阅上面答案中的Howto?详细信息),然后使用这些参数以编程方式创建配置。 connects不是您以编程方式添加的
-Alok Save

1
@AlokSave当您提供有关证书的步骤时,这将是很棒的。(用于EAP WIFI)。我有一些疑问,如何创建这些证书以及如何以编程方式安装它们。实际上,我最近两天一直在搜索,但没有找到以编程方式安装证书的方法。因此,请分享您的知识。
Android学习者2012年

1
我想确认以编程方式进行操作时,Android(此处为4.1)上的WEP出现了问题。但是,如果我在系统设置中手动执行此操作,它将起作用。我花了几天时间尝试所有内容,然后改用WPA,并且在两种情况下都可以使用(手动和程序方式)。因此对于WEP:我确实读过手动创建的WEP结构(它可以正常工作)以编程方式精确地重现它,但是没有办法,它永远保持在“ OBTAINING_IPADDR”状态。我发现另一个人也有同样的问题,因此被警告。这里的信息很棒,但是缺少某些信息(至少在某些情况下)。
亚历克斯

1
您为什么要对WEP使用AuthAlgorithm.OPEN?,Android文档说“开放系统身份验证(WPA / WPA2必需)”
David

36

啊,我用完了编辑空间,在这里添加了剩余的部分。

第4部分:以编程方式保存EAP WiFi配置

如果您已经阅读了第3部分,那么您已经了解了在这里起作用的反射魔术,如果您直接跳到本部分,请在第3部分中的代码片段之前阅读介绍,您将可以通过这里的代码来轻松学习!

void saveEapConfig(String passString, String userName)
    {
    /********************************Configuration Strings****************************************************/
    final String ENTERPRISE_EAP = "TLS";
    final String ENTERPRISE_CLIENT_CERT = "keystore://USRCERT_CertificateName";
    final String ENTERPRISE_PRIV_KEY = "USRPKEY_CertificateName";
    //CertificateName = Name given to the certificate while installing it

    /*Optional Params- My wireless Doesn't use these*/
    final String ENTERPRISE_PHASE2 = "";
    final String ENTERPRISE_ANON_IDENT = "ABC";
    final String ENTERPRISE_CA_CERT = ""; // If required: "keystore://CACERT_CaCertificateName"
    /********************************Configuration Strings****************************************************/

    /*Create a WifiConfig*/
    WifiConfiguration selectedConfig = new WifiConfiguration();

    /*AP Name*/
    selectedConfig.SSID = "\"SSID_Name\"";

    /*Priority*/
    selectedConfig.priority = 40;

    /*Enable Hidden SSID*/
    selectedConfig.hiddenSSID = true;

    /*Key Mgmnt*/
    selectedConfig.allowedKeyManagement.clear();
    selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
    selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

    /*Group Ciphers*/
    selectedConfig.allowedGroupCiphers.clear();
    selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

    /*Pairwise ciphers*/
    selectedConfig.allowedPairwiseCiphers.clear();
    selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

    /*Protocols*/
    selectedConfig.allowedProtocols.clear();
    selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

    // Enterprise Settings
    // Reflection magic here too, need access to non-public APIs
    try {
        // Let the magic start
        Class[] wcClasses = WifiConfiguration.class.getClasses();
        // null for overzealous java compiler
        Class wcEnterpriseField = null;

        for (Class wcClass : wcClasses)
            if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME)) 
            {
                wcEnterpriseField = wcClass;
                break;
            }
        boolean noEnterpriseFieldType = false; 
        if(wcEnterpriseField == null)
            noEnterpriseFieldType = true; // Cupcake/Donut access enterprise settings directly

        Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null, wcefEngine = null, wcefEngineId = null;
        Field[] wcefFields = WifiConfiguration.class.getFields();
        // Dispatching Field vars
        for (Field wcefField : wcefFields) 
        {
            if (wcefField.getName().equals(INT_ANONYMOUS_IDENTITY))
                wcefAnonymousId = wcefField;
            else if (wcefField.getName().equals(INT_CA_CERT))
                wcefCaCert = wcefField;
            else if (wcefField.getName().equals(INT_CLIENT_CERT))
                wcefClientCert = wcefField;
            else if (wcefField.getName().equals(INT_EAP))
                wcefEap = wcefField;
            else if (wcefField.getName().equals(INT_IDENTITY))
                wcefIdentity = wcefField;
            else if (wcefField.getName().equals(INT_PASSWORD))
                wcefPassword = wcefField;
            else if (wcefField.getName().equals(INT_PHASE2))
                wcefPhase2 = wcefField;
            else if (wcefField.getName().equals(INT_PRIVATE_KEY))
                wcefPrivateKey = wcefField;
            else if (wcefField.getName().equals("engine"))
                wcefEngine = wcefField;
            else if (wcefField.getName().equals("engine_id"))
                wcefEngineId = wcefField;
        }


        Method wcefSetValue = null;
        if(!noEnterpriseFieldType){
        for(Method m: wcEnterpriseField.getMethods())
            //System.out.println(m.getName());
            if(m.getName().trim().equals("setValue"))
                wcefSetValue = m;
        }


        /*EAP Method*/
        if(!noEnterpriseFieldType)
        {
                wcefSetValue.invoke(wcefEap.get(selectedConfig), ENTERPRISE_EAP);
        }
        else
        {
                wcefEap.set(selectedConfig, ENTERPRISE_EAP);
        }
        /*EAP Phase 2 Authentication*/
        if(!noEnterpriseFieldType)
        {
                wcefSetValue.invoke(wcefPhase2.get(selectedConfig), ENTERPRISE_PHASE2);
        }
        else
        {
              wcefPhase2.set(selectedConfig, ENTERPRISE_PHASE2);
        }
        /*EAP Anonymous Identity*/
        if(!noEnterpriseFieldType)
        {
                wcefSetValue.invoke(wcefAnonymousId.get(selectedConfig), ENTERPRISE_ANON_IDENT);
        }
        else
        {
              wcefAnonymousId.set(selectedConfig, ENTERPRISE_ANON_IDENT);
        }
        /*EAP CA Certificate*/
        if(!noEnterpriseFieldType)
        {
                wcefSetValue.invoke(wcefCaCert.get(selectedConfig), ENTERPRISE_CA_CERT);
        }
        else
        {
              wcefCaCert.set(selectedConfig, ENTERPRISE_CA_CERT);
        }               
        /*EAP Private key*/
        if(!noEnterpriseFieldType)
        {
                wcefSetValue.invoke(wcefPrivateKey.get(selectedConfig), ENTERPRISE_PRIV_KEY);
        }
        else
        {
              wcefPrivateKey.set(selectedConfig, ENTERPRISE_PRIV_KEY);
        }               
        /*EAP Identity*/
        if(!noEnterpriseFieldType)
        {
                wcefSetValue.invoke(wcefIdentity.get(selectedConfig), userName);
        }
        else
        {
              wcefIdentity.set(selectedConfig, userName);
        }               
        /*EAP Password*/
        if(!noEnterpriseFieldType)
        {
                wcefSetValue.invoke(wcefPassword.get(selectedConfig), passString);
        }
        else
        {
              wcefPassword.set(selectedConfig, passString);
        }               
        /*EAp Client certificate*/
        if(!noEnterpriseFieldType)
        {
            wcefSetValue.invoke(wcefClientCert.get(selectedConfig), ENTERPRISE_CLIENT_CERT);
        }
        else
        {
              wcefClientCert.set(selectedConfig, ENTERPRISE_CLIENT_CERT);
        }
        /*Engine fields*/
        if(!noEnterpriseFieldType)
        {
           wcefSetValue.invoke(wcefEngine.get(wifiConf), "1");
           wcefSetValue.invoke(wcefEngineId.get(wifiConf), "keystore");
        }

        // Adhoc for CM6
        // if non-CM6 fails gracefully thanks to nested try-catch

        try{
        Field wcAdhoc = WifiConfiguration.class.getField("adhocSSID");
        Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");
        //wcAdhoc.setBoolean(selectedConfig, prefs.getBoolean(PREF_ADHOC,
        //      false));
        wcAdhoc.setBoolean(selectedConfig, false);
        int freq = 2462;    // default to channel 11
        //int freq = Integer.parseInt(prefs.getString(PREF_ADHOC_FREQUENCY,
        //"2462"));     // default to channel 11
        //System.err.println(freq);
        wcAdhocFreq.setInt(selectedConfig, freq); 
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    } catch (Exception e)
    {
        // TODO Auto-generated catch block
        // FIXME As above, what should I do here?
        e.printStackTrace();
    }

    WifiManager wifiManag = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    boolean res1 = wifiManag.setWifiEnabled(true);
    int res = wifiManag.addNetwork(selectedConfig);
    Log.d("WifiPreference", "add Network returned " + res );
    boolean b = wifiManag.enableNetwork(selectedConfig.networkId, false);
    Log.d("WifiPreference", "enableNetwork returned " + b );
    boolean c = wifiManag.saveConfiguration();
    Log.d("WifiPreference", "Save configuration returned " + c );
    boolean d = wifiManag.enableNetwork(res, true);   
    Log.d("WifiPreference", "enableNetwork returned " + d );  
}

好吧!我希望这可以在某个时间某处帮助一些迷失的开发人员:)


7
@abresas:正如我在答案中提到的那样,此资源是由我开发的,我是从互联网上的各种资源中获取信息的,并且早就回来了,当时发布了新的SDK,关于这些的信息不多。当前的Android状况(因为我目前无法在Android上使用)。此外,我对该代码没有任何版权,并且使用它的任何人都没有任何问题,这就是将其首先发布在此处的原因,但我不知道关于资料的想法/观点,这有助于我撰写该资料。
Alok保存

1
Field wcAdhoc = WifiConfiguration.class.getField("adhocSSID"); Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");。这些成员不在WifiConfiguration.java中。该代码给了我一个例外java.lang.NoSuchFieldException: adhocSSID 。请帮忙。
Android学习者2012年

1
我正在使用Android 4.1.2,因此也许此代码不再适用于最新版本。
Tiago Babo

1
我注意到它不适用于4.1或4.2。该行wcefPrivateKey.get(selectedConfig)抛出一个NullPointerException。其他人有运气吗?
Dulax

1
您是对的@PrashanthDebbadwar,它不再适用于API 18+。此代码仅适用于早期版本。
BurninatorDor

5

Android已在JellyBean 4.3中添加了API。如果要在API 18上配置WIFI,则必须使用此选项:

http://developer.android.com/reference/android/net/wifi/WifiEnterpriseConfig.html


2
这不仅仅是“另一种选择”。android.net.wifi.WifiConfiguration $ EnterpriseField不再存在(大概是从添加了WifiEnterpriseConfig的API 18开始),因此Alok的解决方案中断了。现在,要在API 18+和API 17-上运行的应用程序都需要这两种解决方案。
rmanna 2014年

1

第四部分让我走上了正确的道路!但是我想创建一个TTLS而不是TLS配置,这就是我的方法!

    /********************************Configuration Strings****************************************************/
    final String ENTERPRISE_EAP = "TTLS";

    /*Optional Params- My wireless Doesn't use these*/
    final String ENTERPRISE_PHASE2 = "PAP";
    final String ENTERPRISE_ANON_IDENT = "ABC";
    final String ENTERPRISE_CA_CERT = "";
    /********************************Configuration Strings****************************************************/

    /*Create a WifiConfig*/
    WifiConfiguration selectedConfig = new WifiConfiguration();

    /*AP Name*/
    selectedConfig.SSID = "\"EAP_SSID_TEST_CONFIG\"";

    /*Priority*/
    selectedConfig.priority = 40;

    /*Enable Hidden SSID*/
    selectedConfig.hiddenSSID = false;

    /*Key Mgmnt*/
    selectedConfig.allowedKeyManagement.clear();
    selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
    selectedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);

    /*Group Ciphers*/
    selectedConfig.allowedGroupCiphers.clear();
    selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    selectedConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

    /*Pairwise ciphers*/
    selectedConfig.allowedPairwiseCiphers.clear();
    selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    selectedConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);

    /*Protocols*/
    selectedConfig.allowedProtocols.clear();
    selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    selectedConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

    // Enterprise Settings
    // Reflection magic here too, need access to non-public APIs
    try {
        // Let the magic start
        Class[] wcClasses = WifiConfiguration.class.getClasses();
        // null for overzealous java compiler
        Class wcEnterpriseField = null;

        for (Class wcClass : wcClasses)
            if (wcClass.getName().equals(INT_ENTERPRISEFIELD_NAME)) 
            {
                wcEnterpriseField = wcClass;
                break;
            }
        boolean noEnterpriseFieldType = false; 
        if(wcEnterpriseField == null)
            noEnterpriseFieldType = true; // Cupcake/Donut access enterprise settings directly

        Field wcefAnonymousId = null, wcefCaCert = null, wcefClientCert = null, wcefEap = null, wcefIdentity = null, wcefPassword = null, wcefPhase2 = null, wcefPrivateKey = null;
        Field[] wcefFields = WifiConfiguration.class.getFields();
        // Dispatching Field vars
        for (Field wcefField : wcefFields) 
        {
            if (wcefField.getName().equals(INT_ANONYMOUS_IDENTITY))
                wcefAnonymousId = wcefField;
            else if (wcefField.getName().equals(INT_CA_CERT))
                wcefCaCert = wcefField;
            else if (wcefField.getName().equals(INT_CLIENT_CERT))
                wcefClientCert = wcefField;
            else if (wcefField.getName().equals(INT_EAP))
                wcefEap = wcefField;
            else if (wcefField.getName().equals(INT_IDENTITY))
                wcefIdentity = wcefField;
            else if (wcefField.getName().equals(INT_PASSWORD))
                wcefPassword = wcefField;
            else if (wcefField.getName().equals(INT_PHASE2))
                wcefPhase2 = wcefField;
            else if (wcefField.getName().equals(INT_PRIVATE_KEY))
                wcefPrivateKey = wcefField;
        }


        Method wcefSetValue = null;
        if(!noEnterpriseFieldType){
        for(Method m: wcEnterpriseField.getMethods())
            //System.out.println(m.getName());
            if(m.getName().trim().equals("setValue"))
                wcefSetValue = m;
        }


        /*EAP Method*/
        if(!noEnterpriseFieldType){
            wcefSetValue.invoke(wcefEap.get(selectedConfig), ENTERPRISE_EAP);
        }
        /*EAP Phase 2 Authentication*/
        if(!noEnterpriseFieldType){
            wcefSetValue.invoke(wcefPhase2.get(selectedConfig), ENTERPRISE_PHASE2);
        }
        /*EAP Anonymous Identity*/
        if(!noEnterpriseFieldType){
            wcefSetValue.invoke(wcefAnonymousId.get(selectedConfig), ENTERPRISE_ANON_IDENT);
        }
        /*EAP CA Certificate*/
        if(!noEnterpriseFieldType){
            wcefSetValue.invoke(wcefCaCert.get(selectedConfig), ENTERPRISE_CA_CERT);
        }


        /*EAP Identity*/
        if(!noEnterpriseFieldType){
            wcefSetValue.invoke(wcefIdentity.get(selectedConfig), "test user name");
        }
        /*EAP Password*/
        if(!noEnterpriseFieldType){
            wcefSetValue.invoke(wcefPassword.get(selectedConfig), "test password");
        }


        try{

        } catch (Exception e)
        {
            e.printStackTrace();
        }

    } catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    WifiManager wifiManag = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    boolean res1 = wifiManag.setWifiEnabled(true);
    int res = wifiManag.addNetwork(selectedConfig);
    Log.d("WifiPreference", "add Network returned " + res );
//        boolean b = wifiManag.enableNetwork(selectedConfig.networkId, false);
//        Log.d("WifiPreference", "enableNetwork returned " + b );
//        boolean c = wifiManag.saveConfiguration();
//        Log.d("WifiPreference", "Save configuration returned " + c );
//        boolean d = wifiManag.enableNetwork(res, true);   
//        Log.d("WifiPreference", "enableNetwork returned " + d ); 


}

希望这可以帮助一些人。@Android学习者,我删除了有关adHocFrequency和SSID的内容,因为它们会导致崩溃,但是没有它们,我的结果仍然很好。


0

WEP密钥被屏蔽,​​因此无法使用上述代码读取它们

    Log.d("WifiPreference", "WEP KEY 0" + wepKeys[0]);
    Log.d("WifiPreference", "WEP KEY 1" + wepKeys[1]);
    Log.d("WifiPreference", "WEP KEY 2" + wepKeys[2]);
    Log.d("WifiPreference", "WEP KEY 3" + wepKeys[3]);

有什么方法可以和EAP解决方案一样解决此问题?有反思吗?


对于eap,反射也不适用于我们要求的特定领域。您解决问题了吗?
Prashanth Debbadwar '16
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.