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.
Ahh I ran out of edit space, Adding the remaining part here.
Part 4: Save a EAP WiFi configuration programmatically
If you already read the part 3, you already understand the Reflection magic that works here, If you are directly jumping to this section please read the introduction before the code snippet in part 3 and you will be up to speed to breeze through the code here!
Well thats it! And I hope this helps some lost developer, somewhere, sometime :)
Part 4 started me off on the right path! However i wanted to create a TTLS rather than TLS config here is how i did it!
Hope this helps some one. @Android learner I removed the bit about adHocFrequency and SSID as they were causing crashes but my results were still good without them.
The WEP keys are masked, so it is not possible to read them with the mentioned code
Is there any way to solve this in same way as the EAP solution? With reflection?
Android has added an API to JellyBean 4.3. You must use this option if you want to configure WIFI on API 18:
http://developer.android.com/reference/android/net/wifi/WifiEnterpriseConfig.html
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:
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 EnterpriseFieldis private
insideWiFiConfiguration
class and all the symbols we cannot find are of the typeEnterpriseField
. 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 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.
This is the code to create a logfile on to SD card before calling the
readEapConfig()
function.Now the
readEapConfig()
function itself: