java - android connecting to wifi WifiManager.SUPPLICANT_STATE_CHANGED_ACTION strange issue -


i having issue when try connect wireless network. code works ok sucessfully connect network whit problems described below.

i have listview whit wifi scan results.

when click first time receiver not getting "completed" state. after clicking second time , , , without chosing network connected , code inside "complete" executed.

the code below called class that thre reason why 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 same issue discommenting line  return true; } 

on class connect been called have registered bradcastreceiver 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 connected wifi ssid chose listview ---->                     if(wifiinfo != null & wifiinfo.getssid()!=null & clientmanager.getssid() !=null& !conectado ) {                          if (wifiinfo.getssid().contains(clientmanager.getssid().trim())) ///i know here better .equals() /// have contain own reasons                             conectado = true;  /*here things when connected (i call runnable make 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 manage authentication error              }         }     } } 

hope able :( if need more code troubleshoot please let me know. if have reference me if need rebuild code accepted also.. goal able connect network ,show authentication errors , execute code on connection susscess. sorry english think have gessed not native. regards

as doc indicate “completed“ use follow:

this state indicates supplicant has completed processing association phase , data connection configured. note, however, there may not ip address associated connection yet.

you should not rely state ensure connection completed. instead, can register broadcastreceiver listener network status change.


Comments