Wifi settings file android

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

How to programmatically create and read WEP/EAP WiFi configurations in Android? 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 achieve 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 achieve the goal. All the credit goes to number of open source projects and their developers. 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: 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.

I’d suggest you format it as a question and then answer it yourself. Format it nicely and we’ll have a quality question and answer.

This is very useful. Really want to know where is the document about this topic? Android’s docs tell me nothing 🙁

5 Answers 5

Part 1: Creating a WEP WiFi configuration programmatically

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

Following the permissions needed in AndroidManifest.xml

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

 void readWepConfig() < WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); Listitem = wifi.getConfiguredNetworks(); int i = item.size(); Log.d("WifiPreference", "NO OF CONFIG " + i ); Iterator 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]); > 

Part 3: Read a EAP WiFi Configuration programmatically
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 EnterpriseField is 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!

Читайте также:  Ax1500 wi fi range extender

Java Reflection API to the rescue Well I am not a Java expert so I wont be getting in to details of Reflection API as such and you can google for tutorials or get more information here. To keep it Short and Sweet, Reflection API allows you to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection.And, Importantly Reflection can help you access private data members inside a class Well this is what we need don’t we? 🙂

Let’s check the code example now which shows how to read a EAP WiFi configuration using Reflection Api. As a bonus the snippet will log the config to a file and save it on the SD Card. pretty slick ..eh 😉 A little bit of overview of Reflection Api and I am sure grasping the code below is easy.

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

This is the code to create a logfile on to SD card before calling the readEapConfig() function.

 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(">>>>>>>>>>>", "Could not write file " + e.getMessage()); > 

Now the readEapConfig() function itself:

 void readEapConfig(BufferedWriter out) < /*Get the WifiService */ WifiManager wifi = (WifiManager)getSystemService(WIFI_SERVICE); /*Get All WIfi configurations*/ ListconfigList = wifi.getConfiguredNetworks(); /*Now we need to search appropriate configuration i.e. with name SSID_Name*/ for(int i = 0;i >>>>>>>>>>", "[SSID]" + config.SSID); out.write(">>>>>>>>>>>" + "[SSID]" + config.SSID); Log.d(">>>>>>>>>>>", "[BSSID]" + config.BSSID); out.write(">>>>>>>>>>>" +"[BSSID]" + config.BSSID); Log.d(">>>>>>>>>>>", "[HIDDEN SSID]" + config.hiddenSSID); out.write(">>>>>>>>>>>" + "[HIDDEN SSID]" + config.hiddenSSID); Log.d(">>>>>>>>>>>", "[PASSWORD]" + config.preSharedKey); out.write(">>>>>>>>>>>"+ "[PASSWORD]" + config.preSharedKey); Log.d(">>>>>>>>>>>", "[ALLOWED ALGORITHMS]"); out.write(">>>>>>>>>>>"+ "[ALLOWED ALGORITHMS]"); Log.d(">>>>>>>>>>>", "[LEAP]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.LEAP)); out.write(">>>>>>>>>>>" + "[LEAP]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.LEAP)); Log.d(">>>>>>>>>>>", "[OPEN]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.OPEN)); out.write(">>>>>>>>>>>" + "[OPEN]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.OPEN)); Log.d(">>>>>>>>>>>", "[SHARED]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.SHARED)); out.write(">>>>>>>>>>>" + "[SHARED]" + config.allowedAuthAlgorithms.get(AuthAlgorithm.SHARED)); Log.d(">>>>>>>>>>>", "[GROUP CIPHERS]"); out.write(">>>>>>>>>>>" + "[GROUP CIPHERS]"); Log.d(">>>>>>>>>>>", "[CCMP]" + config.allowedGroupCiphers.get(GroupCipher.CCMP)); out.write(">>>>>>>>>>>" + "[CCMP]" + config.allowedGroupCiphers.get(GroupCipher.CCMP)); Log.d(">>>>>>>>>>>" , "[TKIP]" + config.allowedGroupCiphers.get(GroupCipher.TKIP)); out.write(">>>>>>>>>>>"+ "[TKIP]" + config.allowedGroupCiphers.get(GroupCipher.TKIP)); Log.d(">>>>>>>>>>>", "[WEP104]" + config.allowedGroupCiphers.get(GroupCipher.WEP104)); out.write(">>>>>>>>>>>" + "[WEP104]" + config.allowedGroupCiphers.get(GroupCipher.WEP104)); Log.d(">>>>>>>>>>>", "[WEP40]" + config.allowedGroupCiphers.get(GroupCipher.WEP40)); out.write(">>>>>>>>>>>" + "[WEP40]" + config.allowedGroupCiphers.get(GroupCipher.WEP40)); Log.d(">>>>>>>>>>>", "[KEYMGMT]"); out.write(">>>>>>>>>>>" + "[KEYMGMT]"); Log.d(">>>>>>>>>>>", "[IEEE8021X]" + config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)); out.write(">>>>>>>>>>>"+ "[IEEE8021X]" + config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)); Log.d(">>>>>>>>>>>", "[NONE]" + config.allowedKeyManagement.get(KeyMgmt.NONE)); out.write(">>>>>>>>>>>" + "[NONE]" + config.allowedKeyManagement.get(KeyMgmt.NONE)); Log.d(">>>>>>>>>>>", "[WPA_EAP]" + config.allowedKeyManagement.get(KeyMgmt.WPA_EAP)); out.write(">>>>>>>>>>>" + "[WPA_EAP]" + config.allowedKeyManagement.get(KeyMgmt.WPA_EAP)); Log.d(">>>>>>>>>>>", "[WPA_PSK]" + config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)); out.write(">>>>>>>>>>>" + "[WPA_PSK]" + config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)); Log.d(">>>>>>>>>>>", "[PairWiseCipher]"); out.write(">>>>>>>>>>>" + "[PairWiseCipher]"); Log.d(">>>>>>>>>>>", "[CCMP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.CCMP)); out.write(">>>>>>>>>>>" + "[CCMP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.CCMP)); Log.d(">>>>>>>>>>>", "[NONE]" + config.allowedPairwiseCiphers.get(PairwiseCipher.NONE)); out.write(">>>>>>>>>>>" + "[NONE]" + config.allowedPairwiseCiphers.get(PairwiseCipher.NONE)); Log.d(">>>>>>>>>>>", "[TKIP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.TKIP)); out.write(">>>>>>>>>>>" + "[TKIP]" + config.allowedPairwiseCiphers.get(PairwiseCipher.TKIP)); Log.d(">>>>>>>>>>>", "[Protocols]"); out.write(">>>>>>>>>>>" + "[Protocols]"); Log.d(">>>>>>>>>>>", "[RSN]" + config.allowedProtocols.get(Protocol.RSN)); out.write(">>>>>>>>>>>" + "[RSN]" + config.allowedProtocols.get(Protocol.RSN)); Log.d(">>>>>>>>>>>", "[WPA]" + config.allowedProtocols.get(Protocol.WPA)); out.write(">>>>>>>>>>>" + "[WPA]" + config.allowedProtocols.get(Protocol.WPA)); Log.d(">>>>>>>>>>>", "[PRE_SHARED_KEY]" + config.preSharedKey); out.write(">>>>>>>>>>>" + "[PRE_SHARED_KEY]" + config.preSharedKey); Log.d(">>>>>>>>>>>", "[WEP Key Strings]"); out.write(">>>>>>>>>>>" + "[WEP Key Strings]"); String[] wepKeys = config.wepKeys; Log.d(">>>>>>>>>>>", "[WEP KEY 0]" + wepKeys[0]); out.write(">>>>>>>>>>>" + "[WEP KEY 0]" + wepKeys[0]); Log.d(">>>>>>>>>>>", "[WEP KEY 1]" + wepKeys[1]); out.write(">>>>>>>>>>>" + "[WEP KEY 1]" + wepKeys[1]); Log.d(">>>>>>>>>>>", "[WEP KEY 2]" + wepKeys[2]); out.write(">>>>>>>>>>>" + "[WEP KEY 2]" + wepKeys[2]); Log.d(">>>>>>>>>>>", "[WEP KEY 3]" + wepKeys[3]); out.write(">>>>>>>>>>>" + "[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(">>>>>>>>>>>", "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(">>>>>>>>>>>", "[EAP METHOD]" + retval); out.write(">>>>>>>>>>>" + "[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(">>>>>>>>>>>", "[EAP PHASE 2 AUTHENTICATION]" + result); out.write(">>>>>>>>>>>" + "[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(">>>>>>>>>>>", "[EAP ANONYMOUS IDENTITY]" + result); out.write(">>>>>>>>>>>" + "[EAP ANONYMOUS IDENTITY]" + result); > else < result = (String) wcefAnonymousId.get(config); >/*CA certificate*/ if(!noEnterpriseFieldType) < result = (String) wcefValue.invoke(wcefCaCert.get(config), null); Log.d(">>>>>>>>>>>", "[EAP CA CERTIFICATE]" + result); out.write(">>>>>>>>>>>" + "[EAP CA CERTIFICATE]" + result); > else < result = (String)wcefCaCert.get(config); >/*private key*/ if(!noEnterpriseFieldType) < result = (String) wcefValue.invoke(wcefPrivateKey.get(config),null); Log.d(">>>>>>>>>>>", "[EAP PRIVATE KEY]" + result); out.write(">>>>>>>>>>>" + "[EAP PRIVATE KEY]" + result); > else < result = (String)wcefPrivateKey.get(config); >/*Identity*/ if(!noEnterpriseFieldType) < result = (String) wcefValue.invoke(wcefIdentity.get(config), null); Log.d(">>>>>>>>>>>", "[EAP IDENTITY]" + result); out.write(">>>>>>>>>>>" + "[EAP IDENTITY]" + result); > else < result = (String)wcefIdentity.get(config); >/*Password*/ if(!noEnterpriseFieldType) < result = (String) wcefValue.invoke(wcefPassword.get(config), null); Log.d(">>>>>>>>>>>", "[EAP PASSWORD]" + result); out.write(">>>>>>>>>>>" + "[EAP PASSWORD]" + result); > else < result = (String)wcefPassword.get(config); >/*client certificate*/ if(!noEnterpriseFieldType) < result = (String) wcefValue.invoke(wcefClientCert.get(config), null); Log.d(">>>>>>>>>>>", "[EAP CLIENT CERT]" + result); out.write(">>>>>>>>>>>" + "[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(">>>>>>>>>>>", "Could not write to ReadConfigLog.txt" + e.getMessage()); > catch(Exception e) < e.printStackTrace(); >> > > 

Источник

Читайте также:  Знать пароль от wi fi

How to get the wifi configuration file location in android

I’m developing an app that backup the wifi configuration from any android device (rooted) so I want to know how to get the file location in the android device so can I deal with it. I know there is a lot of location depending on your ROM or device like /data/wifi/bcm_supp.conf or /data/misc/wifi/wpa_supplicant.conf but I want to get it dynamically .

Care to explain, are YOU saving these settings to some place or have these settings already saved at some location and you just want to find them out? and please share what you have done so far, this will help answering.

Check this out for a couple of locations, rest you have to find out with certain cases. alt236.blogspot.com/2011/04/…

1 Answer 1

You need to create a WifiConfiguration instance like this:

String networkSSID = "test"; String networkPass = "pass"; WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" + networkSSID + "\""; // 

Then, for WEP network you need to do this:

conf.wepKeys[0] = "\"" + networkPass + "\""; conf.wepTxKeyIndex = 0; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

For WPA network you need to add passphrase like this:

conf.preSharedKey = "\""+ networkPass +"\""; 

For Open network you need to do this:

 conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 

Then, you need to add it to Android Wi-Fi manager settings:

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiManager.add(conf); 

And finally, you might need to enable it, so Android connects to it:

List list = wifiManager.getConfiguredNetworks(); for (WifiConfiguration i : list) < if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) < wm.disconnect(); wm.enableNetwork(i.networkId, true); wm.reconnect(); break; >> 

In case of WEP, if your password is in hex, you do not need to surround it with quotation marks.

Читайте также:  Не ищет вай фай дома

Источник

Оцените статью
Adblock
detector