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


For networks using WEP; note that the WEP key is also enclosed in double quotes.


			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.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
Categories: android, wifi
  1. Bhavjinder Singh
    January 23, 2015 at 8:04 am

    Still n’t able to connect wifi network with “WEP” security,Please find my code snippet below:
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    List results = wifiManager.getScanResults();
    WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = “\”MGMT\””;
    wc.wepKeys[0] = “\”2143658709\””;
    wc.wepTxKeyIndex = 0;
    // wc.preSharedKey = “\”2143658709\””;
    wc.status = WifiConfiguration.Status.ENABLED;
    // wc.SSID = “\”SSID_NAME\””; //IMP! This should be in Quotes!!
    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.DISABLED;
    wc.priority = 40;
    wc.wepTxKeyIndex = 0;
    wc.wepKeys[0] = ‘”‘ + “2143658709” + ‘”‘;
    // connect to and enable the connection
    int netId = wifiManager.addNetwork(wc);
    boolean b = wifiManager.enableNetwork(netId, true);
    Actually after running this code, i m n’t able to connect same wifi even manually. Then i have to push the forget option on same network then again connect that network.

    Regards Bhavjinder

  2. Bhavjinder
    January 21, 2015 at 6:59 pm

    Not able to connect wifi network with ‘WEP’ security. Im getting network id from the addNetwork method. Same network id i’m using in the enableNetwork method. EnableNetwork method returning true.

    Please help me..

  3. 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 ^^

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

    wow this is exactly what I’ve been looking for the last 36 hours

    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.

  5. 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?


    • August 17, 2011 at 12:15 pm


      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')) {
      			return false;
      		return true;
  6. emily
    July 26, 2011 at 1:44 pm

    great article,thanks

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

    A great article thank you

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

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

  9. 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!

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


        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?


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

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

    Nice Article… I love Android and Linux.. 😀

  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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s