PeerWatcher class

Applies to Windows and Windows Phone

Dynamically discovers peer apps within wireless range.

Syntax


var peerWatcher = PeerFinder.CreateWatcher();

Attributes

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

Members

The PeerWatcher class has these types of members:

Events

The PeerWatcher class has these events.

EventDescription
Added Occurs when a peer app is found within wireless range.
EnumerationCompleted Occurs after a scan operation is complete and all peer apps within wireless range have been found.
Removed Occurs when a peer app is no longer within wireless range.
Stopped Occurs when the PeerWatcher object has been stopped.
Updated Occurs when the DisplayName or DiscoveryData for a peer app within wireless range has changed.

 

Methods

The PeerWatcher class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

MethodDescription
Start Begin dynamically searching for peer apps within wireless range.
Stop Stop dynamically searching for peer apps within wireless range.

 

Properties

The PeerWatcher class has these properties.

PropertyAccess typeDescription

Status

Read-onlyGets the current state of the PeerWatcher object.

 

Remarks

You can use the FindAllPeersAsync method to get a list of all peers within range. However, the FindAllPeersAsync method scans for peers once and then completes. Alternatively, you can use the PeerWatcher class to scan for peers and get updates as they are found and incrementally update your list of available peer apps. The PeerWatcher continuously scans for new peer apps within range and removes stale peer apps. You can update your list of peer apps by handling the Added event, which occurs when a new peer app is found, and the Removed event which occurs when a stale peer app is removed. The PeerWatcher continues to scan until you call the Stop method, or the PeerFinder.FindAllPeersAsync or PeerFinder.ConnectAsync methods.

To create an instance of the PeerWatcher class, call the PeerFinder.Start method, and then call the CreateWatcher method.


var _peerWatcher;
var _peerWatcherIsRunning = false;
var _peerFinderStarted = false;

// The list of peers discovered by the PeerWatcher.
var _discoveredPeers = new Array();

function peerFinder_StartPeerWatcher() {
    if (!_peerFinderStarted) {
        // PeerFinder must be started first.
       return;
    }

    if (_peerWatcherIsRunning) {
        // PeerWatcher is already running.
        return;
    }

    try {
        if (!_peerWatcher)
        {
            _peerWatcher = PeerFinder.createWatcher();

            // Add PeerWatcher event handlers. Only add handlers once.
            peerWatcher.addEventListener("added", peerWatcher_Added); 
            peerWatcher.addEventListener("removed", peerWatcher_Removed); 
            peerWatcher.addEventListener("updated",  peerWatcher_Updated); 
            peerWatcher.addEventListener("enumerationcompleted", peerWatcher_EnumerationCompleted); 
            peerWatcher.addEventListener("stopped", peerWatcher_Stopped); 
        }

        // Empty the list of discovered peers.
        _discoveredPeers = new Array();

        // Start the PeerWatcher.
        _peerWatcher.start();

        _peerWatcherIsRunning = true;
    }
    catch (ex)
    {
        // Exceptions can occur if PeerWatcher.Start is called multiple times or
        // PeerWatcher.Start is called the PeerWatcher is stopping.
    }
}
    
function cleanup() {
    if (_peerWatcher) {
        // Remove event handlers.
        peerWatcher.removeEventListener("added", peerWatcher_Added); 
        peerWatcher.removeEventListener("removed", peerWatcher_Removed); 
        peerWatcher.removeEventListener("updated", peerWatcher_Updated); 
        peerWatcher.removeEventListener("enumerationCompleted", peerWatcher_EnumerationCompleted); 
        peerWatcher.removeEventListener("stopped", peerWatcher_Stopped); 

        _peerWatcher = null;
    }
}


Requirements

Minimum supported client

Windows 8.1 [Windows Store apps only]

Minimum supported server

Windows Server 2012 R2 [Windows Store apps only]

Minimum supported phone

Windows Phone 8.1 [Windows Phone Silverlight 8.1 and Windows Runtime apps]

Namespace

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

Metadata

Windows.winmd

See also

Proximity and Tapping (JavaScript)
Proximity and Tapping (C#/VB/C++)
Samples
Proximity sample

 

 

Show:
© 2014 Microsoft. All rights reserved.