Question

I have written some software which uses Wifi as a possible internet-connection medium.. I did notice though, that after some time (I am guessing around the 15-20 minutes) after the device has gone into power save mode (anyways when the screen has gone black) the Wifi connection will simply be dropped, even though it is still being used :( This on a ADP 2 (thus a Google Ion with Android 1.6)... I was able to get the following Log :

06-10 15:04:27.009: DEBUG/WifiService(72): got ACTION_DEVICE_IDLE
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: ERROR/wpa_supplicant(538): Set_key: Wrong Key
06-10 15:04:27.069: VERBOSE/WifiMonitor(72): Event [CTRL-EVENT-STATE-CHANGE id=0 state=8]
06-10 15:04:27.079: VERBOSE/WifiStateTracker(72): Changing supplicant state: COMPLETED ==> DORMANT
06-10 15:04:27.079: DEBUG/WifiStateTracker(72): Deconfiguring interface and stopping DHCP
06-10 15:04:27.099: VERBOSE/WifiMonitor(72): Event [CTRL-EVENT-DISCONNECTED - Disconnect event - remove keys]
06-10 15:04:27.099: VERBOSE/WifiMonitor(72): Event [CTRL-EVENT-STATE-CHANGE id=-1 state=8]
06-10 15:04:27.139: WARN/Smack/Packet(169): notify conn break (IOEx), close connection
06-10 15:04:27.139: DEBUG/Smack(169): [XMPPConn] close connection, notifyClosed=false
06-10 15:04:27.139: ERROR/MediaPlayer(390): error (1, -17)
06-10 15:04:27.139: ERROR/MediaPlayer(390): Error (1,-17)
06-10 15:04:28.109: VERBOSE/WifiMonitor(72): Event [CTRL-EVENT-DRIVER-STATE STOPPED]
06-10 15:04:28.129: VERBOSE/WifiStateTracker(72): New network state is DISCONNECTED
06-10 15:04:28.129: VERBOSE/WifiStateTracker(72): Changing supplicant state: DORMANT ==> DORMANT
06-10 15:04:28.189: INFO/MediaUploader(199): No need to wake up
06-10 15:04:28.189: DEBUG/GpsLocationProvider(72): updateNetworkState available
06-10 15:04:28.189: DEBUG/GpsLocationProvider(72): NetworkThread wait for 4484259ms
06-10 15:04:28.289: DEBUG/NetworkLocationProvider(72): onDataConnectionStateChanged 3
06-10 15:04:28.299: DEBUG/GpsLocationProvider(72): state: CONNECTING apnName: iinternet reason: null
06-10 15:04:32.979: DEBUG/NetworkLocationProvider(72): onDataConnectionStateChanged 3
06-10 15:04:33.029: DEBUG/GpsLocationProvider(72): state: CONNECTED apnName: iinternet reason: null
06-10 15:04:33.099: DEBUG/GpsLocationProvider(72): updateNetworkState available
06-10 15:04:33.099: DEBUG/GpsLocationProvider(72): NetworkThread wait for 4479355ms
06-10 15:04:33.129: INFO/MediaUploader(199): No need to wake up
06-10 15:04:33.299: INFO/ActivityManager(72): Stopping service: com.android.mms/.transaction.TransactionService
06-10 15:04:33.339: ERROR/TransactionSettings(156): Invalid APN setting: MMSC is empty
06-10 15:04:33.419: INFO/ActivityManager(72): Stopping service: com.android.providers.downloads/.DownloadService
06-10 15:04:38.309: DEBUG/dalvikvm(199): GC freed 301 objects / 19232 bytes in 92ms
06-10 15:04:43.349: DEBUG/dalvikvm(216): GC freed 1430 objects / 84920 bytes in 121ms
06-10 15:04:48.319: DEBUG/dalvikvm(156): GC freed 323 objects / 15152 bytes in 96ms

Is this some kind of known bug/feature ? If so how to get around it ? Also is there someway to actually intercept this EVENT and then soimehow ignore it or re-setup a wifi connection ? Thanks in advance

Was it helpful?

Solution

You need to create a wifi lock. Here is how you do it:

WifiManager wifimanager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
WifiLock lock = wifimanager.createWifiLock("my_lock");

lock.acquire();

// your code here

lock.release();

Note you should only create a wifi lock when you're actively using wifi (i.e. downloading a large file), otherwise you will needlessly impact battery life.

OTHER TIPS

I get a SecurityException when I call WifiLock.acquire(). Am I missing a uses-permission in my manifest or is there something I need to check beforehand in code? I'm assuming it has to do with the advanced settings in the wifi settings page set to NEVER.

Getting a Wi-Fi lock is not the preferred solution to this. The intention of the Wi-Fi lock is to only hold it when you're actively accessing the network, i.e. downloading a file. E.g. Skype and Google HangOut don't take and keep the Wi-Fi lock. You're supposed to do what Martin Molnar said in his comment above. You're supposed to let the user decide if he wants to allow his device (and your App on his device) to stay connected to Wi-Fi when the display is off via the "WiFi awake while in sleep" setting. If you absolutely insist that your App should keep Wi-Fi on even 15min after the display is off, even if the user set "WiFi awake while in sleep" to "no" then you're supposed to take the Partial Wake Lock. Again Skype does not take the Partial Wake Lock.

If you really want to have a slick App that can receive data/notifications asynchronously then you should set up GCM push notifications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top