0

I tried to use Zeroconf to get all the devices. When i called the ResolveAsync, the code got freezed.

I am running Xamarin Form on Android phone. It didn't produce any error or force close. It just stays there.

public async Task Update()
    {
        DeviceInfoStruct device;
        string service = Constant.LAUNCHER_THRIFT_SERVICE + "local.";
        int tried = 0;
        List<DeviceInfoStruct> toDelete = new List<DeviceInfoStruct>();
        foreach (var d in _devices)
        {
            toDelete.Add(d);
        }
        try
        {
            if (IsHotSpotIp())
            {
                device = LauncherClient.GetDeviceInfo(Constant.HOTSPOT_DEVICE_IP);
                if (device != null)
                {
                    device.Ip = Constant.HOTSPOT_DEVICE_IP;
                    if (!_devices.Any(d => d.Ip == device.Ip))
                    {
                        _devices.Add(device);
                    }
                }
            }
            do
            {
                for (int i = 0; i < 5; i++)
                {
                    foreach (var r in await ZeroconfResolver.ResolveAsync(service))
                    {
                        if (r.Services.ContainsKey(service))
                        {
                            device = LauncherClient.GetDeviceInfo(r.IPAddress);
                            if (device != null)
                            {
                                device.Ip = r.IPAddress;
                                if (!_devices.Any(d => d.Ip == device.Ip))
                                {
                                    Debug.WriteLine("Found Device " + device.DeviceName + " @ " + device.Ip);
                                    _devices.Add(device);
                                    toDelete.RemoveAll(d => d.Ip == device.Ip);
                                }
                            }
                        }
                    }
                }
                tried++;
            } while ((tried < 2) && (_devices.Count == 0));

            foreach (var d in toDelete)
            {
                if (LauncherClient.GetDeviceInfo(d.Ip) == null)
                {
                    _devices.Remove(d);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

Locks from GitHub

buttonHosting.Command = new Command( () =>
        {

            #if __ANDROID__

            var wifi = (Android.Net.Wifi.WifiManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.WifiService);
            var mlock = wifi.CreateMulticastLock("Zeroconf lock");

               try
               {
                   mlock.Acquire();

                   var deviceModel = new DevicesViewModel();
                   var devices = deviceModel.GetAvailableDevicesAsync().Result;

                   foreach (var device in devices)
                   {
                       System.Diagnostics.Debug.WriteLine("Device Info: Name = " + device.DeviceName);
                       System.Diagnostics.Debug.WriteLine("             Version = " + device.DeviceVersion);
                   }
               }
               finally
               {
                   mlock.Release();
               }
            #endif

            //await Navigation.PushAsync(new MeetingPage(), true);
        });

The code below will just print a list of devices

ILookup<string, string> domains = await ZeroconfResolver.BrowseDomainsAsync();
        var responses = await ZeroconfResolver.ResolveAsync(domains.Select(g => g.Key));
        foreach (var resp in responses)
            Console.WriteLine(resp);

I can only see other devices' but not the one I wanted to look at...

LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • I followed the GitHub adding the try and final part in the code-behind of the axml which get called when clicked by the button – LittleFunny Sep 20 '16 at 23:25
  • I think it works now.. not sure what is wrong though. The GetAvilableDeviceAsync is the one calling the update function. Now I have async and await instead of having .Result. – LittleFunny Sep 20 '16 at 23:36
  • oh, i see it now, you were trying to the result without run the async method... – SushiHangover Sep 21 '16 at 00:00
  • Do I really need the mlock stuff? When I commented it out , the code still work. – LittleFunny Sep 21 '16 at 00:18
  • Short answer: Yes... Multicast packets are filtered out otherwise, are you testing on a physical device? Are there other apps installed that might be also acquiring a multicast lock? Some manufacturers/cell providers have their own zeroconf/multicast wireless services pre-installed, so it might already be enabled due to that, but you can not assume every device will. – SushiHangover Sep 21 '16 at 00:52
  • Yes I am using physical device – LittleFunny Sep 21 '16 at 00:55

0 Answers0