0

I am new in Android network programming. I am developing an app in which I want to enable and connect wifi programmatically. I searched so many tutorials but I get most probeb same code as I written below. But in my case I am always getting res value -1.

    WifiConfiguration wifiConf = new WifiConfiguration();
    wifiConf.SSID = '\"' + bestHotspot.SSID + '\"';
    wifiConf.hiddenSSID = true;
    wifiConf.status = WifiConfiguration.Status.ENABLED;
    wifiConf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
    wifiConf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
    wifiConf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
    wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
    wifiConf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    int res = wPoint.wifiManager.addNetwork(WifiConf);
    Log.d("WPoint WIFI", "add Network returned " + res);
    boolean b = wPoint.wifiManager.enableNetwork(res, true);
    Log.d("WPoint WIFI", "enableNetwork returned " + b);
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Try following [this](http://www.viralandroid.com/2015/12/turn-on-and-off-wifi-connection-programmatically-in-android.html) tutorial, and don't forget about permissions. You'll get it right! – jorjSB Nov 28 '17 at 09:02
  • https://stackoverflow.com/questions/8818290/how-do-i-connect-to-a-specific-wi-fi-network-in-android-programmatically – SahdevRajput74 Nov 28 '17 at 09:06

1 Answers1

0

You need to create WifiConfiguration instance like this:

String networkSSID = "test";
String networkPass = "pass";

WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";   
// Please note the quotes.String should contain ssid in quotes
Then, for WEP network you need to do this:

conf.wepKeys[0] = "\"" + networkPass + "\""; 
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 
For WPA network you need to add passphrase like this:

conf.preSharedKey = "\""+ networkPass +"\"";
For Open network you need to do this:

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Then, you need to add it to Android wifi manager settings:

WifiManager wifiManager = 
(WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetwork(conf);
And finally, you might need to enable it, so Android connects to it:

List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
    if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
         wifiManager.disconnect();
         wifiManager.enableNetwork(i.networkId, true);
         wifiManager.reconnect();               

         break;
    }           
 }
UPD: In case of WEP, if your password is in hex, you do not need to 
 surround it with quotes.
Jackey kabra
  • 199
  • 1
  • 9
  • 1
    But i am still confused addNetwork(wifiConfigration) returning -1. becaus most of the cases peoples says it will return connected network id. – Vijay Kaushik Nov 28 '17 at 10:01