如何以编程方式在Android中创建和读取WEP / EAP WiFi配置?
我看到很多人在各种论坛和整个社区的这个问题上苦苦挣扎.我知道这不是那么直接(尤其是EAP)要弄清楚因为当我想要实现同样的目标时我也非常挣扎.很好,代码分析和搜索互联网上各种实现的所有艰苦工作都是我用的终于能够实现目标了.所有的功劳归功于开源项目及其开发人员的数量.
我想与所有人分享这些知识,从SO encourages开始:“只要你假装自己在Jeopardy上,问问并回答你自己的问题也是完全没问题的:用问题的形式说出来.”
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.
解决方法:
第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配置的代码.很简单我们可以在我们的应用程序中使用相同的代码,不!如果你碰巧尝试这个,你会得到错误,说找不到符号eap,phase,clIEnt_cert等等.一个小细节调查告诉我们EnterpriseField在WiFiConfiguration类中是私有的,我们找不到的所有符号都是EnterpriseFIEld类型.好吧,我们遇到了障碍,我们需要这些字段来读取/保存EAP配置,但我们没有编程访问它们!
Java Reflection API来拯救
好吧,我不是Java专家所以我不会深入了解Reflection API的细节,你可以谷歌获取教程或获取更多信息here.
为了保持Short和Sweet,Reflection API允许您在运行时检查类,接口,字段和方法,而无需在编译时知道类,方法等的名称.也可以使用反射来实例化新对象,调用方法和获取/设置字段值.重要的是,Reflection可以帮助您访问类中的私有数据成员这是我们需要的不是吗? 总结
以上是内存溢出为你收集整理的如何以编程方式在Android中创建和读取WEP / EAP WiFi配置?全部内容,希望文章能够帮你解决如何以编程方式在Android中创建和读取WEP / EAP WiFi配置?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)