0

I'm developing an app with a built in feature to connect to a Wi-Fi access point. After a user has entered the password to the access point they want to connect, the code below is executed. My problem is that even after my android device successfully connects to an AP, after ~2 seconds, the if statement is run. Or sometimes it fails to connect, but the else statement runs. What am I doing wrong? Is there a simpler way to accomplish what I am trying to do?

    final WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if (wifiMgr.isWifiEnabled()) {
                WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

                if (wifiInfo.getNetworkId() == -1) {
                    Log.v("rht", "Problems connecting. Try again.");
                    Toast.makeText(NetworkScanner.this, "Problems connecting. Try again.", Toast.LENGTH_LONG).show();
                }
                else {
                    Log.v("rht", "Successfully Connected.");
                }
            }
        }
    }, 4000);

3 Answers3

1

You can do like this..

final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        //Do something after 200ms
      }
    }, 200);

This solution is useful only on UI thread. Otherwise on normal thread, you need to implement looper which is not the best version..

for more information you can visit this link

Vikas singh
  • 3,461
  • 3
  • 14
  • 28
  • Im still getting an incorrect output. For example, if the connection is unsuccessful, I will see "Successfully Connected." I will post the solution as soon as I figure it out. Thank you anyways =). – Hernan Duran Jan 25 '18 at 06:26
0

Here is to init Handler object

 Handler mHandler = new Handler();
    mHandler.posttoDealy(run,1000*10) // 10 Sec

Call to run thread for your task

Runnable run = new Runnable() {
      @Override
      public void run() {

//this will run after ten Sec

//mHandler.posttoDealy(run,1000*10) // 10 Sec for again and again loop

      }
    })

To Remove Handler

mHandler.removeCallback(run)
Deepak Ror
  • 2,084
  • 2
  • 20
  • 26
0

Using Supplicant state did the charm. For some reason "wifiInfo.getNetworkId() == -1" was giving me unpredicted results. Eventually I found a different method to check if my android device is connected to a network. Lots of thanks to Donal Rafferty and Armand. They both contributed to this SO question

    final WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            if (wifiMgr.isWifiEnabled()) {

                SupplicantState supState;
                supState = wifiInfo.getSupplicantState();

                if(supState != SupplicantState.COMPLETED) {
                    Log.v("rht", "Problems connecting. Try again.");
                }
                else {
                    Log.v("rht", "Successfully Connected.");
                }
            }
        }
    }, 4000);