Search code examples
javaandroidsocketswifiwifimanager

Android: Wifi is turned off even with WifiLock


I have a foreground service that runs as long as I have a Wifi connection. During the duration of the service it holds a WifiLock. Everything works fine until I turn off the screen and wait about 15 minutes. My socket that I hold then throws a connection timeout exception because the Wifi disconnects (I see it in the logs and my android.net.wifi.STATE_CHANGE broadcast receiver is started). The Wifi then proceeds and gets a new connection.

Bwt the same thing happens if I remove my WifiLock and instead change the Wifi sleep policy to "never". But without either the wifi is just turned off until the screen is unlocked.

Couldn't find anything that solved this problem, any suggestion/workarounds are appreciated. If not, my service will just use the new connection and I will have to build around this issue.

Using a Galaxy Tab.


Solution

  • As ahmedre noted this is a bug with Android. I now create a response from the client to make sure the message was received. Otherwise try again momentarily. This is to get around the problem of sending a message when the Wifi just died.

    Update

    I have not seen this particular issue on newer devices. Originally this behavior occured on the Galaxy Tab.