Android Multicast disables listening

I have an Android service that listens and responds to packets received via multicast.

This works most of the time, but unfortunately I find that from time to time it just stops receiving packets. The thread remains blocked when MulticastSocket.receive () is called, but nothing comes out. I slowly tried to track what causes this, and I found that turning the wireless network on and off, or changing the wireless connection, would do it.

To successfully continue listening to my packages, I need to close MulticastSocket and recreate it again. However, I did not find any way to do this. I do not receive a notification that something has happened: no intentions, no Java exceptions, nothing. I would expect the receiver to receive an IOException if the socket became invalid, but this does not seem to be happening.

Can someone give an idea of ​​what is really happening and (b) how to get around it? Right now, my best guess is to constantly destroy and recreate a socket every ten seconds or so, and that is just evil.

+3
source share
2 answers

?

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
MulticastLock lock = manager.createMulticastLock("lock name");
lock.setReferenceCounted(true);
lock.acquire();

, :

Android 2.2

+2

Source: https://habr.com/ru/post/1770941/


All Articles