19

In Android 1.5 (also on 1.6)

How to add an Access Point from code?

Given Access point that supports WPA2. Here is my code snippet.

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
// This is must be quoted according to the documentation 
// http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#SSID
wc.SSID = "\"SSIDName\"";
wc.preSharedKey  = "password";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;        
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifi.enableNetwork(res, true);        
Log.d("WifiPreference", "enableNetwork returned " + b );

This code fails as in LogCat appear

01-26 16:44:13.550: ERROR/wpa_supplicant(2032): Line 0: Invalid PSK 'password'.

I am sure that this is the password and that all of the rest of the parameters are right. What do I do I miss?

Boris Daich
  • 2,431
  • 3
  • 23
  • 27

3 Answers3

47

The reason for the my sorrow is here in this Documentation issue

While documentation here states

"Pre-shared key for use with WPA-PSK. When the value of this key is read, the actual key is not returned, just a "*" if the key has a value, or the null string otherwise."

It is correct, but very important what it does not say is that expected here ether 64 byte hash result of the linux command

wpa_passphrase <ssid> [passphrase] 

or Access Point's password IN DOUBLE QUOTES!

So in case that Access Point's PSK is "example" it has to be passed in java like this

WifiConfiguration myWiFiConfig = new WifiConfiguration();
...
myWiFiConfig.preSharedKey = "\"example\"";
...

OR

myWiFiConfig.preSharedKey = "0a0b0f62170ecc5bcf721b6ff170b8b560101b5d56b00a26abec217e0bb4aa1f";

For all the rest of you that will stumble on this the right way is:

Copy&Paste it as is and save your self half a day of pain we already spent on it (Special Thanks to Reflog)

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"SSIDName\"";
wc.preSharedKey  = "\"password\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;        
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = wifi.addNetwork(wc);
Log.d("WifiPreference", "add Network returned " + res );
boolean b = wifi.enableNetwork(res, true);        
Log.d("WifiPreference", "enableNetwork returned " + b );
Community
  • 1
  • 1
Boris Daich
  • 2,431
  • 3
  • 23
  • 27
  • 1
    Hi, i am using the same code for creating wifi configuration and to connect to newly created wifi access point. but when i once connect to the wifi network and then after rebooting the device my wifi configuration does get remembered by the android, do you know any way which we can tell Android that he needs to remember this wifi configuration through out the runs. – User7723337 Mar 18 '12 at 07:29
  • i tried the same code but not getting coonected. When i see the wifi settings i see that for that particular accesspoint the status is shown as "rememberd secured with WPA/WPA2 PSK" . When i try to mannually coonect then it doesnt ask for password but do not connect. Please help – png Mar 25 '12 at 08:11
  • 1
    @A_user Add this to the end: `wifi.saveConfiguration();` – Jeremy Logan Aug 23 '13 at 22:16
  • FYI, since API 26, there is no longer any need to call saveConfiguration() because addNetwork() automatically updates the configuration. – Oke Uwechue Apr 24 '20 at 16:48
6

thanks, all i can user your code conncet to my wpa psk wifi.

   WifiConfiguration wc = new WifiConfiguration();
    // This is must be quoted according to the documentation 
    // http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#SSID
    wc.SSID = "\"zpoint\"";
    wc.preSharedKey  = "\"sipisP@ssw0rd!\"";
    wc.hiddenSSID = true;
    wc.status = WifiConfiguration.Status.ENABLED;        
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    int res = wifi.addNetwork(wc);
    Log.d("WifiPreference", "add Network returned " + res );
    boolean b = wifi.enableNetwork(res, true);        
    Log.d("WifiPreference", "enableNetwork returned " + b );

early, I input the error password, but later I correct password then it works.

Chris
  • 2,471
  • 25
  • 36
poe
  • 77
  • 1
  • 4
3

You will have to add bellow line in order to:

wifi.saveConfiguration();
Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
sl96314
  • 31
  • 1