4

I am building an app that can use WifiManager to automagically add/connect our WiFi hosting hardware, so that the user can easily get to its webinterface.

The wifiManager.enableNetwork(i.networkId, true); part works. It returns immediately and in a few seconds wifi connects. But the webinterface activity is started right away and the WebView quickly timeouts: page could not be loaded.

This is because the WiFi connection takes a few seconds. I am also unable to inform the user about failed connection attempts.

I read up on this and it seems to me that a BroadcastReceiver could "poke" my wifi-connect activity into starting the webinterface activity. A bonus would be to also visualize a failed connect.

So, my questions:

  • I recon i need a BroadcastReceiver for WifiManager.SUPPLICANT_STATE_CHANGED_ACTION or WifiManager.NETWORK_STATE_CHANGED_ACTION ?
  • When to register and unregister it?
  • Can/may i just put a loop to check a statemachine (and timeout) in between, or would that be bad design and why?

I hope this can be done while staying inside my app instance .. or would that inherently result in bugs when exiting the app while wifi is trying to connect?

edit:

As for code; i use WifiManager exactly like so: How do I connect to a specific Wi-Fi network in Android programmatically?

Targeting Android 4.1.2, and am using fragments in activities to recycle view/state instances.

Community
  • 1
  • 1
Barry Staes
  • 3,890
  • 4
  • 24
  • 30
  • The connection part - some fresh answers might appear [here](http://stackoverflow.com/q/39101640/1034042) - if not, follow links in question. Determining failure? No idea, timeout maybe? – kaay Aug 23 '16 at 13:25

1 Answers1

0

You should listen to WifiManager.NETWORK_STATE_CHANGED_ACTION and check on the NetworkInfo object contained in the Intent that your are connected.

When to register : it's difficult to say without seing your code, but you could probably register just before calling wifiManager.enableNetwork

Pierre Rust
  • 2,474
  • 18
  • 15