uwp 中的 Wifi 直连
Posted
技术标签:
【中文标题】uwp 中的 Wifi 直连【英文标题】:Wifi direct in uwp 【发布时间】:2017-01-03 07:09:45 【问题描述】:我正在为windows IOT
核心设备在通用平台中直接实现wi-fi
,但我有一个问题,我没有显示器可以连接到设备(根据我的项目要求)。
那么,是否可以在服务器端对设备进行硬编码或不使用 pin 进行配对?
【问题讨论】:
【参考方案1】:是的,这是可能的,使用 Windows.Devices.WiFi.WiFiAdapter API 看看这个教程:
https://developer.microsoft.com/en-us/windows/iot/samples/wificonnector
简而言之(取自教程并编辑):
确保将正确的设备功能添加到您的 appx 清单中:
<DeviceCapability Name="wifiControl" />
之后就可以使用下面的代码连接wifi了;
enum WifiConnectResult
WifiAccessDenied,
NoWifiDevice,
Success,
CouldNotConnect,
SsidNotFound,
private async Task<WifiConnectResult> ConnectWifi(string ssid, string password)
if (password == null)
password = "";
var access = await WiFiAdapter.RequestAccessAsync();
if (access != WiFiAccessStatus.Allowed)
return WifiConnectResult.WifiAccessDenied;
else
var allAdapters = await WiFiAdapter.FindAllAdaptersAsync();
if (allAdapters.Count >= 1)
var firstAdapter = allAdapters[0];
await firstAdapter.ScanAsync();
var network = firstAdapter.NetworkReport.AvailableNetworks.SingleOrDefault(n => n.Ssid == ssid);
if (network != null)
WiFiConnectionResult wifiConnectionResult;
if (network.SecuritySettings.NetworkAuthenticationType == Windows.Networking.Connectivity.NetworkAuthenticationType.Open80211)
wifiConnectionResult = await firstAdapter.ConnectAsync(network, WiFiReconnectionKind.Automatic);
else
// Only the password potion of the credential need to be supplied
var credential = new Windows.Security.Credentials.PasswordCredential();
credential.Password = password;
wifiConnectionResult = await firstAdapter.ConnectAsync(network, WiFiReconnectionKind.Automatic, credential);
if (wifiConnectionResult.ConnectionStatus == WiFiConnectionStatus.Success)
return WifiConnectResult.Success;
else
return WifiConnectResult.CouldNotConnect;
else
return WifiConnectResult.SsidNotFound;
else
return WifiConnectResult.NoWifiDevice;
【讨论】:
【参考方案2】:还有另一个库 PeerFinder 用于 UWP 应用程序的 wifi-direct。它非常方便和可靠。它利用 wifi-direct 进行广告和发现。默认情况下,它通过 App Id 发现对等点,并与运行相同应用的其他对等点建立连接。
【讨论】:
PeerFinder
从来没有为我工作过,而且已经坏了好几年了(见here 和here),你知道它是否已经修复了吗?您是否设法在两台设备之间正确使用 WiFi-Direct 功能?以上是关于uwp 中的 Wifi 直连的主要内容,如果未能解决你的问题,请参考以下文章