title: Wifi sleeps, even with Lock
body: <p>Summary: even when wifi lock is acquired, when the phone is running on batteries, WiFi is disconnected after a while.</p>
  
  <p>I've simplified the problem to a single activity with a button that launches a thread. It just sends 100.000 strings to an echo server running on a PC (one string every 100ms). See code below. I can see the traffic with WireShark, and also the echo server shows the strings. Notice how WiFi and power locks are acquired before starting to send (and released after, of course).</p>
  
  <p>However, when the phone is running on battery and the user turns off the phone, it keeps sending strings for some time and then WiFi is disconnected and the phone does not even respond to ping. It takes from 600s to 6000s to be disconnected (the figures are that round, so I think they are important).</p>
  
  <p>It perfectly works when A/C is connected, so I guess it is somehow related to power management.</p>
  
  <p>To test it I just launch the activity, start the echo server, start WireShark, press the "Start" button (<code>android:onClick="doStart"</code>), blocks the phone and let it on the table. I go for lunch or whatever and after 600-6000s I can see the tx errors on WireShark, the echo server has stopped receiving traffic and the phone does not respond to ping.</p>
  
  <p>The phone is 2.2, with WiFi policy set to "sleep after 15m".</p>
  
  <pre><code>package Odroid.test;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintStream;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.Date;
  
  import android.app.Activity;
  import android.content.Context;
  import android.net.wifi.WifiManager;
  import android.os.Bundle;
  import android.os.PowerManager;
  import android.view.View;
  import android.widget.Button;
  
  public class Test extends Activity {
    PowerManager _powerManagement = null;
    PowerManager.WakeLock _wakeLock = null;
    WifiManager.WifiLock _wifiLock = null;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    }
  
    public void doStart(View v) {
      DoerThreadFake t = new DoerThreadFake();
      t.start();
    }
  
    private class DoerThreadFake extends Thread {
      public void run() {
        runOnUiThread(new Runnable() {
          public void run() {
            ((Button) findViewById(R.id.start)).setText("Doing...");
          }
        });
        _keepOnStart();
        Socket s;
        byte[] buffer = new byte[1000];
  
        try {
          s = new Socket("192.168.0.16", 2000);
          PrintStream ps = new PrintStream(s.getOutputStream());
          InputStream is = s.getInputStream();
          for (int i = 0; i &lt; 100000; i++) {
            ps.println(System.currentTimeMillis() +"("+(new Date()).toString() +") : " + i);
            try {
              Thread.sleep(100);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            while (is.available() &gt; 0) {
              int a = is.available();
              if (a &gt; 1000) a = 1000;
              is.read(buffer, 0, a); // Clean echo
            }
          }
        } catch (UnknownHostException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        _keepOnStop();
        runOnUiThread(new Runnable() {
          public void run() {
            ((Button) findViewById(R.id.start)).setText("Done");
          }
        });
      }
  
      private void _keepOnStart() {
        if (_powerManagement == null) {
          _powerManagement = (PowerManager) getSystemService(Context.POWER_SERVICE);
        }
        if (_wakeLock == null) {
          _wakeLock = _powerManagement.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
              "0 Backup power lock");
        }
        _wakeLock.acquire();
        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
          _wifiLock = wifiManager.createWifiLock("0 Backup wifi lock");
          _wifiLock.acquire();
        }
      }
  
      private void _keepOnStop() {
        if ((_wifiLock != null) &amp;&amp; (_wifiLock.isHeld())) {
          _wifiLock.release();
        }
        if ((_wakeLock != null) &amp;&amp; (_wakeLock.isHeld())) {
          _wakeLock.release();
        }
      }
    }
  }
  </code></pre>
  
  <p>The manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="Odroid.test"
    android:versionCode="1"
    android:versionName="1.0"
  &gt;
    &lt;uses-sdk android:minSdkVersion="4" /&gt;
  
    &lt;application
      android:icon="@drawable/icon"
      android:label="@string/app_name"
    &gt;
      &lt;activity
        android:name=".Test"
        android:label="@string/app_name"
      &gt;
        &lt;intent-filter&gt;
          &lt;action
            android:name="android.intent.action.MAIN" /&gt;
          &lt;category
            android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
    &lt;/application&gt;
    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
    &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
    &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Any idea?</p>