无线直接在uwp

时间:2017-01-03 07:09:46

标签: c# networking uwp wifi-direct windowsiot

我正在为wi-fi核心设备直接在通用平台上实现windows IOT,但我遇到了一个问题,即我没有监视器连接到设备(根据我的项目要求)。

那么是否可以将服务器端硬编码的设备配对或不使用引脚进行配对?

2 个答案:

答案 0 :(得分:2)

是的,这是可能的,使用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;
        }
    }
}

答案 1 :(得分:0)

还有另一个用于UWP应用程序的wifi-direct库PeerFinder。它非常方便可靠。它利用wifi-direct进行广告和发现。默认情况下,它按应用ID发现对等体,并与运行相同应用的其他对等体建立连接。