Adding wifi networks to Android’s “known” list
Android’s wifi manager maintains a list of “known” wifi networks. This list can be obtained by calling the getConfiguredNetworks() method in WifiManager.
For each known network, Android keeps information about its security mode as well as its authorization key. This allows the user to provide the authorization key only once, and later easily connect to the network when coming within its range.
Networks are usually added to this list by Android’s built-in wifi manager. After connecting to a network for the first time, Android automaticalaly saves its data for later use.
Networks can also be added to the known list by calling addNetwork() in WifiManager. The network to be added is described by a WifiConfiguration object. Here is how to provide correct data.
First, instantiate a WifiConfiguration object and fill in the network’s SSID (note that it has to be enclosed in double quotes), set the initial state to disabled, and specify the network’s priority (numbers around 40 seem to work well).
WifiConfiguration wfc = new WifiConfiguration();
wfc.SSID = "\"".concat(ssid).concat("\"");
wfc.status = WifiConfiguration.Status.DISABLED;
wfc.priority = 40;
Now for the more complicated part: we need to fill several members of WifiConfiguration to specify the network’s security mode.
For open networks
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); wfc.allowedAuthAlgorithms.clear(); wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
For networks using WEP; note that the WEP key is also enclosed in double quotes.
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wfc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
if (isHexString(password)) wfc.wepKeys[0] = password;
else wfc.wepKeys[0] = "\"".concat(password).concat("\"");
wfc.wepTxKeyIndex = 0;
For networks using WPA and WPA2, we can set the same values for either
wfc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wfc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wfc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wfc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wfc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wfc.preSharedKey = "\"".concat(password).concat("\"");
Finally, we can add the network to the WifiManager’s known list
WifiManager wfMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int networkId = wfMgr.addNetwork(wfc);
if (networkId != -1) {
// success, can call wfMgr.enableNetwork(networkId, true) to connect
}
aww
Do you know where I can find those sources? I can’t see anything like that on this site ^^
There is a variety of places: github, grepcode, the offical repository…
Here is the relevant bit on grepcode:
http://www.grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/2.3.7_r1/com/android/settings/wifi/WifiDialog.java#88
thank you SO MUCH!
wow this is exactly what I’ve been looking for the last 36 hours
thanks!!
I’ve a problem tho, I keep getting a networkId value of -1, so I can’t actually connect, any idea of what the problem may be?
Furthermore I see that in wap you add WEP GroupCipher too, and in open there are both wep and wpa, why is that?
Thanks again for the article!
Hi, that post is rather old and may not be accurate for recent Android versions. You may want to check the sources for the built-in Settings app.
Great tutorial, I’ve been searching everywhere for this.
Just a couple quick questions. You are using the isHexString function. I can’t find that function anywhere. How are you calling it?
Also isn’t a hex number 0-9, A-F ? Can’t a password contain another value such as g-z? or is this converted to an ascii equivalent somewhere?
Thanks!
Dave,
WEP has two kinds of passwords, a hex value that specifies the key itself, or a character string, used to generate the real hex key. Depending on the kind, it needs to be surrounded with double quotes, or left as is.
Here is the correct function to check whether a string is a hex value – kind of password. Note the length checks: they correspond to WEP40, WEP104, and WEP232:
private boolean isHexWepKey(String s) { if (s == null) { return false; } int len = s.length(); if (len != 10 && len != 26 && len != 58) { return false; } for (int i = 0; i < len; ++i) { char c = s.charAt(i); if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { continue; } return false; } return true; }great article,thanks
A great article thank you
Nice article, great job… I was having problems with my wifi!!
Ok, cool.
Wow. I was missing the quotes all along. Great article, saved me from pulling all of the remaining hair out!
Hi Kostya Vasilyev,
How to put in username to the wificonfiguration ? wfc.preSharedKey is the password, but i can’t find any parameter is from username. Waiting for your reply.
Thanks
.
There is no such thing (username).
Looks like you are using WEP – there is just the “pre shared key”, a secret known to two parties – the router, and the client.
Hi,
My configuration is like below:
Security: WPA Enterprise
EAP Type: PEAP
Sub Type: PEAPv0/MSCHAPv2
And, It require a username + password. How can I enableNetwork() it and pass in username + password?
Thanks./
I don’t have exact info off hand.
You might want to try adding the network using Android’s built in tools, then call WifiManager.getConfiguredNetworks and inspect the matching WifiConfiguration.
Nice Article… I love Android and Linux..
Thanks, I’m glad.