4

I have an Android app that creates a MulticastSocket, joins a MC group and receives messages from another machine on the local wifi network.

MulticastSocket socket = new MulticastSocket(null); // Create an unbound socket.
socket.setSoTimeout(LISTEN_TIMEOUT_MILLIS);
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(listenPort)); // Bind to the configured multicast port

final WifiManager.MulticastLock lock = wifiManager.createMulticastLock("my_lock");
lock.acquire();

socket.setNetworkInterface(networkInterface);
socket.joinGroup(multicastGroup);
while (true) {
    socket.receive(packet);
    // Do something with the packet
    // Handle timeout etc.
    // Handle change of network interface by leaving group, setting netIntf and joining group again.
}
socket.leaveGroup(multicastGroup);
socket.close();

lock.release();

Works well on most Android devices (Huawei, Samsung), but on some (Pixel3), if the WiFi on the device is switched off and then back on again, while the app sees the Wifi connection come live, it can take up to 14 mins (it is extremely variable) before the MC messages start being received again.

Even throwing away the Socket and creating a fresh MCSocket doesn't alleviate the delay.

But it has to be some state that is held within the JVM, because a restart of the app causes it to connect immediately.

It feels like there is some lease that is being held for the MC connection that is only being renewed on a clock cycle.

So my questions are:

  1. What is causing the MC messages to not flow immediately after the WiFi connection comes back up and a new MCSocket is created to listen to it.
  2. What can I do to ensure timely resumption of the message flow?
William
  • 20,150
  • 8
  • 49
  • 91
  • Since this is multicast, are sent packets being received by some clients and not others? – Wayne Phipps Jan 01 '19 at 02:57
  • Yes. The packets are still being sent. Same app running on other Android devices still receive the packets. The only client that stops receiving the packets (for some indeterminate time) is the app running on the Android device that had it's WiFi disabled and then re-enabled (or switched to another WiFi network and back again). It will stop receiving the packets for between 0 and 14 mins. – William Jan 01 '19 at 03:50
  • Try this: [WifiManager.MulticastLock](https://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock) – Kousic Jan 02 '19 at 06:25
  • Thanks @Kousic but I am already using the WiFiManager#MulticastLock. Have added it to the code above. – William Jan 02 '19 at 07:41

1 Answers1

1

I notice you've updated your question to include WifiManager.MulticastLock

I wonder if you are you reacquiring the lock when the Wifi connection comes back, some posts here on SO imply this is necessary.

I note the comment on the following post:

Re: https://stackoverflow.com/a/4002084/1015289

it turns out that your multicast lock is destroyed when the connectivity goes away (the long delay was me rewriting my code three times before I figured this out). So, you have to reacquire the lock every time the connection comes back

Wayne Phipps
  • 2,019
  • 6
  • 26
  • 31
  • 1
    Thanks, that is one option that I didn't consider. Will test it. – William Jan 03 '19 at 21:18
  • One thing to bare in mind is that the function they mention is deprecated, *apps should use the more versatile requestNetwork(NetworkRequest, PendingIntent), registerNetworkCallback(NetworkRequest, PendingIntent) or registerDefaultNetworkCallback(ConnectivityManager.NetworkCallback)*. That is mentioned in the docs though here: https://developer.android.com/reference/android/net/ConnectivityManager#CONNECTIVITY_ACTION – Wayne Phipps Jan 03 '19 at 21:20
  • 1
    Thank you - this is the missing piece of the puzzle. I've stressed it for the last 2 hours and the messages always start to flow immediately. Hopefully this saves other Android devs from endless days of pain in what appears to be MC flakiness on Android. – William Jan 03 '19 at 23:50
  • Thanks for posting your question, it was certainly interesting. I'm glad I could help, thank you for the bounty. – Wayne Phipps Jan 04 '19 at 08:59