Windows.Devices.WiFiDirect Windows.Devices.WiFiDirect Windows.Devices.WiFiDirect Windows.Devices.WiFiDirect Namespace

Contains classes that support connecting to associated Wi-Fi Direct devices and associated endpoints for PCs, tablets, and phones.

Classes

WiFiDirectAdvertisement WiFiDirectAdvertisement WiFiDirectAdvertisement WiFiDirectAdvertisement

Represent a Wi-Fi Direct advertisement and allows the app to control the listen state and custom information elements in the advertisement.

WiFiDirectAdvertisementPublisher WiFiDirectAdvertisementPublisher WiFiDirectAdvertisementPublisher WiFiDirectAdvertisementPublisher

An object to publish Wi-Fi Direct advertisements.

WiFiDirectAdvertisementPublisherStatusChangedEventArgs WiFiDirectAdvertisementPublisherStatusChangedEventArgs WiFiDirectAdvertisementPublisherStatusChangedEventArgs WiFiDirectAdvertisementPublisherStatusChangedEventArgs

Provides data for a StatusChanged event on a WiFiDirectAdvertisementPublisher.

WiFiDirectConnectionListener WiFiDirectConnectionListener WiFiDirectConnectionListener WiFiDirectConnectionListener

Class used to listen for incoming Wi-Fi Direct connection requests.

WiFiDirectConnectionParameters WiFiDirectConnectionParameters WiFiDirectConnectionParameters WiFiDirectConnectionParameters

Used by an app to specify the connection parameters for a Wi-Fi Direct connect/pairing operation.

WiFiDirectConnectionRequest WiFiDirectConnectionRequest WiFiDirectConnectionRequest WiFiDirectConnectionRequest

A Wi-FI Direct connection request received by a WiFiDirectConnectionListener.

WiFiDirectConnectionRequestedEventArgs WiFiDirectConnectionRequestedEventArgs WiFiDirectConnectionRequestedEventArgs WiFiDirectConnectionRequestedEventArgs

Provides data for a ConnectionRequested event on a WiFiDirectConnectionListener.

WiFiDirectDevice WiFiDirectDevice WiFiDirectDevice WiFiDirectDevice

Manages connections to associated Wi-Fi Direct devices.

WiFiDirectInformationElement WiFiDirectInformationElement WiFiDirectInformationElement WiFiDirectInformationElement

Represents information elements in a Wi-Fi Direct packet.

WiFiDirectLegacySettings WiFiDirectLegacySettings WiFiDirectLegacySettings WiFiDirectLegacySettings

Settings governing "legacy" mode (non-Wi-Fi Direct connections to the access point being advertised.)

This access point allows devices that do not support Wi-Fi Direct to connect to a device that does support it, and to use the services offered by the Wi-Fi Direct device. The connection does not serve as a general-purpose Wi-Fi access point.

Enums

WiFiDirectAdvertisementListenStateDiscoverability WiFiDirectAdvertisementListenStateDiscoverability WiFiDirectAdvertisementListenStateDiscoverability WiFiDirectAdvertisementListenStateDiscoverability

Specifies whether the Wi-Fi Direct device is listening and discoverable.

WiFiDirectAdvertisementPublisherStatus WiFiDirectAdvertisementPublisherStatus WiFiDirectAdvertisementPublisherStatus WiFiDirectAdvertisementPublisherStatus

Represents the possible states of the WiFiDirectAdvertisementPublisher.

WiFiDirectConfigurationMethod WiFiDirectConfigurationMethod WiFiDirectConfigurationMethod WiFiDirectConfigurationMethod

Specifies a Wi-Fi Direct configuration method. Configuration is how a user tells one piece of Wi-Fi Direct equipment to connect to another piece of Wi-Fi Direct equipment.

WiFiDirectConnectionStatus WiFiDirectConnectionStatus WiFiDirectConnectionStatus WiFiDirectConnectionStatus

Describes the connection status of a WiFiDirectDevice object.

WiFiDirectDeviceSelectorType WiFiDirectDeviceSelectorType WiFiDirectDeviceSelectorType WiFiDirectDeviceSelectorType

Specifies the device selector type for Wi-Fi Direct.

WiFiDirectError WiFiDirectError WiFiDirectError WiFiDirectError

Specifies some common Wi-Fi Direct error cases.

WiFiDirectPairingProcedure WiFiDirectPairingProcedure WiFiDirectPairingProcedure WiFiDirectPairingProcedure

Specifies a direct pairing procedure.

Wi-Fi Direct supports two different pairing procedures. In the Group Owner Negotiation procedure, the connecting device chooses the configuration method (PIN entry, PIN display, or pushbutton pairing.) In the P2P Invitation procdure, the accepting device chooses the configuration method. Use the values in this enumeration to identify a pairing procedure.

Examples

Windows.Devices.WiFiDirect.WiFiDirectDevice wfdDevice;
private async System.Threading.Tasks.Task<String> Connect(string deviceId)
{
    string result = ""; 

    try
    {
        // No device Id specified.
        if (String.IsNullOrEmpty(deviceId)) { return "Please specify a Wi- Fi Direct device Id."; }

        // Connect to the selected Wi-Fi Direct device.
        wfdDevice = await Windows.Devices.WiFiDirect.WiFiDirectDevice.FromIdAsync(deviceId);

        if (wfdDevice == null)
        {
            result = "Connection to " + deviceId + " failed.";
        }

        // Register for connection status change notification.
        wfdDevice.ConnectionStatusChanged += new TypedEventHandler<Windows.Devices.WiFiDirect.WiFiDirectDevice, object>(OnConnectionChanged);

        // Get the EndpointPair information.
        var EndpointPairCollection = wfdDevice.GetConnectionEndpointPairs();

        if (EndpointPairCollection.Count > 0)
        {
            var endpointPair = EndpointPairCollection[0];
            result = "Local IP address " + endpointPair.LocalHostName.ToString() + 
                " connected to remote IP address " + endpointPair.RemoteHostName.ToString();
        }
        else
        {
           result = "Connection to " + deviceId + " failed.";
        }
    }
    catch (Exception err)
    {
        // Handle error.
        result = "Error occurred: " + err.Message;
    }

    return result;
}

private void OnConnectionChanged(object sender, object arg)
{
    Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus status = 
        (Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus)arg;

    if (status == Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus.Connected)
    {
        // Connection successful.
    }
    else
    {
        // Disconnected.
        Disconnect();
    }
}

private void Disconnect()
{
    if (wfdDevice != null) 
    {
        wfdDevice.Dispose(); 
    }
}
var wfdDevice;

function connect(deviceId) {
    var result = ""; 

    try
    {
        // No device Id specified.
        if (!deviceId) { return "Please specify a Wi- Fi Direct device Id."; }

        // Connect to the selected Wi-Fi Direct device.
        Windows.Devices.WiFiDirect.WiFiDirectDevice.fromIdAsync(deviceId).then(
            function (wfdDeviceResult) {
                if (!wfdDeviceResult) {
                    result = "Connection to " + deviceId + " failed.";
                }

                wfdDevice = wfdDeviceResult;

                // Register for connection status change notification.
                wfdDevice.onconnectionstatuschanged = onConnectionChanged; 

                // Get the EndpointPair information.
                var endpointPairCollection = wfdDevice.getConnectionEndpointPairs();

                if (endpointPairCollection.Count > 0) {
                    var endpointPair = endpointPairCollection[0];
                    result = "Local IP address " + endpointPair.localHostName.toString() + 
                        " connected to remote IP address " + endpointPair.remoteHostName.toString();
                }
                else {
                    result = "Connection to " + deviceId + " failed.";
                }
            });

    }
    catch (err) {
        // Handle error.
        result = "Error occurred: " + err.message;
    }

    return result;
}

function onConnectionChanged(status) {
    if (status == Windows.Devices.WiFiDirect.WiFiDirectConnectionStatus.connected) {
        // Connection successful.
    }
    else {
        // Disconnected.
       disconnect();
    }
}

function disconnect() {
    if (wfdDevice) {
        wfdDevice.dispose(); 
    }
}

Remarks

You can use the WiFiDirectDevice class to establish a socket connection with other devices that have a Wi-Fi Direct (WFD) capable device. You can call the GetDeviceSelector method to get the device identifier for a Wi-Fi Direct device. Once you have a reference to a WiFiDirectDevice on your computer, you can call the GetConnectionEndpointPairs method to get an EndpointPair object and establish a socket connection using classes in the Windows.Networking.Sockets namespace.

You can add a handler for the ConnectionStatusChanged event to be notified when the connection has been established or disconnected.

Only one app can be connected to a Wi-Fi Direct device at a time.

You must enable the Proximity capability to communicate with Wi-Fi Direct devices.

See Also