Home > android, wifi > Adding wifi networks to Android’s “known” list

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
}
About these ads
Categories: android, wifi
  1. thedix
    February 12, 2012 at 1:25 pm

    aww :(
    Do you know where I can find those sources? I can’t see anything like that on this site ^^

  2. thedix
    February 11, 2012 at 10:26 pm

    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!

    • February 11, 2012 at 11:32 pm

      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.

  3. Dave
    August 17, 2011 at 8:43 am

    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!

    • August 17, 2011 at 12:15 pm

      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;
      	}
      
  4. emily
    July 26, 2011 at 1:44 pm

    great article,thanks

  5. Richard
    January 27, 2011 at 2:17 am

    A great article thank you

  6. David
    October 2, 2010 at 3:53 pm

    Nice article, great job… I was having problems with my wifi!! :)

  7. Paint me purple and call me barney...
    August 19, 2010 at 6:00 am

    Wow. I was missing the quotes all along. Great article, saved me from pulling all of the remaining hair out!

  8. kc
    July 2, 2010 at 6:09 am

    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 :) .

    • July 2, 2010 at 10:13 am

      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.

      • kc
        July 2, 2010 at 12:47 pm

        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./

      • July 2, 2010 at 3:35 pm

        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.

  9. androidz
    April 20, 2010 at 7:00 pm

    Nice Article… I love Android and Linux.. :D

  1. April 8, 2010 at 7:54 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 100 other followers