Not sending/receiving udp packets when the phone's screen is blocked Android

532 Views Asked by At

I'm in a problem sending and receiving UDP packets in Android. I've to send them periodically, and everything goes fine when both telephones are with their screens on.

When I turn off the screen, the phone that sends messages stops sending them, then, when I touch the screen again, it starts to send messages again, but if the phone that is receiving is with the screen off it doesn't receive anything.

Lets suppose two phones A is receiving and B is sending:

1 - Both of them have the screen on.

2 - I turn off B's screen --> B doesn't send messages

3 - I turn on B's screen --> B starts to send messages again

4 - I turn off A's screen --> B is sending, but A isn't receiving anything

5 - I turn on A's screen --> everything works fine again.

Does anyone knows if there is a kind of problem with UDP send/reception in Android? (for example in iOS the reception over UDP is blocked when the screen is off). I've read that there is a problem in reception for UDP broadcast packets, but in this case there are not broadcast, they were sent to a particular IP and port.

Thanks in advance!

EDIT:

Just another question, is there any aditional consideration when we use mobile network? With wakelock everything works fine when I'm with wifi, but when I'm with 3g I can't receive anything (but the other phone is sending with 3g). Is there a restriction in receiving UDP packets in a mobile network? or it should work fine as well?

1

There are 1 best solutions below

0
On BEST ANSWER

In android if you turn screen off, the processor goes to sleep. In this state only the necessary functions (phone call, sms, etc.) work. You can use however wakelock, this way you can prevent your phone from sleep, and your code will be executed.