Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive UDP in Android Marshmallow

I'm having problems to receive constantly the UDP packets from the server with new Nexus 5X (Marshmallow)

I have another real devices that receive all UDP packets, but it seems that something changed in Android API 23.

CODE:

Anyone with the same problem?

                        if (s == null || s.isClosed()){


                            Log.v("udp", "----------------------------------------------------new socket---------------------------------");


                            s = new DatagramSocket(null);
                            s.setReuseAddress(true);
                            s.setBroadcast(true);
                            s.setSoTimeout(5000);
                            s.bind(new InetSocketAddress(8002));
                            p = new DatagramPacket(message, message.length);

                            try{
                                Thread.sleep(100);
                            }
                            catch(Exception e){

                            }
                        }



                        //Log.v("test","---------------------------------------------------- Pas1 ---------------------------------");
                        message = new byte[100];
                        //p = new DatagramPacket(message, message.length);
                        p.setLength(message.length);

                        s.receive(p);

The problem is:

I receive randomly the broadcast packets, meanwhile in other real devices I receive all.

I'm sure that something was changed in android api 23. It seems that the receive function only "triggers" if it has a packet in the buffer, but if it's called without it "inside", the sockettimeout appears.

Thanks!

like image 517
Kotik_o Avatar asked Oct 30 '22 07:10

Kotik_o


1 Answers

Android Marsmallow introduces doze mode, which will suspend network access after some time. Maybe this affects your application as well.

"The following restrictions apply to your apps while in Doze:

- Network access is suspended

- ..."

http://developer.android.com/training/monitoring-device-state/doze-standby.html

The bad news is that you cannot do much about it, except for asking the user to put your application on the list with applications exempted from battery optimization. Be careful with this though, because Google will likely remove your application from the Play store for it (this is why I didn't include source code for it).

"However, it is Google’s opinion of what “the core function of the app” is that matters. Not the user. Not the developer. Google. The user and developer might be in perfect harmony over what adding the app to the whitelist means, but if Google thinks that it is not warranted, Google sets themselves up as being more important than the user and bans the app." (https://commonsware.com/blog/2015/11/11/google-anti-trust-issues.html)

Mostly the only viable option is to use Google Cloud messaging, but that might not be a good solution in your case.

like image 95
M66B Avatar answered Nov 09 '22 14:11

M66B