WiFiDirectDevice Class

Definition

Manages connections to associated Wi-Fi Direct devices.

public ref class WiFiDirectDevice sealed : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class WiFiDirectDevice final : IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class WiFiDirectDevice : System.IDisposable
Public NotInheritable Class WiFiDirectDevice
Implements IDisposable
Inheritance
Object Platform::Object IInspectable WiFiDirectDevice
Attributes
Implements

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

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(); 
    }
}

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 the Windows.Networking.Sockets API.

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.

Properties

ConnectionStatus

Gets the connection status for the WiFi-Direct device.

DeviceId

Gets the DeviceInformation Id for the Wi-Fi Direct device.

Methods

Close()

Close the active Wi-Fi Direct device connection.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

FromIdAsync(String, WiFiDirectConnectionParameters)

Creates an instance of the WiFiDirectDevice class for the specified device interface id with specific connection parameters.

FromIdAsync(String)

Creates an instance of the WiFiDirectDevice class for the specified device interface id.

GetConnectionEndpointPairs()

Gets a collection of network addresses for the Wi-Fi Direct device as endpoint pairs.

GetDeviceSelector()

Returns the class selection string that you can use to enumerate Wi-Fi Direct devices.

GetDeviceSelector(WiFiDirectDeviceSelectorType)

Returns the class selection string that you can use to enumerate Wi-Fi Direct devices for a specific Wi-Fi Direct device selector type.

Events

ConnectionStatusChanged

Occurs when a Wi-Fi Direct device connection is connected or disconnected.

Applies to

See also