信息
您所需的主题如下所示。但此主题未包含在此库中。

PeerFinder.FindAllPeersAsync | findAllPeersAsync Method

适用于无线范围内运行相同应用程序的对等计算机的异步浏览。

语法


Windows.Networking.Proximity.PeerFinder.findAllPeersAsync().done( /* 你的成功和错误处理程序 */ );

参数

此 方法 没有参数。

返回值

类型: IAsyncOperation<IVectorView>

通过使用 Wi-Fi Direct 浏览对等类的异步操作。

备注

在异步浏览操作成功完成后,返回位于无线范围内对等类列表。对等类是使用匹配应用程序 ID 在前台运行应用程序的计算机。对等类也可以具有一个指定为替代标识的匹配浏览 ID。有关更多信息,请参见 AlternateIdentities

可以使用 SupportedDiscoveryTypes 属性确定是否支持浏览使用 Wi-Fi Direct 对等类。

如果一个应用程序调用 ConnectAsync 方法创建一个与对等的连接,则该应用程序将不再进行连接播发,并且不会被 FindAllPeersAsync 方法发现,直到该应用程序调用 Close 方法关闭套接字连接为止。

只将找到计算机在无线范围内的对等类,然后对等类应用程序前景运行。如果某个对等应用程序正在后台运行,则接近不会进行对等连接播发。

作为 FindAllPeersAsync 方法的替代,可以使用 PeerWatcher 对象动态搜索对等类(当它们在范围中被发现时)。

Windows Phone 8

Wi-Fi Direct 在 Windows Phone 8 上不支持。有关更多信息,请参见邻近感应

示例


var started = false;

// Click event for "Advertise" button.
function advertiseForPeers(launchedFromTap) {
    Windows.Networking.Proximity.PeerFinder.displayName = displayNameTextBox.Text;

    if (Windows.Networking.Proximity.PeerFinder.supportedDiscoveryTypes &
        Windows.Networking.Proximity.PeerDiscoveryTypes.triggered) {

        Windows.Networking.Proximity.PeerFinder.addEventListener(
            "triggeredconnectionstatechanged", triggeredConnectionStateChanged);

        id("messageDiv").innerHTML +=
            "You can tap to connect a peer device that is " +
            "also advertising for a connection.<br />";
    } else {
        id("messageDiv").innerHTML +=
            "Tap to connect is not supported.<br />";
    }

    if (!launchedFromTap) {
        if (!(Windows.Networking.Proximity.PeerFinder.SupportedDiscoveryTypes &
              Windows.Networking.Proximity.PeerDiscoveryTypes.Browse)) {
            id("messageDiv").innerHTML +=
                "Peer discovery using Wi-Fi Direct is not supported.<br />";
        }
    }

    if (!started) {
        Windows.Networking.Proximity.PeerFinder.start();
        started = true;
    }
}

function triggeredConnectionStateChanged(e) {
    if (e.state === Windows.Networking.Proximity.TriggeredConnectState.peerFound) {
        id("messageDiv").innerHTML +=
            "Peer found. You may now pull your devices out of proximity.<br />";
    }
    if (e.state === Windows.Networking.Proximity.TriggeredConnectState.completed) {
        id("messageDiv").innerHTML += "Connected. You may now send a message.<br />";
        sendMessage(e.socket);
    }
}


// Click event for "Browse" button.
function findPeers() {
    if (Windows.Networking.Proximity.PeerFinder.supportedDiscoveryTypes &
        Windows.Networking.Proximity.PeerDiscoveryTypes.browse) {

        Windows.Networking.Proximity.PeerFinder.findAllPeersAsync().done(
    function (peerInfoCollection) {
        if (peerInfoCollection.length > 0) {
            // Connect to first peer found - example only.
            // In your app, provide the user with a list of available peers.
            connectToPeer(peerInfoCollection[0]);
        }
    },
    function (err) {
        id("messageDiv").innerHTML += "Error finding peers: " + err + "<br />";
    });
    } else {
        id("messageDiv").innerHTML +=
        "Peer discovery using Wi-Fi Direct is not supported.<br />";
    }
}

function connectToPeer(peerInfo) {
    id("messageDiv").innerHTML += ("Peer found. Connecting to " + peerInfo.displayName + "<br />");
    Windows.Networking.Proximity.PeerFinder.connectAsync(peerInfo).done(
        function (socket) {
            id("messageDiv").innerHTML += "Connection successful. You may now send messages.<br />";
            sendMessage(socket);
        },
        function (err) {
            id("messageDiv").innerHTML += "Connection failed: " + err + "<br />";
        });

    requestingPeer = null;
}

function stopFindingPeers() {
    Windows.Networking.Proximity.PeerFinder.stop();
    started = false;
    if (proximitySocket != null) { closeSocket(); }
}

// Handle external connection requests.
var requestingPeer;

function connectionRequested(e) {
    id("messageDiv").innerHTML +=
        "Connection requested by " + e.peerInformation.DisplayName + ". " +
        "Click 'Accept Connection' to connect.";
    requestingPeer = e.PeerInformation;
}

function acceptConnection() {
    if (requestingPeer == null) {
        id("messageDiv").innerHTML += "No peer connection has been requested.";
        return;
    }

    connectToPeer(requestingPeer);
}


要求

最低支持客户端

Windows 8

最低支持服务器

Windows Server 2012

最低支持电话

Windows Phone 8

命名空间

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

元数据

Windows.winmd

Capabilities

proximity
ID_CAP_PROXIMITY [Windows Phone]
ID_CAP_NETWORKING [Windows Phone]

另请参见

PeerFinder
AlternateIdentities
SupportedDiscoveryTypes
PeerWatcher
接近程度和轻击 (JavaScript)
接近程度和轻击 (C#/VB/C++)
示例
接近程度示例

 

 

显示: