Wifi direct in uwp

2.7k views Asked by At

I am implementing wi-fi direct in universal platform for windows IOT core devices but I have a problem that I have no monitor to connect to the devices(according to my project requirements).

So is it possible to pair the device making the pin hard coded at the server end or not using pin for pairing?

2

There are 2 answers

0
SynerCoder On

Yes this is possible, use the Windows.Devices.WiFi.WiFiAdapter API take a look at this tutorial:

https://developer.microsoft.com/en-us/windows/iot/samples/wificonnector

In short (taken and edited from the tutorial):

Make sure you add the correct device capability to your appx manifest:

<DeviceCapability Name="wifiControl" />

After that you can use the following code to connect to 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
Shashi Bhushan On

There is another library PeerFinder for wifi-direct available for UWP apps. It is very handy and reliable. It make use of wifi-direct for advertisement and discovery. By default , it discovers peers by App Id and will get connected with other peers who runs same app.