1

I did research, but i cannot find solution to my problem here, actually i don't know what I am doing wrong but enableNetwork always is returning true and even reconnect() method always return true which is weird, even when wrong password is passed.. can you please point me to right direction? I just want to check wifi connection for specific password. Is this right way to do it?

      WifiConfiguration conf = new WifiConfiguration();
                                    conf.preSharedKey = "\""+ networkPass +"\"";
                                    WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
                                    int res = wifi.addNetwork(conf);
                                    wifiManager.disconnect();
                                    boolean b = wifiManager.enableNetwork(res, true);
                                    String str = String.valueOf(b);
                                    Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();

                                    if (wifiManager.reconnect()) {
                                        Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();

                                    } else {
                                        Toast.makeText(getApplicationContext(), "Not Connected", Toast.LENGTH_LONG).show();
                                    }
Nikanor
  • 262
  • 1
  • 5
  • 17
  • Can you clarify your question a bit? Are you having trouble connecting to a network, or having trouble only with the verification of a connection established or not after a password entered? – Daniel Nugent May 21 '15 at 02:02
  • I have trouble connecting, as enableNetwork(res, true) for some reason always return true, even when i pass wrong password, and even reconnect() is returning true, but I am not sure if it is actually connected (should not be because I pass wrong password) – Nikanor May 21 '15 at 02:30
  • What type of authentication is the network you are trying to connect to? Does it work when you give it the correct password? – Daniel Nugent May 21 '15 at 02:33
  • This code is for WPA type authentication, and when given correct password I got the same result , enableNetwork and reconnect giving true as a result, but what is strange is that they are always true, even when given wrong password – Nikanor May 21 '15 at 02:40
  • But do you get successfully connected with the correct password? – Daniel Nugent May 21 '15 at 02:43
  • I assume yes, because reconnect returns true. I followed tutorial from here http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically – Nikanor May 21 '15 at 02:46
  • Can't you just check to see if you're connected successfully or not? Are you testing on a device or an emulator? – Daniel Nugent May 21 '15 at 02:48
  • Sorry if I am being dumb, but how can I effectively check if I am really connected except with reconnect method? I am testing on the device – Nikanor May 21 '15 at 02:57
  • Go to Settings->Wi-Fi, and see if you're connected to Wi-Fi on the SSID that you expect! – Daniel Nugent May 21 '15 at 03:04
  • No, it doesnt show that I am connected, so I assume something is wrong with the code, but reconnect still throw "true".. – Nikanor May 21 '15 at 03:17
  • Are you going off of the accepted answer in the question you linked to? You're missing some code. You can also take a look at my answer here: http://stackoverflow.com/questions/29574730/unable-to-connect-to-new-wifi-network/29575563#29575563 Also, to detect connectivity, take a look at this: http://stackoverflow.com/questions/29709489/weird-behavior-when-trying-to-detect-when-wifi-network-connected-android/ – Daniel Nugent May 21 '15 at 03:58
  • I tried with your answer from the link, but still get the same, I get reconnect() return true always, but there is no connection.. – Nikanor May 21 '15 at 09:08

0 Answers0