/// <summary> /// Asynchronously attempts to connect to the specified WiFi access point using a generated xml profile. /// </summary> /// <param name="accessPoint">WiFi access point to connect to.</param> /// <param name="password">Password required for authorization.</param> /// <param name="timeout">Timeout duration.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>True if successfully connected, false if failed or timed out.</returns> /// <remarks> /// Only BssType.Infrastructure is supported by Microsoft. /// WlanSetProfile, ErrorCode: 1206, ErrorMessage: The network connection profile is corrupted. /// ReasonCode: 524301, ReasonMessage: The BSS type is not valid. /// https://forums.intel.com/s/question/0D50P0000490AeNSAU/the-bss-type-is-not-valid?language=en_US#400176 /// </remarks> public Task <bool> ConnectNetworkAsync(AccessPoint accessPoint, string password, TimeSpan timeout, CancellationToken cancellationToken) { if (accessPoint == null) { throw new ArgumentNullException(nameof(accessPoint)); } if (accessPoint.IsPasswordRequired && string.IsNullOrEmpty(password)) { throw new ArgumentNullException(nameof(password)); } if (timeout.TotalSeconds < 0) { throw new ArgumentOutOfRangeException(nameof(timeout)); } return(Task.Run(() => { string profile = _profileService.CreateProfileXml(accessPoint, password); SetProfile(accessPoint.Id, ProfileType.AllUser, profile, null, true); return ConnectNetworkAsync(accessPoint.Id, accessPoint.Ssid, BssType.Infrastructure, timeout, cancellationToken); })); }
/// <summary> /// Create a valid profile xml according to: http://msdn.microsoft.com/en-us/library/ms707381(v=VS.85).aspx /// </summary> /// <param name="accessPoint">Access point for which the profile is created.</param> /// <param name="password">Password for the access point.</param> /// <returns></returns> public string CreateProfileXml(AccessPoint accessPoint, string password) { string profile = string.Empty; string template = string.Empty; string name = accessPoint.Ssid; string hex = HexConverter.ToHexadecimalString(accessPoint.Ssid); if (accessPoint.BssType != WiFiBssType.Infrastructure) { throw new NotSupportedException("Only BssType.Infrastructure is supported by Microsoft."); } switch (accessPoint.Encryption) { case WiFiEncryptionType.None: if (accessPoint.Authentication == WiFiAuthentication.Open) { template = GetTemplate("OPEN"); profile = string.Format(template, name, hex); } break; case WiFiEncryptionType.WEP: if (accessPoint.Authentication == WiFiAuthentication.Open) { template = GetTemplate("WEP"); profile = string.Format(template, name, hex, password); } break; case WiFiEncryptionType.AES: if (accessPoint.Authentication == WiFiAuthentication.WPA2_Personal) { template = GetTemplate("WPA2-PSK"); profile = string.Format(template, name, hex, password, accessPoint.Encryption); } else if (accessPoint.Authentication == WiFiAuthentication.WPA_Personal) { template = GetTemplate("WPA-PSK"); profile = string.Format(template, name, hex, password, accessPoint.Encryption); } break; case WiFiEncryptionType.TKIP: if (accessPoint.Authentication == WiFiAuthentication.WPA_Personal) { template = GetTemplate("WPA-PSK"); profile = string.Format(template, name, hex, password, accessPoint.Encryption); } else if (accessPoint.Authentication == WiFiAuthentication.WPA2_Personal) { template = GetTemplate("WPA2-PSK"); profile = string.Format(template, name, hex, password, accessPoint.Encryption); } break; } if (string.IsNullOrEmpty(profile)) { throw new NotImplementedException("Profile generation for access point is not implemented."); } else { return(profile); } }