public async Task ConnectAsync()

in WindowsDevicePortalWrapper/WindowsDevicePortalWrapper.Shared/DevicePortal.cs [222:344]


        public async Task ConnectAsync(
            string ssid = null,
            string ssidKey = null,
            bool updateConnection = false,
#if WINDOWS_UWP
            Certificate manualCertificate = null)
#else
            X509Certificate2 manualCertificate = null)
#endif
        {
#if WINDOWS_UWP
            this.ConnectionHttpStatusCode = HttpStatusCode.Ok;
#else
            this.ConnectionHttpStatusCode = HttpStatusCode.OK;
#endif // WINDOWS_UWP
            string connectionPhaseDescription = string.Empty;

            if (manualCertificate != null)
            {
                this.SetManualCertificate(manualCertificate);
            }

            try 
            {
                // Get the device family and operating system information.
                connectionPhaseDescription = "Requesting operating system information";
                this.SendConnectionStatus(
                    DeviceConnectionStatus.Connecting,
                    DeviceConnectionPhase.RequestingOperatingSystemInformation,
                    connectionPhaseDescription);
                this.deviceConnection.Family = await this.GetDeviceFamilyAsync().ConfigureAwait(false);
                this.deviceConnection.OsInfo = await this.GetOperatingSystemInformationAsync().ConfigureAwait(false);

                // Default to using whatever was specified in the connection.
                bool requiresHttps = this.IsUsingHttps();

                // HoloLens is the only device that supports the GetIsHttpsRequired method.
                if (this.deviceConnection.OsInfo.Platform == DevicePortalPlatforms.HoloLens)
                {
                    // Check to see if HTTPS is required to communicate with this device.
                    connectionPhaseDescription = "Checking secure connection requirements";
                    this.SendConnectionStatus(
                        DeviceConnectionStatus.Connecting,
                        DeviceConnectionPhase.DeterminingConnectionRequirements,
                        connectionPhaseDescription);
                    requiresHttps = await this.GetIsHttpsRequiredAsync().ConfigureAwait(false);
                }

                // Connect the device to the specified network.
                if (!string.IsNullOrWhiteSpace(ssid))
                {
                    connectionPhaseDescription = string.Format("Connecting to {0} network", ssid);
                    this.SendConnectionStatus(
                        DeviceConnectionStatus.Connecting,
                        DeviceConnectionPhase.ConnectingToTargetNetwork,
                        connectionPhaseDescription);
                    WifiInterfaces wifiInterfaces = await this.GetWifiInterfacesAsync().ConfigureAwait(false);

                    // TODO - consider what to do if there is more than one wifi interface on a device
                    await this.ConnectToWifiNetworkAsync(wifiInterfaces.Interfaces[0].Guid, ssid, ssidKey).ConfigureAwait(false);
                }

                // Get the device's IP configuration and update the connection as appropriate.
                if (updateConnection)
                {
                    connectionPhaseDescription = "Updating device connection";
                    this.SendConnectionStatus(
                        DeviceConnectionStatus.Connecting,
                        DeviceConnectionPhase.UpdatingDeviceAddress,
                        connectionPhaseDescription);
                    
                    bool preservePort = true;

                    // HoloLens and Mobile are the only devices that support USB.
                    // They require the port to be changed when the connection is updated
                    // to WiFi.
                    if ((this.Platform == DevicePortalPlatforms.HoloLens) ||
                        (this.Platform == DevicePortalPlatforms.Mobile))
                    {
                        preservePort = false;
                    }

                    this.deviceConnection.UpdateConnection(
                        await this.GetIpConfigAsync().ConfigureAwait(false), 
                        requiresHttps,
                        preservePort);
                }

                this.SendConnectionStatus(
                    DeviceConnectionStatus.Connected,
                    DeviceConnectionPhase.Idle,
                    "Device connection established");
            }
            catch (Exception e)
            {
                DevicePortalException dpe = e as DevicePortalException;

                if (dpe != null)
                {
                    this.ConnectionHttpStatusCode = dpe.StatusCode;
                    this.ConnectionFailedDescription = dpe.Message;
                }
                else
                {
                    this.ConnectionHttpStatusCode = HttpStatusCode.Conflict;

                    // Get to the innermost exception for our return message.
                    Exception innermostException = e;
                    while (innermostException.InnerException != null)
                    {
                        innermostException = innermostException.InnerException;
                        await Task.Yield();
                    }

                    this.ConnectionFailedDescription = innermostException.Message;
                }

                this.SendConnectionStatus(
                    DeviceConnectionStatus.Failed,
                    DeviceConnectionPhase.Idle,
                    string.Format("Device connection failed: {0}, {1}", connectionPhaseDescription, this.ConnectionFailedDescription));
            }
        }