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

PeerFinder.TriggeredConnectionStateChanged | triggeredconnectionstatechanged Event

在远程对等类的轻击笔势期间发生。

语法


function onTriggeredConnectionStateChanged(eventArgs) { /* 代码 */ }
 
// addEventListener syntax
peerFinder.addEventListener("triggeredconnectionstatechanged", onTriggeredConnectionStateChanged);
peerFinder.removeEventListener("triggeredconnectionstatechanged", onTriggeredConnectionStateChanged);
 
- or -

peerFinder.ontriggeredconnectionstatechanged = onTriggeredConnectionStateChanged;


事件信息

委托 TypedEventHandler<Object, TriggeredConnectionStateChangedEventArgs>

备注

通过使用 TriggeredConnectionStateChanged 事件,您可以确定对等类何时可连接,以及连接的进度到对等类。使用 TriggeredConnectionStateChanged 事件来确定发现新对等类时间以及在完成连接之后访问与该对等类的连接。

在轻击笔势期间,TriggeredConnectionStateChanged 事件发生若干次。使用 State 属性,您可以确定在连接处理中的位置。

接近程度连接中的第一个 State 值为 PeerFound。此时,您可以告诉应用程序的用户邻近笔势是完整的,他们可以把他们的设备放在一边。建立连接的剩余的工作使用类似 TCP/IP 或 Bluetooth 的不同传输。

如果 State 属性返回 Connecting,则您知道您的设备启动了点击笔势。如果 State 属性返回 Listening,则您知道另一设备启动了点击笔势。此信息有助于决定在连接完成时应由哪个对等类启动通信。

State 属性返回 Completed 时,可以访问 Socket 属性来获取引用到 StreamSocket 对象。可以使用此引用与持久传输 likeTCP/IP 或 Bluetooth 对等类应用程序通信。

示例


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
接近程度和轻击 (JavaScript)
接近程度和轻击 (C#/VB/C++)
示例
接近程度示例

 

 

显示: