WiFiDirectDevice クラス

定義

関連付けられているWi-Fi Direct デバイスへの接続を管理します。

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
継承
Object Platform::Object IInspectable WiFiDirectDevice
属性
実装

Windows の要件

デバイス ファミリ
Windows 10 (10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox で導入)
API contract
Windows.Foundation.UniversalApiContract (v1.0 で導入)

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

注釈

WiFiDirectDevice クラスを使用すると、Wi-Fi Direct (WFD) 対応デバイスを持つ他のデバイスとのソケット接続を確立できます。 GetDeviceSelector メソッドを呼び出して、Wi-Fi Direct デバイスのデバイス識別子を取得できます。 コンピューターで WiFiDirectDevice への参照を取得したら、 GetConnectionEndpointPairs メソッドを呼び出して EndpointPair オブジェクトを取得し、 Windows.Networking.Sockets API を使用してソケット接続を確立できます。

接続が確立または切断されたときに通知される ConnectionStatusChanged イベントのハンドラーを追加できます。

Wi-Fi Direct デバイスに一度に接続できるアプリは 1 つだけです。

近接通信機能を有効にして、Wi-Fi Direct デバイスと通信する必要があります。

プロパティ

ConnectionStatus

WiFi-Direct デバイスの接続状態を取得します。

DeviceId

Wi-Fi Direct デバイスの DeviceInformation ID を 取得します。

メソッド

Close()

アクティブなWi-Fiダイレクト デバイス接続を閉じます。

Dispose()

アンマネージ リソースの解放またはリセットに関連付けられているアプリケーション定義のタスクを実行します。

FromIdAsync(String)

指定したデバイス インターフェイス ID の WiFiDirectDevice クラスのインスタンスを作成します。

FromIdAsync(String, WiFiDirectConnectionParameters)

特定の接続パラメーターを持つ、指定したデバイス インターフェイス ID の WiFiDirectDevice クラスのインスタンスを作成します。

GetConnectionEndpointPairs()

Wi-Fi Direct デバイスのネットワーク アドレスのコレクションをエンドポイント ペアとして取得します。

GetDeviceSelector()

Direct デバイスを列挙するために使用できるクラス選択文字列Wi-Fi返します。

GetDeviceSelector(WiFiDirectDeviceSelectorType)

Direct デバイス セレクターの種類を特定のWi-Fi Direct デバイスWi-Fi列挙するために使用できるクラス選択文字列を返します。

イベント

ConnectionStatusChanged

Wi-Fiダイレクト デバイス接続が接続または切断されたときに発生します。

適用対象

こちらもご覧ください