0

I am having an issue when try to connect to wireless network. My code works ok I sucessfully connect to the network whit the problems described below.

I have a listview whit the wifi scan results.

When I click the first time my receiver is not getting the "completed" state. After clicking the second time , and , without chosing any network it get connected and my code inside the "complete" is executed.

The code below is called from another class that thre reason why it is static

Coonect Code:

 public static boolean connect(String ssid,String password)
{

    String networkSSID = ssid;
    String networkPass = password;
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";
    conf.preSharedKey = "\""+ networkPass +"\"";
    //WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    int netid=mainWifi.addNetwork(conf);
    mainWifi.disconnect();
    mainWifi.enableNetwork(netid, true);

    //mainWifi.reconnect(); <-- exact the same issue discommenting this line
 return true;
}

On the class were the connect is been called I have registered BradcastReceiver as follow:

 public void onClick(View v)
        {

            mainWifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            wifiinfo = mainWifi.getConnectionInfo();
            AuthWifi authWifi = new AuthWifi();
            IntentFilter mIntentFilter = new IntentFilter();
            mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
            getApplicationContext().registerReceiver(authWifi, mIntentFilter);
            ClientManager.scan(CameraActivity.this, mainWifi);
        }

my broadcast receiver

public class AuthWifi extends BroadcastReceiver {
    @Override
    public void onReceive(Context c, Intent intent) {
        String action = intent.getAction();
        if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {

            SupplicantState supl_state = ((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
            switch (supl_state) {

                case COMPLETED:
                  /////////////IF I AM CONNECTED THE WIFI SSID I CHOSE FROM A LISTVIEW THEN ---->
                    if(wifiinfo != null & wifiinfo.getSSID()!=null & ClientManager.getSSID() !=null& !conectado ) {

                        if (wifiinfo.getSSID().contains(ClientManager.getSSID().trim())) ///I know here is better .equals() /// I have contain for my own reasons
                            conectado = true;

/*HERE I DO SOME THINGS WHEN CONNECTED (I CALL A RUNNABLE TO MAKE A SERVER SOCKET)*/


                        }
                    }
                    break;
                case DISCONNECTED:
                    Log.i("SupplicantState", "Disconnected");

                    conectado = false;
                    if (ClientStartReceive.isStopped)
                    {
                        ClientStartReceive.stop();
                    }
                    break;

                default:
                    Log.i("SupplicantState", "Unknown");
                    break;

            }
            int supl_error = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
            if (supl_error == WifiManager.ERROR_AUTHENTICATING) {

 /////HERE I MANAGE AUTHENTICATION ERROR

            }
        }
    }
}

Hope someone is able to help :( if you need more code to troubleshoot please let me know. If you have some reference to help me even if i need to rebuild the code is accepted also.. My goal is be able to connect to a network ,show for authentication errors and execute some code on connection susscess. Sorry for my english I think you have gessed I am not native. Regards

Noel Carcases
  • 711
  • 1
  • 7
  • 27
  • The broad cast receiver might be take more time to trigger after any SUPPLICANT_STATE_CHANGED_ACTION occurred . take a look at http://stackoverflow.com/questions/30889089/android-connect-to-open-wifi-programmatically-by-name-which-is-best-solution/30889687#30889687 and http://stackoverflow.com/questions/31284285/android-wifi-broadcast-receiver-not-working-as-expected/31284820#31284820 – Anoop M Maddasseri Jul 21 '15 at 07:11
  • I am almost sure I have fixed it by registering the receiver oncreate . I will confirm when tested – Noel Carcases Jul 21 '15 at 11:22

1 Answers1

0

As doc indicate that “COMPLETED“ use as follow:

This state indicates that the supplicant has completed its processing for the association phase and that data connection is fully configured. Note, however, that there may not be any IP address associated with the connection yet.

You should not rely this state to ensure your connection is completed. Instead, you can register an BroadcastReceiver listener for network status change.

qianlv
  • 502
  • 5
  • 15