1

I have a foreground service with a thread that monitors UDP packets. It works perfectly until I leave the phone alone for about 5 minutes (but not exactly 5 minutes), then it stops sending the updates it's getting from UDP packets. This is 100% reproducible, and I can see the UDP packets on the network with a packet capture, so I know they are there.

    private class ArduinoUDPListener extends Thread {
    private MulticastSocket socket;
    private AtomicBoolean done;
    private Instant lastMsgTime;

    public void run() {

        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyApp::MyWakelockTag");
        wakeLock.acquire();


        WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        WifiManager.MulticastLock multicastLock = null;
        multicastLock = wm.createMulticastLock("udp for hamwin");
        multicastLock.acquire();
        WifiManager.WifiLock wifiLock = null;
        wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "wifi lock for ham");
        wifiLock.acquire();

        lastMsgTime = Instant.MIN;

        done = new AtomicBoolean(false);
        try {
            socket = new MulticastSocket(7234);
            InetAddress group = InetAddress.getByName("239.1.1.234");
            socket.joinGroup(group);
            socket.setReuseAddress(true);
            socket.setBroadcast(true);
            if (!socket.getBroadcast()) {
                socket.close();
                return;
            }
            socket.setSoTimeout(100);
            byte[] buf = new byte[65535];
            DatagramPacket p = new DatagramPacket(buf, 65535);
            while (!done.get()) {
                try {
                    socket.receive(p);
                } catch (SocketTimeoutException e) {
                    // retry
                    continue;
                } catch (IOException e) {
                    e.printStackTrace();
                    break;
                }


                // Process packet,
                //    sometimes we just stop getting here after a few minutes of normal activity if the phone is not touched.

                if (mainThreadHdlr != null) {
                    Message msg = mainThreadHdlr.obtainMessage();
                    msg.obj = update;
                    mainThreadHdlr.sendMessage(msg);
                    Log.i("Net", "Sent update");
                } else {
                    System.out.println("null?");
                }
            }
        }
        socket.close();
    }

Are there any other wake locks I should be taking? Is there anything else I can do to keep the network/thread alive? I'm not even sure in what way it has stopped. Network? CPU? Other?

UPDATE After adding some more detailed logging I found that the SocketTimeoutException gets thrown at regular intervals, always, but the UDP packets are getting sent to the application less frequently when the screen is off! I'm expecting a packet every second and sometimes they wont arrive for ~5-10 seconds or more when the screen is off.

Chris H
  • 6,433
  • 5
  • 33
  • 51

0 Answers0