public static async Task DetectDevicesAsync() { if (DetectingDevices) { return; } FreeDevices(); DetectingDevices = true; var serialTask = SerialDevice.GetDevicesAsync(); var udpTask = UDPDevice.GetDevicesAsync(); DetectingDevices = false; }
public static async Task GetDevicesAsync() { BroadcastMessanger.SendCommand(new Command.GetInfo()); DataReceivedEventHandler detector = (s, e) => { Reply reply = Reply.ParseFrom(e.Data); if (reply is Reply.DeviceInfo) { var deviceInfo = (Reply.DeviceInfo)reply; string serialNumber = deviceInfo.SerialNumber.ToString(); string name = deviceInfo.Name; name = (name.Length) > 0 ? name : serialNumber; bool inNetwork = (deviceInfo.NetworkName.Length > 0); lock (Devices) { if (Devices.SingleOrDefault(d => d.Name == name) != null) { return; } var device = new UDPDevice(e.RemoteEndPoint.Address.ToString()); device.SerialNumber = serialNumber; device.Name = name.Length > 0 ? name : serialNumber; device.ConnectionInfo = inNetwork ? $"via {deviceInfo.NetworkName} network" : "via wifi direct"; device.ConnectionType = inNetwork ? ConnectionType.LAN : ConnectionType.DirectWiFI; Devices.Add(device); NotifyDevicesChanged(); } } }; BroadcastMessanger.OnDataReceived += detector; await BroadcastMessanger.EventListenAsync(300); BroadcastMessanger.OnDataReceived += detector; }