Windows.Networking.Proximity.WindowsUri

The WindowsUri class represents a Uniform Resource Identifier (URI) that can be used for Windows Runtime (WinRT) applications to communicate with other devices using Wi-Fi Direct. This allows for peer-to-peer connections and data transfer without the need for a network infrastructure.

Overview

Wi-Fi Direct enables devices to connect directly to each other using Wi-Fi. The WindowsUri class is a key component for initiating and managing these connections within UWP applications. It defines a standardized way to represent the target of a Wi-Fi Direct connection.

Key Concepts

Namespace

Windows.Networking.Proximity

Syntax

public sealed class WindowsUri

Properties

Methods

Usage Example

The following example demonstrates how to create a WindowsUri object and use it to initiate a Wi-Fi Direct connection.

Note:

This example assumes you have the necessary permissions and have implemented the peer discovery and connection logic using other Windows Runtime APIs like ProximityDevice and ConnectionRequestedEventArgs.

using Windows.Networking.Proximity;
using Windows.Foundation;

// ... inside an async method or event handler

// Define the URI for a Wi-Fi Direct service (example)
string serviceUriString = "ms-winsock:tcpip/192.168.1.100:8080";

try
{
    // Create a WindowsUri object
    WindowsUri peerUri = new WindowsUri(serviceUriString);
    peerUri.DisplayName = "My File Transfer Service";

    // Now you can use this peerUri object with ProximityDevice for connection
    // For instance, when handling a ConnectionRequested event:

    // e.ConnectionRequestedEventArgs.PeerFinderUri = peerUri;

    // Or when publishing a service (if applicable)
    // ProximityDevice.PublishUriMessage(peerUri);

    System.Diagnostics.Debug.WriteLine($"Successfully created WindowsUri for: {peerUri.RawUri}");
}
catch (ArgumentException ex)
{
    System.Diagnostics.Debug.WriteLine($"Error creating WindowsUri: {ex.Message}");
}
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine($"An unexpected error occurred: {ex.Message}");
}

Related Topics