Apparently UDP packets sent with a broadcast address such as 192.168.10.255 will be blocked by some versions of Android (which is the case with my Motorola MotoG phone). To get around this, some people recommended using WifiManager.MulticastLock when creating the listening socket. This works, but only temporarily. I am finding that only a handful of these broadcast packets get through before Android blocks them again. Does anyone have a suggestion? Below is the code snippet.
val wifiManager: WifiManager = getApplicationContext().getSystemService(WIFI_SERVICE) as WifiManager
val lock: MulticastLock = wifiManager.createMulticastLock(“Log_Tag”)
lock.acquire();
val socketIn = MulticastSocket(4000)
val packetIn = DatagramPacket(buffer, buffer.size)
socketIn.receive(packetIn)