My Android application periodically sends and receives UDP broadcast messages on the WiFi LAN. When the display is on, everything works fine and all messages are sent and received correctly. When the display goes to sleep the application however stops receiving UDP broadcast messages (but still sends them).
Acquiring the WiFiLock (as well as a Multicast Lock) does not seem to make any difference and my wifi sleep policy is set to WIFI_SLEEP_POLICY_NEVER.
This is an issue on Android 2.3.3 and 2.3.5 (Samsung galaxy SII phones) but not an issue on an old HTC Hero running cyanogen mod with android 2.3.7.
Has anyone else had this problem and found a solution?
This Google Groups thread deals with the problem:
https://groups.google.com/forum/?fromgroups=#!topic/android-platform/OpbSdp9FTmA
It has to do with certain chipsets and deep sleep. Basically, drivers detect when the device goes into a deep sleep and shut down UDP Broadcast Reception. In my experience, UDP send still works and a UDP send/listen on the device will also hear packets, but it's a ghost, the device is actually deaf.
The following recommendation is what I use:
- Acquire a PARTIAL_WAKE_LOCK, and trap when the screen goes off. Then disable and reenable the wifi. This works because the filter only turns on when the screen goes off, so starting wifi with the screen off will keep it working until the screen goes off again. This is what we did before we figured out solution (1).
I use this:
WifiManager wifi;
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
MulticastLock wifilock = wifi.createMulticastLock("just some tag text");
wifilock.acquire();
manifest:
permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"
来源:https://stackoverflow.com/questions/9363389/udp-broadcast-packets-not-received-in-sleep-mode