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

Posted

技术标签:

【中文标题】如何以编程方式在 Android 中创建和读取 WEP/EAP WiFi 配置?【英文标题】:How to programmatically create and read WEP/EAP WiFi configurations in Android? 【发布时间】:2011-05-21 11:10:04 【问题描述】:

如何在 android 中以编程方式创建和读​​取WEP/EAP WiFi configurations

我在各种论坛和整个社区中看到很多人在这个问题上苦苦挣扎。我知道这不是那么直截了当(尤其是 EAP),因为当我想实现同样的目标时,我也很挣扎。好吧,所有的代码分析和在互联网上搜索各种实现的辛勤工作都是我完成的终于能够达到目的。所有功劳都归功于开源项目及其开发人员的数量。

我想与所有人分享这些知识,因为 SO encourages 这个:“问和回答你自己的问题也很好,只要你假装你在危险中:用以下形式表达它一个问题。”

Part 1: Creating a WEP WiFi configuration programmatically.

Part 2: Read a WEP WiFi configuration programmatically.

Part 3: Read a EAP WiFi Configuration programmatically.

Part 4: Save a EAP WiFi configuration programmatically.

【问题讨论】:

我建议您将其格式化为问题,然后自己回答。很好地格式化它,我们会有一个高质量的问题和答案。 @Octavian Damiean:感谢您的提醒。我试图确保良好的格式。欢迎任何cmets! 看起来很棒!感谢分享!访问我们的SO Android chat room。 Android 在 API 18 中添加 WifiEnterpriseConfig 以支持 EAP wifi 这非常有用。真的想知道关于这个主题的文档在哪里? Android 的文档什么也没告诉我 :( 【参考方案1】:

第 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 中找到通过 vanilla Android UI 保存 EAP WiFi 配置的代码。很简单我们可以在我们的应用程序中使用相同的代码,嗯,不!如果你碰巧尝试这个,你会得到错误提示找不到符号eapphaseclient_cert 等等。稍微详细的调查告诉我们EnterpriseFieldis privateWiFiConfiguration 类中,我们找不到的所有符号都是EnterpriseField 类型。好吧,我们遇到了一个障碍,我们需要这些字段来读取/保存 EAP 配置,但我们没有对它们的编程访问权限!

Java Reflection API 救援 好吧,我不是 Java 专家,所以我不会详细介绍反射 API,您可以在谷歌上搜索教程或获取更多信息 here。 为了保持简短和甜蜜,反射 API 允许您在运行时检查类、接口、字段和方法,而无需在编译时知道类、方法等的名称。还可以使用反射实例化新对象、调用方法和获取/设置字段值。重要的是,反射可以帮助您访问类中的私有数据成员是我们需要的不是吗? :)

现在让我们检查一下代码示例,它展示了如何使用 Reflection Api 读取 EAP WiFi 配置。作为奖励,sn-p 会将配置记录到文件中并将其保存在 SD 卡上....非常漂亮..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:恐怕我不再涉足Android(已经很长时间没有了)所以怀疑如果我能提供任何帮助。AFAIR,你不应该有两个配置SSID 名称相同,至于无法访问的问题,请检查您的配置参数。我的猜测是某处可能存在不匹配..手动进行配置,检查它连接然后以编程方式读取配置参数(请参阅上面答案中的如何?详细信息)然后使用这些参数以编程方式创建配置。可能是配置连接不是您以编程方式添加的那个 @AlokSave 这将是一个伟大的,当您将提供有关证书的步骤时。 (对于 EAP WIFI)。我有一些问题如何创建这些证书以及如何以编程方式安装它们。实际上,我最近两天都在谷歌搜索,但没有找到以编程方式安装证书的方法。所以请分享你的知识。 我想确认以编程方式执行 WEP 在 Android(此处为 4.1)上存在问题。但是,如果我在系统设置中手动执行此操作,则它可以工作。我尝试了几天,然后改为 WPA,它在两种情况下都有效(手动和编程)。所以对于 WEP:我确实在手动创建(有效)时阅读了 WEP 结构以完全以编程方式重现它,但没办法,它只是永远保持在“OBTAINING_IPADDR”状态。我发现另一个人有同样的问题,所以请注意。这里的信息很棒,但缺少一些东西(至少在某些情况下)。 你为什么使用 AuthAlgorithm.OPEN 进行 WEP?,android 文档说“开放系统身份验证(WPA/WPA2 需要)”【参考方案2】:

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

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

如果您已经阅读了第 3 部分,那么您已经了解了此处起作用的反射魔法,如果您直接跳到本节,请阅读第 3 部分中代码 sn-p 之前的介绍,您将快速掌握轻松浏览这里的代码!

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 );  

嗯,就是这样!我希望这可以帮助一些迷失的开发人员,在某个地方,某个时候:)

【讨论】:

@abresas:正如我在答案中提到的,这个来源是我从互联网上的各种来源获取信息而开发的,很久以前,当时发布了一个新的 SDK,并没有太多信息可用关于这些。我不知道当前的 Android 情况,因为我目前不使用 Android。另外,我对这段代码没有版权,我对任何使用它的人都没有任何问题,这就是在这里发布它的原因首先,但我不知道帮助我编写此资源的资源的想法/前景。 Field wcAdhoc = WifiConfiguration.class.getField("adhocSSID"); Field wcAdhocFreq = WifiConfiguration.class.getField("frequency");。这些成员不在 WifiConfiguration.java 中。代码给了我一个例外java.lang.NoSuchFieldException: adhocSSID 。请帮忙。 我使用的是 Android 4.1.2,所以这段代码可能不再适用于最新版本。 我注意到它不适用于 4.1 或 4.2。 wcefPrivateKey.get(selectedConfig) 行抛出 NullPointerException。其他人有什么好运吗? 你是对的@PrashanthDebbadwar,它不再适用于 API 18+。此代码仅适用于早期版本。【参考方案3】:

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

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

【讨论】:

这不仅仅是“另一种选择”。 android.net.wifi.WifiConfiguration$EnterpriseField 不再存在(大概从 API 18 开始,他们添加了 WifiEnterpriseConfig),所以 Alok 的解决方案中断了。现在,想要在 API 18+ 和 API 17- 上运行的应用程序都需要这两种解决方案。【参考方案4】:

第 4 部分让我走上了正确的道路!但是我想创建一个 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 的内容,因为它们会导致崩溃,但没有它们我的结果仍然很好。

【讨论】:

【参考方案5】:

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,反射也不适用于我们需要的特定字段。你的问题解决了吗?

以上是关于如何以编程方式在 Android 中创建和读取 WEP/EAP WiFi 配置?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Firestore 中创建和更新数字数组?

如何在 MATLAB 中创建和保存大型数据集?

如何在 Flutter 中创建和读取一组键值对(映射)以在 Firestore 中存储数据

如何在android中以编程方式授予系统级权限?

如何“手动”(以编程方式)在古腾堡中插入一个块?

如何以编程方式在 android 中创建 *** 配置文件并连接到它?