android - detecting disconnection from a WiFi access point -
i trying use broadcastreceiver detect when phone has disconnected wifi access point. this, registered broadcastreceiver in manifest:
<receiver android:name="com.eshayne.android.wifibroadcastreceiver"> <intent-filter> <action android:name="android.net.wifi.state_change" /> </intent-filter> </receiver>
in wifibroadcastreceiver class, checking network_state_changed_action action , looking @ networkinfo's detailed state:
if (action.equals(wifimanager.network_state_changed_action)) { networkinfo info = (networkinfo)intent.getparcelableextra(wifimanager.extra_network_info); android.util.log.d("com.eshayne.android.wifibroadcastreceiver", "network state change - detailedstate=" + info.getdetailedstate() + ": " + info.tostring()); if (info.getdetailedstate() == detailedstate.disconnected) { ... } else if (info.getdetailedstate() == detailedstate.connected) { ... }
the problem i'm seeing when phone leaves wifi access point's range, "disconnected" callback gets called 6 times - pretty regularly @ once every 15 seconds - before stops. far haven't been able find distinguishing characteristics between each callback's networkinfo. each networkinfo object being written log looks this:
02-18 10:16:51.918 d/com.eshayne.android.wifibroadcastreceiver( 1511): network state change - detailedstate=disconnected: networkinfo: type: wifi[], state: disconnected/disconnected, reason: (unspecified), extra: (none), roaming: false, failover: false, isavailable: true
it's not issue of phone wandering in , out of wifi range, "connected" callback not called in between "disconnected" callbacks. nor other states being triggered in between. rapid series of 6 callbacks each detailedstate of disconnected.
is there better way me detect when phone has lost wifi connection, callback gets called once per disconnect? or way detect of 6 callbacks i'm seeing "final" one?
you it's "rapid series of 6 disconnected callbacks", if/else-if checks disconnected , connected, looks no default block handle other cases. networkinfo.detailedstate api page, there's 10 possible states returned networkinfo.getdetailedstate(), including "connecting", "scanning", "disconnecting", of plausible behavior phone got disconnected network.
throw down default case alerts any change in wifi state, not "connected" , "disconnected". might find phone rotating between several different states, , not shotgunning same 1 @ 6 times. there, how proceed in code little clearer.
Comments
Post a Comment