/// <summary> /// Asynchronously attempts to connect to the wireless LAN associated to a specified wireless profile. /// </summary> /// <param name="interfaceId">Interface ID</param> /// <param name="profileName">Profile name</param> /// <param name="bssType">BSS network type</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> public static async Task <bool> ConnectNetworkAsync(Guid interfaceId, string profileName, BssType bssType, TimeSpan timeout, CancellationToken cancellationToken) { if (interfaceId == Guid.Empty) { throw new ArgumentException(nameof(interfaceId)); } if (string.IsNullOrWhiteSpace(profileName)) { throw new ArgumentNullException(nameof(profileName)); } if (timeout < TimeSpan.Zero) { throw new ArgumentException(nameof(timeout)); } using (var client = new Base.WlanClient()) { var tcs = new TaskCompletionSource <bool>(); Action <IntPtr, IntPtr> callback = (data, context) => { var notificationData = Marshal.PtrToStructure <WLAN_NOTIFICATION_DATA>(data); if (notificationData.NotificationSource != WLAN_NOTIFICATION_SOURCE_ACM) { return; } Debug.WriteLine("Callback: {0}", (WLAN_NOTIFICATION_ACM)notificationData.NotificationCode); switch (notificationData.NotificationCode) { case (uint)WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_complete: Task.Run(() => tcs.SetResult(true)); break; case (uint)WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_attempt_fail: Task.Run(() => tcs.SetResult(false)); break; } }; Base.RegisterNotification(client.Handle, WLAN_NOTIFICATION_SOURCE_ACM, callback); var result = Base.Connect(client.Handle, interfaceId, profileName, ConvertFromBssType(bssType)); if (!result) { tcs.SetResult(false); } var connectTask = tcs.Task; var completedTask = await Task.WhenAny(connectTask, Task.Delay(timeout, cancellationToken)); return((completedTask == connectTask) && connectTask.Result); } }
/// <summary> /// Attempts to connect to the wireless LAN associated to a specified wireless profile. /// </summary> /// <param name="interfaceId">Interface ID</param> /// <param name="profileName">Profile name</param> /// <param name="bssType">BSS network type</param> /// <returns>True if successfully requested the connection. False if failed.</returns> public static bool ConnectNetwork(Guid interfaceId, string profileName, BssType bssType = BssType.Any) { if (interfaceId == Guid.Empty) { throw new ArgumentException(nameof(interfaceId)); } if (string.IsNullOrWhiteSpace(profileName)) { throw new ArgumentNullException(nameof(profileName)); } using (var client = new Base.WlanClient()) { return(Base.Connect(client.Handle, interfaceId, profileName, ConvertFromBssType(bssType))); } }