PeerFinder.CreateWatcher | createWatcher method

Applies to Windows and Windows Phone

Creates a new instance of a PeerWatcher object for dynamic discovery of peer apps.

Syntax


var peerWatcher = Windows.Networking.Proximity.PeerFinder.createWatcher();

Parameters

This method has no parameters.

Return value

Type: PeerWatcher

An instance of a PeerWatcher object for dynamic discovery of peer apps.

Remarks

You must first call the Start method before calling the CreateWatcher method.

If you call the CreateWatcher method multiple times, you will receive a reference to the same PeerWatcher instance.


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

Minimum supported server

Windows Server 2012 R2

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

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

 

 

Show:
© 2014 Microsoft