WiFiDirectDevice WiFiDirectDevice WiFiDirectDevice WiFiDirectDevice Class

Definition

Manages connections to associated Wi-Fi Direct devices.

public : sealed class WiFiDirectDevice : IWiFiDirectDevice, IClosable
public sealed class WiFiDirectDevice : IWiFiDirectDevice, IDisposable
Public NotInheritable Class WiFiDirectDevice Implements IWiFiDirectDevice, IDisposable
// This class does not provide a public constructor.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

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 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 ConnectionStatus ConnectionStatus ConnectionStatus

Gets the connection status for the WiFi-Direct device.

public : WiFiDirectConnectionStatus ConnectionStatus { get; }
public WiFiDirectConnectionStatus ConnectionStatus { get; }
Public ReadOnly Property ConnectionStatus As WiFiDirectConnectionStatus
var wiFiDirectConnectionStatus = wiFiDirectDevice.connectionStatus;
See Also

DeviceId DeviceId DeviceId DeviceId

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

public : Platform::String DeviceId { get; }
public string DeviceId { get; }
Public ReadOnly Property DeviceId As string
var string = wiFiDirectDevice.deviceId;
Value
Platform::String string string string

The DeviceInformation Id for the Wi-Fi Direct device.

See Also

Methods

Close() Close() Close() Close()

Close the active Wi-Fi Direct device connection.

public : void Close()
This member is not implemented in C#
This member is not implemented in VB.Net
wiFiDirectDevice.close();
See Also

Dispose() Dispose() Dispose() Dispose()

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

This member is not implemented in C++
void Dispose()
Sub Dispose
void Dispose()

FromIdAsync(String) FromIdAsync(String) FromIdAsync(String) FromIdAsync(String)

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

public : static IAsyncOperation<WiFiDirectDevice> FromIdAsync(Platform::String deviceId)
public static IAsyncOperation<WiFiDirectDevice> FromIdAsync(String deviceId)
Public Static Function FromIdAsync(deviceId As String) As IAsyncOperation( Of WiFiDirectDevice )
var iAsyncOperation = Windows.Devices.WiFiDirect.WiFiDirectDevice.fromIdAsync(deviceId);
Parameters
deviceId
Platform::String String String String

The DeviceInformation Id of a Wi-Fi Direct device.

Returns

Remarks

You can use the GetDeviceSelector methods to enumerate Wi-Fi Direct devices to determine their interface ids.

See Also

FromIdAsync(String, WiFiDirectConnectionParameters) FromIdAsync(String, WiFiDirectConnectionParameters) FromIdAsync(String, WiFiDirectConnectionParameters) FromIdAsync(String, WiFiDirectConnectionParameters)

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

public : static IAsyncOperation<WiFiDirectDevice> FromIdAsync(Platform::String deviceId, WiFiDirectConnectionParameters connectionParameters)
public static IAsyncOperation<WiFiDirectDevice> FromIdAsync(String deviceId, WiFiDirectConnectionParameters connectionParameters)
Public Static Function FromIdAsync(deviceId As String, connectionParameters As WiFiDirectConnectionParameters) As IAsyncOperation( Of WiFiDirectDevice )
var iAsyncOperation = Windows.Devices.WiFiDirect.WiFiDirectDevice.fromIdAsync(deviceId, connectionParameters);
Parameters
deviceId
Platform::String String String String

The DeviceInformation Id of a Wi-Fi Direct device.

connectionParameters
WiFiDirectConnectionParameters WiFiDirectConnectionParameters WiFiDirectConnectionParameters WiFiDirectConnectionParameters

Connection parameters for a Wi-Fi Direct connect/pairing operation.

Returns

Remarks

You can use the GetDeviceSelector methods to enumerate Wi-Fi Direct devices to determine their interface ids.

See Also

GetConnectionEndpointPairs() GetConnectionEndpointPairs() GetConnectionEndpointPairs() GetConnectionEndpointPairs()

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

public : IVectorView<EndpointPair> GetConnectionEndpointPairs()
public IReadOnlyList<EndpointPair> GetConnectionEndpointPairs()
Public Function GetConnectionEndpointPairs() As IReadOnlyList<EndpointPair>( Of EndpointPair )
var iVectorView = wiFiDirectDevice.getConnectionEndpointPairs();
Returns
IVectorView<EndpointPair> IReadOnlyList<EndpointPair> IReadOnlyList<EndpointPair> IReadOnlyList<EndpointPair>

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

See Also

GetDeviceSelector() GetDeviceSelector() GetDeviceSelector() GetDeviceSelector()

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

public : static Platform::String GetDeviceSelector()
public static string GetDeviceSelector()
Public Static Function GetDeviceSelector() As string
var string = Windows.Devices.WiFiDirect.WiFiDirectDevice.getDeviceSelector();
Returns
Platform::String string string string

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

Remarks

This method only returns Wi-Fi Direct device interfaces.

See Also

GetDeviceSelector(WiFiDirectDeviceSelectorType) GetDeviceSelector(WiFiDirectDeviceSelectorType) GetDeviceSelector(WiFiDirectDeviceSelectorType) 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.

public : static Platform::String GetDeviceSelector(WiFiDirectDeviceSelectorType type)
public static string GetDeviceSelector(WiFiDirectDeviceSelectorType type)
Public Static Function GetDeviceSelector(type As WiFiDirectDeviceSelectorType) As string
var string = Windows.Devices.WiFiDirect.WiFiDirectDevice.getDeviceSelector(type);
Parameters
Returns
Platform::String string string string

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

Remarks

This method will return either Wi-Fi Direct device interfaces or associated endpoints for PCs, tablets, and phones that support Wi-Fi Direct depending on the value of the type parameter.

See Also

Events

ConnectionStatusChanged ConnectionStatusChanged ConnectionStatusChanged ConnectionStatusChanged

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

public : event TypedEventHandler ConnectionStatusChanged<WiFiDirectDevice,  object>
public event TypedEventHandler ConnectionStatusChanged<WiFiDirectDevice,  object>
Public Event TypedEventHandler ConnectionStatusChanged( Of ( Of WiFiDirectDevice ), ( Of  object ))
function onConnectionStatusChanged(eventArgs){/* Your code */}


wiFiDirectDevice.addEventListener("connectionStatusChanged", onConnectionStatusChanged);
wiFiDirectDevice.removeEventListener("connectionStatusChanged", onConnectionStatusChanged);

Remarks

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

See Also

See Also