Yortw / RSSDP

Really Simple Service Discovery Protocol - a 100% .Net implementation of the SSDP protocol for publishing custom/basic devices, and discovering all device types on a network.
http://yortw.github.io/RSSDP/
MIT License
282 stars 66 forks source link

Android does not find UPnP devices on wireless network(Xamarin Forms) #87

Open skaletech opened 5 years ago

skaletech commented 5 years ago

I have read the FAQ and applied these two answers:

I get notifications but no search results, why? I don't get search results or notifications?

My code is simple:

        private SsdpDeviceLocator _DeviceLocator;
        private static List<SsdpDevice> DeviceLst { get; set; }

        // Call this method from somewhere in your code to start the search.
        public void BeginSearch() {
            DeviceLst = new List<SsdpDevice>();
            IPAddress localNetwork = Dns.GetHostAddresses(Dns.GetHostName())
                .Where(ia => (ia.AddressFamily == AddressFamily.InterNetwork)).First();
            Rssdp.Infrastructure.SsdpCommunicationsServer localPortServer =
                new Rssdp.Infrastructure.SsdpCommunicationsServer(new Rssdp.SocketFactory(localNetwork.ToString()), 60000);
            _DeviceLocator = new SsdpDeviceLocator(localPortServer);

            // (Optional) Set the filter so we only see notifications for devices we care about
            // (can be any search target value i.e device type, uuid value etc - any value that appears in the
            // DiscoverdSsdpDevice.NotificationType property or that is used with the searchTarget parameter of the Search method).
            //_DeviceLocator.NotificationFilter = "upnp:rootdevice";
            _DeviceLocator.DeviceAvailable += deviceLocator_DeviceAvailable;
            _DeviceLocator.StartListeningForNotifications();
            _DeviceLocator.SearchAsync();
        }

        public void PauseSearch() {
            _DeviceLocator.StopListeningForNotifications();
            _DeviceLocator.Dispose();
            _DeviceLocator = null;
        }

        async static void deviceLocator_DeviceAvailable(object sender, DeviceAvailableEventArgs e) {
            var fullDevice = await e.DiscoveredDevice.GetDeviceInfo();
            lock(DeviceLst) {
                if(!DeviceLst.Any(c => c.Uuid == fullDevice.Uuid)) {
                    DeviceLst.Add(fullDevice);
                    (Application.Current as App).DeviceListView.ItemsSource = DeviceLst;
                }
            }
        }

I can see the device in Device Spy, and I can see both M-SEARCH and NOTIFY messages in Device Sniffer.

Device and phone are on the same subnet.

My only clue is that the Galaxy S9 test device does not find any UPnP devices on the network using SSDPTester app.

Is there a setting on the phone that is keeping DeviceLocator from receiving UDP traffic?

Yortw commented 5 years ago

Hi,

Unfortunately I don't have an S9 to test with, but it should work, it works fine on my S8. There may be a setting causing a problem, but if there is I don't know what it is - there was nothing I had to change o my S8 for it to work for me. I doubt it's setting related.

Can you check/confirm the IP address being passed to the communications server object is actually your phone's IP address on the wifi? I've seen issues before where the IP for the cellular modem gets assigned and then you're on the wrong network so it doesn't work. Your code looks like it should get the right IP, but it's hard to be certain without being able to test it.

skaletech commented 5 years ago

Thanks for the response, the IP on the phone is 10.10.7.105, and the IP on the PC is 10.10.7.102. On the PC, I can see M-SEARCH requests from the source IP 10.10.7.105, whether I run my application with RSSDP or SSDPTester app from Google Play. 10.10.7.105 is the IP that I see when I look at the value of localNetwork in the code snippet above.

When I run the code above in a process on the PC, it receives a response, but that isn't surprising.

Yortw commented 5 years ago

Odd. Is there any sort of firewall or VPN software on the phone that might be blocking it? The notifications & search are both broadcast, but the responses to a search are unicast direct to your IP IIRC, so maybe a firewall or something would block that but pass notifications? (Diffferent ports anyway).

skaletech commented 5 years ago

There's no additional software on this phone - it is at factory settings with developer options turned on. Both searcher and publisher are on port 60000.

It's ok. It feels like it is coded right. I'm going to set up a separate network for testing in case we have something weird on the business network.

I'll let you know if, for some reason, it still doesn't work.