c# - C# - 使用 WiFiAdapter.ConnectAsync 方法连接到特定接入点是不可靠的
问题描述
当使用 Windows.Devices.WiFi 命名空间中的 WiFiAdapter.ConnectAsync 方法并尝试连接到通过 WiFiAdapter.ScanAsync 方法检索到的特定 wifi 接入点时,它并不总是连接到通过 WiFiAvailableNetwork 类枚举的接入点。相反,它通常以最佳连接方式连接到接入点。
private async void ConnectButton_Click(object sender, RoutedEventArgs e)
{
var selectedNetwork = ResultsListView.SelectedItem as WiFiNetworkDisplay;
if (selectedNetwork == null || firstAdapter == null)
{
rootPage.NotifyUser("Network not selcted", NotifyType.ErrorMessage);
return;
}
WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Manual;
if (IsAutomaticReconnection.IsChecked.HasValue && IsAutomaticReconnection.IsChecked == true)
{
reconnectionKind = WiFiReconnectionKind.Automatic;
}
WiFiConnectionResult result;
if (selectedNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Open80211 &&
selectedNetwork.AvailableNetwork.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None)
{
result = await firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind);
}
else
{
// Only the password portion of the credential need to be supplied
var credential = new PasswordCredential();
// Make sure Credential.Password property is not set to an empty string.
// Otherwise, a System.ArgumentException will be thrown.
// The default empty password string will still be passed to the ConnectAsync method,
// which should return an "InvalidCredential" error
if (!string.IsNullOrEmpty(NetworkKey.Password))
{
credential.Password = NetworkKey.Password;
}
result = await firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, credential);
}
if (result.ConnectionStatus == WiFiConnectionStatus.Success)
{
rootPage.NotifyUser(string.Format("Successfully connected to {0}.", selectedNetwork.Ssid), NotifyType.StatusMessage);
}
else
{
rootPage.NotifyUser(string.Format("Could not connect to {0}. Error: {1}", selectedNetwork.Ssid, result.ConnectionStatus), NotifyType.ErrorMessage);
}
}
我希望能够使用被传递给 ConnectAsync() 方法的关联 WiFiAvailableNetwork 类中的 BSSID 连接到 wifi 接入点。
解决方案
推荐阅读
- flutter - Flutter 语音和视频通话请求
- php - 从组合的 json 数组中获取数据
- ssh - Ansible 数据无法发送到远程主机。确保可以通过 ssh 访问此主机
- c# - 我如何使用硒移动这些数字
- bash - Ansible 执行脚本时跳过 shell 命令
- c# - C# 列表 - 删除重复项
- swift - 点击一个 ImageView 并打开一个 TableViewController
- vb.net - 带有孙子节点的vb.net树视图人口问题
- webpack - Webpack:根据条目名称设置我的资产的路径
- javascript - 无法读取 null 的属性“getElementsByClassName” - 我可以使用“if”语句强制它吗?