Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wait until wifi connected on android

Tags:

java

android

wifi

I have a small program that trying to connect to a wifi network. It enables the wifi on the device then if it's the first time that is connect to the certain networkss It loads the device wifi in order to select and add the password for connection. Until I add the password in order to connect the program should not be finished. How can I add something to wait until I get from the wifi manager that is connected? I try sleep but it freeze the application and am not getting the wifi pop up menu to connect? are there any other ways?

like image 802
prokopis Avatar asked Dec 30 '11 10:12

prokopis


People also ask

What does Wi-Fi waiting mean?

The "Waiting for Wi-Fi" message shows when your download settings are set to Wi-Fi Only but a Wi-Fi connection isn't available. To download using your mobile network instead of Wi-Fi: From the "Waiting for Wi-Fi" message, select Download Settings or Download Now.

Does Android automatically connect to Wi-Fi?

When you have Wi-Fi turned on, your phone automatically connects to nearby Wi-Fi networks you've connected to before. You can also set your phone to automatically turn on Wi-Fi near saved networks. Important: You're using an older Android version.


1 Answers

I have found the solution for your problem a month ago, just use Thread put method isConnected() in it.
In this case, I use WifiExplorerActivity to display all wifi network and allow user connect to it.

        Thread t = new Thread() {
        @Override
        public void run() {
            try {
                    //check if connected!
                while (!isConnected(WifiExplorerActivity.this)) {
                    //Wait to connect
                    Thread.sleep(1000);                     
                }

                Intent i = new Intent(WifiExplorerActivity.this, MainActivity.class);
                startActivity(i);

            } catch (Exception e) {
            }
        }
    };
    t.start();

And this is function to check wifi has connected or not:

  public static boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = null;
    if (connectivityManager != null) {
        networkInfo = connectivityManager.getActiveNetworkInfo();
    }

    return networkInfo != null && networkInfo.getState() == NetworkInfo.State.CONNECTED;
}

Finally, make sure your Androidmanifest.xml look like this:

    <activity android:name=".WifiExplorerActivity" >        
    <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
</activity>

Also, you can use ProgressDialog to wait connect. See http://developer.android.com/guide/topics/ui/dialogs.html

like image 176
ductran Avatar answered Sep 29 '22 10:09

ductran