networking - Understanding Android's Wifi State -


i need check if device connected wifi network. events occur confuse me. here steps reproduce:

registerreceiver

so registered receiver action wifimanager.network_state_changed_action):

private void registerreceiver() {     log.v(tag, "registerreceiver");      final intentfilter intentfilter = new intentfilter();     intentfilter.addaction(wifimanager.network_state_changed_action);      broadcastreceiver = new connectivitybroadcastreceiver();     context.registerreceiver(broadcastreceiver, intentfilter); } 

onreceive

and how receive events.

private class connectivitybroadcastreceiver extends broadcastreceiver {     @override     public void onreceive(context context, intent intent) {         log.v("broadcast", "onreceive: " + intent.getaction());         final networkinfo networkinfo =                 intent.getparcelableextra(wifimanager.extra_network_info);          if (networkinfo != null) {             final networkinfo.state state = networkinfo.getstate();             final networkinfo.detailedstate detailedstate = networkinfo.getdetailedstate();              log.d(tag, "networkinfo.state: " + state);             log.d(tag, "networkinfo.detailedstate: " + detailedstate);         }          final wifimanager wifimanager = (wifimanager) context.getsystemservice(context.wifi_service);         final wifiinfo wifiinfo = wifimanager.getconnectioninfo();          final supplicantstate supplicantstate = wifiinfo.getsupplicantstate();         final networkinfo.detailedstate detailedstate = wifiinfo.getdetailedstateof(supplicantstate);          log.d(tag, "supplicantstate: " + supplicantstate.tostring());         log.d(tag, "detailedstateofsupplicant: " + detailedstate.tostring());     } } 

user input

  1. i start app, pull android menu down above, open wifi settings (by holding wifi symbol) , connect wifi network. return app.

or

  1. i start app, pull android menu down above, active wifi quick settings. return app. device automatically connects known wifi.

output

first receive events connecting , authenticating. seems normal here.

but output four times in row during one setup:

onreceive: broadcast android.net.wifi.state_change networkinfo.state: connected networkinfo.detailedstate: connected supplicantstate: completed detailedstateofsupplicant: obtaining_ipaddr 
  1. why these 4 events triggered?

that supplicant state completed gets mapped obtaining_ipaddr seems logic according documentation:

supplicantstate completed:
state indicates supplicant has completed processing association phase , data connection configured. note, however, there may not ip address associated connection yet. typically, dhcp request needs sent @ point obtain address.

  1. but why network info state , detailed state set connected if device obtaining it's ip address?

  2. how can ensure device "really" connected, in other words, how know when device obtained it's ip address , when network ready use? there better action can register upon?

sources

  • an overview of networkinfo.state , networkinfo.detailedstate can found here.
  • an overview of supplicantstate can found here.

check network state using below class:

 public class connectivity {     public static networkinfo getnetworkinfo(context context){         connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service);         return cm.getactivenetworkinfo();     }       public static boolean isconnected(context context){         networkinfo info = connectivity.getnetworkinfo(context);         return (info != null && info.isconnected());     }      public static boolean isconnectedwifi(context context){         networkinfo info = connectivity.getnetworkinfo(context);         return (info != null && info.isconnected() && info.gettype() == connectivitymanager.type_wifi);     }       public static boolean isconnectedmobile(context context){         networkinfo info = connectivity.getnetworkinfo(context);         return (info != null && info.isconnected() && info.gettype() == connectivitymanager.type_mobile);     }       public static boolean isconnectedfast(context context){         networkinfo info = connectivity.getnetworkinfo(context);         return (info != null && info.isconnected() && connectivity.isconnectionfast(info.gettype(),info.getsubtype()));     }       public static boolean isconnectionfast(int type, int subtype){         if(type== connectivitymanager.type_wifi){             return true;         }else if(type==connectivitymanager.type_mobile){             switch(subtype){                 case telephonymanager.network_type_1xrtt:                     return false; // ~ 50-100 kbps                 case telephonymanager.network_type_cdma:                     return false; // ~ 14-64 kbps                 case telephonymanager.network_type_edge:                     return false; // ~ 50-100 kbps                 case telephonymanager.network_type_evdo_0:                     return true; // ~ 400-1000 kbps                 case telephonymanager.network_type_evdo_a:                     return true; // ~ 600-1400 kbps                 case telephonymanager.network_type_gprs:                     return false; // ~ 100 kbps                 case telephonymanager.network_type_hsdpa:                     return true; // ~ 2-14 mbps                 case telephonymanager.network_type_hspa:                     return true; // ~ 700-1700 kbps                 case telephonymanager.network_type_hsupa:                     return true; // ~ 1-23 mbps                 case telephonymanager.network_type_umts:                     return true; // ~ 400-7000 kbps                 case telephonymanager.network_type_ehrpd: // api level 11                     return true; // ~ 1-2 mbps                 case telephonymanager.network_type_evdo_b: // api level 9                     return true; // ~ 5 mbps                 case telephonymanager.network_type_hspap: // api level 13                     return true; // ~ 10-20 mbps                 case telephonymanager.network_type_iden: // api level 8                     return false; // ~25 kbps                 case telephonymanager.network_type_lte: // api level 11                     return true; // ~ 10+ mbps                 // unknown                 case telephonymanager.network_type_unknown:                 default:                     return false;             }         }else{             return false;         }     } } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -