如何以编程方式创建和读​​取WEP / EAP无线网络配置,Android的?无线网络、方式、EAP、WEP

2023-09-12 00:04:30 作者:醉风吹客衣

如何以编程方式创建并在Android中读 WEP / EAP无线网络配置

How to programatically create and read WEP/EAP WiFi configurations in Android?

我已经看到了一些人挣扎在此的各种论坛和所有在整个社会来说是非常的问题。我知道这是不是直线前进(尤其是EAP)来计算,因为当我想实现同样的我也挣扎着相当lot.Well中,code分析和搜索互联网上的各种实现所有的辛勤工作完成与我终于能获得了这幅目标。全部归功于开源项目及其开发者的数量。

I have seen a number of people struggling on this very question on various forums and all across the community. I know this is not that straight forward(especially EAP) to figure out because When I wanted to acheive the same I too struggled quite a lot.Well, all the hard work of code analysis and searching various implementations on the internet done with I was finally able to acheive the goal. All the credit goes to number of open source projects and their developers.

我想与大家分享这方面的知识与所有的,既然有这么鼓励这一点:这也是完全没有提出和回答你自己的问题,只要pretend你在危险边缘:短语它的问题的形式

I would like to share this knowledge with all, Since SO encourages this: "It's also perfectly fine to ask and answer your own question, as long as you pretend you're on Jeopardy: phrase it in the form of a question."

Part 1: 创建一个WEP无线网络的配置编程的

Part 2: 读一个WEP无线网络的配置编程的

Part 3: 看了EAP的WiFi配置编程的

Part 4: 保存EAP无线网络配置编程的

推荐答案

第1部分:创建一个WEP无线网络的配置编程的

Part 1: Creating a WEP WiFi configuration programatically

这是pretty的多少直接的,WifiConfiguration暴露来创建相同的接口。下面是示例code:

This is pretty much straightforward, WifiConfiguration exposes the interface to create the same. Here is the sample code:

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中的权限

Following the permissions needed in 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无线网络的配置编程的 再次Straighforward。下面是示例code:

Part 2: Read a WEP WiFi configuration programatically Straighforward again. Here is the sample code:

    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配置编程的 现在,这是非常棘手。你可以找到code它通过香草Android的UI在WifiDialog.java.那么很容易,我们可以使用相同的code在我们的应用程序,哦,不!如果你碰巧试试这个,你会得到错误说找不到符号 EAP client_cert这等。有一点详细的调查告诉我们EnterpriseFieldis私人在 WiFiConfiguration 类和所有我们无法找到该符号的类型 EnterpriseField 的。好了,我们已经打了一个障碍,我们需要这些领域的读取/保存EAP配置,但我们没有编程访问他们!

Part 3: Read a EAP WiFi Configuration programatically Now this is tricky. You can find the code which saves a EAP WiFi configuration through the vanilla Android UI in WifiDialog.java. Well easy enough We can use the same code in our Application, Well NO! If you happen to try this you will get errors saying cannot find the symbols eap, phase, client_cert and so on. A little detailed investigation tells us EnterpriseFieldis private inside WiFiConfiguration class and all the symbols we cannot find are of the type EnterpriseField. Well we've hit a roadblock, We need these fields for reading/saving a EAP config but we don't have programmatic access to them!

Java反射API 救援的 好吧,我不是Java专家,所以我不会越来越到的反射API细节,比如,你可以谷歌教程或获得更多的信息的