In my Android application, it is
noticed that when the device goes to sleep/standby WiFi is
disconnected. When the device wakes up, it gets reconnected. Before making a
httpClient.execute(..) call to remote server we check if the device is connected to n/w.
When the data transfer is being done and if
the device goes to sleep then Android runtime will switch to another
medium for connectivity(3G,GPRS etc.).
Is the switch from WiFi to alternate cellular service
say 3G, seamless?
How do I wait for WiFi to become available again? Should I use Thread.sleep(delay) when the WiFi wakes up? I have seen broadcast actions when the WiFi state changes.
In general, what is the ideal approach to handling WiFi disconnects in a mobile app?
Why not use WifiManager.WifiLock when the transfer is happening and release it when you have finished.
I would suspect network connection switch would not be seamless. I don't know for sure.
A BroadcastReceiver will let you know when Wifi connection state changes. Have a look at ConnectivityManager though because that will monitor Wifi and GPRS etc and it does do failover. Whether it is seamless though I don't know.
Related
To my understanding, one can request the WifiManager to start an AP scan, which is great and you get scan results back, but my question is regarding the continuous wifi scan that happens under the hood.
Aside from unregistering the wifi listener for scan callback, is disabling the wifi the only way to stop the hardware from scanning? So if my device is connected to some Wifi BSSID/SSID it's ALSO constantly scanning? (Yes http://www.androidauthority.com/community/threads/how-to-get-wifi-to-stop-scanning-after-connected.7760/) So if I request a scan while connected to some Wifi, it starts to constantly scan since there's no way to actually stop the scanning without turning off the hardware... at which point you lose connectivity with the Wifi Network, NOT acceptable.
Seems like an oversight from Google. Why didn't they just leave the hardware alone rather than make it constantly scan?! Unless provoked to start scanning, connect, disconnect, or stop scanning, DO NOT DO ANYTHING on your own... why didn't they implement it this way? My concern is battery drainage with continuous scanning... whether or not I have a listener is irrelevant. The fact that the hardware is constantly querying for nearby networks sounds pretty resource and battery intensive.
Is there anything one can do in this case without rooting?
I am trying to build an application/service that will execute some coding everytime I connect to a specific wifi network (to log into that network and then keep the connection active ). I do already have the code for the login part and it is working fine, but I don't really know how I can automatically run this code when I connect to a network.
Ideally, I would like to build a service that starts when I join a specific wifi network, keeps the connection active (like makes a request every 10 min to keep the connection active) and finally stops when I leave that network.
I know how to build the service and keep it active, but don't know how to start and stop it automatically when joining / leaving a wifi network. Any suggestions on how to do this is appreciated. If my approach is not correct and there is a better/simpler way to do that, please let me know
Regards,
Marcel
you need a broadcast receiver.
this broadcast receiver will receives broadcasts of Wifi connected and wifi disconnected actions and from there you can start and stop your service.
Implement a BroadcastReceiver listening for the scan results from WifiManager.startScan()
Have a look at Android Scan for Wifi networks
My objective is to be able to scan changes in Wi-Fi networks (mainly to check what networks are available). Right know I'm doing it by registering a reciever:
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
where WifiReceiver is my BroadcastReceiver.
The problem is that it works only when the phone is on. As soon as it goes to sleep mode (either by pushing the power button or after some time), the receiver stops receiving. I already acquired a wake lock in onCreate of my main class and releasing it in onDestroy (it's a partial wake lock). Additionally I've tried this:
Settings.System.putInt(getContentResolver(),Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);
to keep wifi from sleeping. Unfortunately that didn't help.
Is there any possibility to scan for network changes, even when phone is asleep? I want to be able to check what networks are in range (by SSID). Maybe I should use another action?
Appreciate your help.
Literally, no, the hardware isn't typically designed nor the low level drivers written to allow wifi to operate while the application CPU is in sleep mode - unlike the case with the mobile network interface which is intended to receive push events like phone calls, sms, and gmail "tickles".
So your choices are to either manage to successfully prevent the device from sleeping (good bye battery) or wake up periodically using an alarm, kick the wifi active, and poll the situation (still not good for battery life, but not as drastically so).
I'm afraid this sleep behaviour is managed by the system under (on Android 2.3.4) Settings > Wireless & Networks > Wi-Fi Settings > Press Menu > Advanced > Wi-Fi Sleep policy.
This could actually go against the users will, careful with that.
However I think there has to be a way to modify this param. programmatically, using this settings : Settings.System, and of course the corresponding permission in your manifest.
Like many apps my app depends on WIFI and Http Connection to operate. What I am not clear on is how I can time a wait until WIFI is enabled AND device is attached to WIFI network is. What is the best way to delay on application startup long enough for this to happen? during this time I would for example keep buttons deactivated. Also is there any way to make the device attempt to connect to its preferred network? Thanks
Take a look at ConnectivityManager and WiFiManager. Using these you can get events when network state changes
Use WifiManager to get the state of the connection.
Generally you will have to try. A Wifi connection can show as connected but not be able to actually send/receive because the signal is too weak. Once the connection is up, the app should try connecting and only when it succeeds, the buttons should be activated and so forth.
By default, Wifi sleep policy is "Sleep on screen idle".
With this policy, is it possible for a Background Service at a later time to wake up Wifi using some API?
Am trying the following, but does not work:
When my Background Service wakes up, it calls "ConnectivityManager.getActiveNetworkInfo()" to get active network.
Since, the wireless is off on idle, I tried waking it up using "WifiManager.startScan" on a previously used Wifi connection.
But still dont get Wifi connectivity.
Any ideas?
I preferably do not want to change my sleep policy to "Never".
Thanks
Hemant
There are no real simple solutions for this. To with a high probability ensure you have WIFI connectivity when the phone/screen goes to sleep the best way is to turn it off. Look here for a lot of details - http://wififixer.wordpress.com/
It is important to realize that in sleep mode the Wifi enters a low power mode. This will become tricky then to programmatically check as it might have connectivity to the Wifi but the Wifi connection is too weak or too slow to complete the HTTP request and hence it times out. This would force you to also check the speed of the Wifi connectivity as well as you will have an active network but a pretty lousy one.
Proper handling of the escaping when timeout occurs for the HTTP call you make makes it ok to use but ultimately the only way to have a background thread constantly running to get data is only doable when you have the Wifi mode to never sleep.
It is tricky and not the best way I know. :-( It is however the only path I have found which is reliable enough.