情報
要求されたトピックは次のとおりです。しかし、このトピックはこのライブラリには含まれていません。

PeerFinder Class

近くのデバイスでアプリの別のインスタンスを検出し、タップ ジェスチャまたはブラウズを使用してピア アプリ間のソケット接続を作成できるようにします。

構文


Public NotInheritable Class PeerFinder

属性

[MarshalingBehavior(Agile)]
[Threading(Both)]
[Version(0x06020000)]

メンバー

PeerFinderクラス には次の種類のメンバーがあります。

イベント

PeerFinderクラス にはこれらのイベントがあります。

イベント説明
ConnectionRequested Occurs when a remote peer requests a connection using the ConnectAsync method.
TriggeredConnectionStateChanged Occurs during a tap gesture from a remote peer.

 

メソッド

The PeerFinder クラス には次のメソッドがあります。 With C#、Visual Basic、および C++ では、以下からもメソッドが継承されます。Object クラス.

メソッド説明
ConnectAsync Connects to a peer discovered by a call to the FindAllPeersAsync method.
CreateWatcher Creates a new instance of a PeerWatcher object for dynamic discovery of peer apps.
FindAllPeersAsync Asynchronously browses for peer computers that are running the same app within wireless range.
Start() Starts the process of finding peer apps and makes an app discoverable to remote peers.
Start(String) Delivers a message to a peer app on a proximate device.
Stop Stops the process of finding peer apps or advertising for a peer connection.

 

プロパティ

PeerFinderクラス には次のプロパティがあります。

プロパティアクセスの種類説明

AllowBluetooth

読み取り/書き込みSpecifies whether the PeerFinder class may connect a StreamSocket object by using Bluetooth.

AllowInfrastructure

読み取り/書き込みSpecifies whether the PeerFinder class may connect to a StreamSocket object using TCP/IP.

AllowWiFiDirect

読み取り/書き込みSpecifies whether the PeerFinder class may connect a StreamSocket object by using Wi-Fi Direct.

AlternateIdentities

読み取り専用Gets a list of alternate appId values to match with peer applications on other platforms.

DiscoveryData

読み取り/書き込みGets or sets the name that identifies your computer to remote peers.

DisplayName

読み取り/書き込みGets or sets the name that identifies your computer to remote peers.

Role

読み取り/書き込みGets or sets the role of the app in peer-to-peer connections.

SupportedDiscoveryTypes

読み取り専用Gets a value that indicates which discovery options are available to use with the PeerFinder class.

 

解説

重要  

近接 API は、認証、暗号化、またはメッセージの整合性を提供しません。ユーザーの機密情報 (パスワード、財務データ、テキスト メッセージ、電子メール、写真、政府の ID 番号など) をやり取りする場合は、近接通信を使用しないでください。


Protected Overrides Sub OnNavigatedTo(e As Navigation.NavigationEventArgs)
    DisplayNameTextBox.Text = Windows.Networking.Proximity.PeerFinder.DisplayName
    AddHandler Windows.Networking.Proximity.PeerFinder.ConnectionRequested, AddressOf ConnectionRequested

    ' If activated from launch or from the background, create a peer connection.
    Dim args = TryCast(e.Parameter, Windows.ApplicationModel.Activation.LaunchActivatedEventArgs)
    If args IsNot Nothing AndAlso args.Kind = Windows.ApplicationModel.Activation.ActivationKind.Launch Then

        If args.Arguments = "Windows.Networking.Proximity.PeerFinder:StreamSocket" Then
            AdvertiseForPeersButton_Click()
        End If
    End If
End Sub

Protected Overrides Sub OnNavigatingFrom(e As Navigation.NavigatingCancelEventArgs)
    If _started Then
        ' Detach the callback handler (there can only be one PeerConnectProgress handler).
        RemoveHandler Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged, AddressOf TriggeredConnectionStateChanged
        ' Detach the incoming connection request event handler.
        RemoveHandler Windows.Networking.Proximity.PeerFinder.ConnectionRequested, AddressOf ConnectionRequested
        Windows.Networking.Proximity.PeerFinder.Stop()
        CloseSocket()
        _started = False
    End If
End Sub

' Write a message to MessageBlock on the UI thread.
Private Async Sub WriteMessageText(message As String, Optional overwrite As Boolean = False)
    Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        Sub()
            If overwrite Then
                MessageBlock.Text = message
            Else
                MessageBlock.Text &= message
            End If
        End Sub)
End Sub

Private _started As Boolean = False

' Click event handler for "Advertise" button.
Private Sub AdvertiseForPeersButton_Click()

    If _started Then
        WriteMessageText("You are already advertising for a connection." & vbCrLf)
        Return
    End If

    Windows.Networking.Proximity.PeerFinder.DisplayName = DisplayNameTextBox.Text

    If ((Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes And
         Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) =
         Windows.Networking.Proximity.PeerDiscoveryTypes.Triggered) Then

        AddHandler Windows.Networking.Proximity.PeerFinder.TriggeredConnectionStateChanged,
            AddressOf TriggeredConnectionStateChanged

        WriteMessageText("You can tap to connect a peer device that is " &
                         "also advertising for a connection." & vbCrLf)
    Else
        WriteMessageText("Tap to connect is not supported." & vbCrLf)
    End If

    If (Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes And
        Windows.Networking.Proximity.PeerDiscoveryTypes.Browse) <>
        Windows.Networking.Proximity.PeerDiscoveryTypes.Browse Then

        WriteMessageText("Peer discovery using Wifi-Direct is not supported." & vbCrLf)
    End If

    Windows.Networking.Proximity.PeerFinder.Start()
    _started = True
End Sub

Private Sub TriggeredConnectionStateChanged(
    sender As Object,
    e As Windows.Networking.Proximity.TriggeredConnectionStateChangedEventArgs)

    If e.State = Windows.Networking.Proximity.TriggeredConnectState.PeerFound Then

        WriteMessageText("Peer found. You may now pull your devices out of proximity." & vbCrLf)
    End If

    If e.State = Windows.Networking.Proximity.TriggeredConnectState.Completed Then
        WriteMessageText("Connected. You may now send a message." & vbCrLf)
        SendMessage(e.Socket)
    End If
End Sub

' Click event handler for "Browse" button.
Private Async Sub FindPeersButton_Click()
    If (Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes And
        Windows.Networking.Proximity.PeerDiscoveryTypes.Browse) <>
        Windows.Networking.Proximity.PeerDiscoveryTypes.Browse Then

        WriteMessageText("Peer discovery using Wifi-Direct is not supported." & vbCrLf)
        Return
    End If

    Try
        Dim peerInfoCollection = Await Windows.Networking.Proximity.PeerFinder.FindAllPeersAsync()
        If peerInfoCollection.Count > 0 Then
            ' Connect to first peer found - example only.
            ' In your app, provide the user with a list of available peers.
            ConnectToPeer(peerInfoCollection(0))
        End If
    Catch err As Exception
        WriteMessageText("Error finding peers: " & err.Message & vbCrLf)
    End Try
End Sub

Private Async Sub ConnectToPeer(peerInfo As Windows.Networking.Proximity.PeerInformation)

    WriteMessageText("Peer found. Connecting to " & peerInfo.DisplayName & vbCrLf)
    Try
        Dim socket = Await Windows.Networking.Proximity.PeerFinder.ConnectAsync(peerInfo)

        WriteMessageText("Connection successful. You may now send messages." & vbCrLf)
        SendMessage(socket)
    Catch err As Exception
        WriteMessageText("Connection failed: " & err.Message & vbCrLf)
    End Try

    requestingPeer = Nothing
End Sub

' Click event handler for "Stop" button.
Private Sub StopFindingPeersButton_Click()

    _started = False
    Windows.Networking.Proximity.PeerFinder.Stop()
    If proximitySocket IsNot Nothing Then CloseSocket()
End Sub

' Handle external connection requests.
Private requestingPeer As Windows.Networking.Proximity.PeerInformation

Private Sub ConnectionRequested(sender As Object,
    e As Windows.Networking.Proximity.ConnectionRequestedEventArgs)

    WriteMessageText("Connection requested by " & requestingPeer.DisplayName & ". " &
        "Click 'Accept Connection' to connect.")

    requestingPeer = e.PeerInformation
End Sub

Private Sub AcceptConnectionButton_Click()
    If requestingPeer Is Nothing Then
        WriteMessageText("No peer connection has been requested.")
        Return
    End If

    ConnectToPeer(requestingPeer)
End Sub


要件

最小限サポートされるクライアント

Windows 8 [Windows ストア アプリのみ]

最小限サポートされるサーバー

Windows Server 2012 [Windows ストア アプリのみ]

最小限サポートされる携帯電話

Windows Phone 8

名前空間

Windows.Networking.Proximity
Windows::Networking::Proximity [C++]

メタデータ

Windows.winmd

Capabilities

proximity
ID_CAP_PROXIMITY [Windows Phone]
ID_CAP_NETWORKING [Windows Phone]

参照

近接通信とタップ (JavaScript)
近接通信とタップ (C#/VB/C++)
サンプル
近接通信のサンプル

 

 

表示: