Xamarin Android 10 UDP Client In Background (Release Mode Only)


I am having an issue with the UDPClient in Android 10 (worked fine in Android 9) so Im assuming Im missing from Android 10 that I have yet to be able to find so far.

I have a UDPService (background service) running which is a pretty standard implementation (Port is 64200)

                    m_udpSocket = new UdpClient(m_port);
                    m_udpSocket.Client.ReceiveTimeout = (int)m_healthTimeout.TotalMilliseconds;
                    m_healthSocket.Client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, (int)m_healthTimeout.TotalMilliseconds );

I wont post the sending and receiving code at this stage as I think its more permissions related.

The client periodically sends some UDP data to the server for processing. Its all local network based (not internet) and the data is sent via the WiFi on the device.

In Android 9, it all works as expected with the app in the foreground or the background.

In Android 10, it works perfectly when the app is in the foreground, but when the app goes to the background, when I try to issue the send on the socket

System.Net.Sockets.SocketException (0x80004005): Access denied

This error only happens in release mode on Android 10. If Im running in Debug Mode attached to the device, I can put the app into the background and the UDP traffic works as expected.

Based on all the above, Im assuming there is something else I need to do to allow the socket to work in the background in release mode but I cant seem to put my finger on what it is. Ive read about INTERNET permission and how Debug mode automatically adds this so is there something else I may be missing in the Android Manifest that could cause this.

Any help would be greatly appreciated.

Thanks in advance.

asked on Stack Overflow Jan 28, 2021 by Daniel Kelly

1 Answer


For anyone experiencing similar problems, in the end it had nothing to do with the actual code, but it was the Battery Optimisation Settings in Android.

Once I removed the app from the automatic battery optimisations, everything worked as expected.

answered on Stack Overflow Jan 30, 2021 by Daniel Kelly

User contributions licensed under CC BY-SA 3.0