Android WifiLock not working?

I need to collect data from a server on the local network every x minutes via WiFi and HTTP. The service is running in the background and I am using AlarmManager to run the request. Wifi Sleep Policy is set to never on the device.

The problem is that the device goes into sleep mode when the screen is off, and the Wi-Fi lock does not seem to restore the Wi-Fi connection properly, so the request fails.

Any idea what's wrong here?

Exectution

{...} wakeLockUtil.lock(); //wait for wifi to connect (no idea if this is useful) Thread.sleep(3000); doQuery(); wakeLockUtil.unlock(); {...} 

Wakelock

 public WakeLockUtil(Context context) { wifiLock = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "PeriodUpdateWifiLock"); wakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PeriodUpdateWakeLock"); } public void lock() { System.out.println("WakeLockUtil.lock()"); try { wakeLock.acquire(); wifiLock.acquire(); } catch (Exception e) { Log.e(this.getClass().getSimpleName(), "Error getting Lock: " + e.getMessage()); } } public void unlock() { System.out.println("WakeLockUtil.unlock()"); if (wakeLock.isHeld()) wakeLock.release(); if (wifiLock.isHeld()) wifiLock.release(); } 
0
android wakelock
source share
1 answer

I saw problems on some devices where the combination of PARTIAL_WAKE_LOCK and WifiLock: WIFI_MODE_FULL_HIGH_PERF does not work completely when the screen is off. Very annoying. The only solution for me is to use SCREEN_DIM_WAKE_LOCK .

+4
source share

All Articles