我尝试使用Zeroconf来获取所有设备。当我调用ResolveAsync时,代码被冻结了。
我在Android手机上运行Xamarin Form。它没有产生任何错误或力量关闭。它就在那里。
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());
}
}
来自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);
});
以下代码只会打印设备列表
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);
我只能看到其他设备,但不是我想看的那个......