I have an interesting problem that appears to be related to multicast
being switched off (at the kernal) on newer andriod devices, please note This is NOT
an issue with setting up UDP on multi adapters or LTE/WIFI, I understand that.
A lot of googling and trying loads things out have offered up a multitude of answers, e.g. acquire multi cast locks and some git hub issues where people say its impossible as devices have multicast disabled in the kernel
. I have also just found this on SO
My code works fine on:-
- Nexus 5
Andriod 8.1 - API 27
- Lenovo Tablet
Andriod 8.1 - API 27
That is it can send and receive UDP packets that is not directly targeted to the IP address of the phone.
It does NOT receive UDP packets on:-
- Pixel 3a
Andriod 10/11 APIS 29/30
The Xamarin forms code to replicate this is:
public TestCameraPage()
{
InitializeComponent();
sendClient = new UdpClient
{
EnableBroadcast = true,
ExclusiveAddressUse = false,
MulticastLoopback = true
};
sendClient.Client.Bind(new IPEndPoint(IPAddress.Any, CameraPort));
Lab1.Text = "Started to listen for UDP packets";
sendClient.BeginReceive(DiscoverCallback, sendClient);
}
private void DiscoverCallback(IAsyncResult result)
{
try
{
var ep = new IPEndPoint(IPAddress.Any, CameraPort);
var data = sendClient.EndReceive(result, ref ep);
var msg = $"Received: {Encoding.UTF8.GetString(data)}";
//Sniff out camera IP
var ip = $"{data[15]}.{data[14]}.{data[13]}.{data[12]}";
Device.BeginInvokeOnMainThread(() =>
{
Lab1.Text = $"CAMERA IP: {ip} FULL MESSAGE: {msg}";
});
}
finally
{
sendClient.BeginReceive(DiscoverCallback, sendClient);
}
}
private void Button_OnClicked(object sender, EventArgs e)
{
var data = PollMessageToCamera();
sendClient.Send(data, data.Length, "255.255.255.255", ListeningPort);
}
So my questions are two fold
- Is it possible to receive UDP packets that are NOT broadcasted directly to the device on a newer Andriod phone?
- If it is possible what do I need to do to fix it?
Example output on nexus and tablets
I should also point out I have a console app that can send UDP messages and if I use this (ip address of pixel 3a), it works
sendClient.Send(data, data.Length, "192.168.1.248", CameraPort);
If I use 255.255.255.255 (multi cast) in console app only the nexus and tablet works the pixel 3a doesn't
sendClient.Send(data, data.Length, "255.255.255.255", CameraPort);