PeerWatcher PeerWatcher PeerWatcher PeerWatcher Class

Dynamically discovers peer apps within wireless range.

Syntax

Declaration

public sealed class PeerWatcherpublic sealed class PeerWatcherPublic NotInheritable Class PeerWatcher

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 FindAllPeersAsync() or ConnectAsync(Windows.Networking.Proximity.PeerInformation) methods.

Important

For Windows Phone Store app, calling ConnectAsync(Windows.Networking.Proximity.PeerInformation) from within an EnumerationCompleted, Added or Updated event handler will fail. Instead, call it outside of these event handlers, for example, when the user has explicitly chosen to connect to a peer.

To create an instance of the PeerWatcher class, call the Start(System.String) method, and then call the CreateWatcher() method.

private PeerWatcher _peerWatcher;
private bool _peerWatcherIsRunning = false;
private bool _peerFinderStarted = false;

// The list of peers discovered by the PeerWatcher.
ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
{
    if (!_peerFinderStarted)
    {
        // PeerFinder must be started first.
        return;
    }

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

    try
    {
        if (_peerWatcher == null)
        {
            _peerWatcher = PeerFinder.CreateWatcher();

            // Add PeerWatcher event handlers. Only add handlers once.
            _peerWatcher.Added += PeerWatcher_Added;
            _peerWatcher.Removed += PeerWatcher_Removed;
            _peerWatcher.Updated += PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped += PeerWatcher_Stopped;
        }

        // Empty the list of discovered peers.
        _discoveredPeers.Clear();

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

        _peerWatcherIsRunning = true;
    }
    catch (Exception ex)
    {
        // Exceptions can occur if PeerWatcher.Start is called multiple times or
        // PeerWatcher.Start is called the PeerWatcher is stopping.
    }
}

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
    if (_peerWatcher != null)
    {
        // Remove event handlers.
        _peerWatcher.Added -= PeerWatcher_Added;
        _peerWatcher.Removed -= PeerWatcher_Removed;
        _peerWatcher.Updated -= PeerWatcher_Updated;
        _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
        _peerWatcher.Stopped -= PeerWatcher_Stopped;

        _peerWatcher = null;
    }
}
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;
    }
}

Properties summary

Gets the current state of the PeerWatcher object.

Methods summary

Begin dynamically searching for peer apps within wireless range.

Stop dynamically searching for peer apps within wireless range.

Events summary

Occurs when a peer app is found within wireless range.

Occurs after a scan operation is complete and all peer apps within wireless range have been found.

Occurs when a peer app is no longer within wireless range.

Occurs when the PeerWatcher object has been stopped.

Occurs when the DisplayName or DiscoveryData for a peer app within wireless range has changed.

Properties

Methods

  • Start()
    Start()
    Start()
    Start()

    Begin dynamically searching for peer apps within wireless range.

    public void Start()public void Start()Public Function Start() As void

    Remarks

    You must register an event handler for Added event before calling the Start() method.

    You can call the Stop() method to stop dynamically searching for peer apps within wireless range.

    You can use the Status property to determine the status of the PeerWatcher object. You cannot call the Start() method if the Status value is Started, EnumerationCompleted, or Stopping.

    private PeerWatcher _peerWatcher;
    private bool _peerWatcherIsRunning = false;
    private bool _peerFinderStarted = false;
    
    // The list of peers discovered by the PeerWatcher.
    ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
    void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
    {
        if (!_peerFinderStarted)
        {
            // PeerFinder must be started first.
            return;
        }
    
        if (_peerWatcherIsRunning)
        {
            // PeerWatcher is already running.
            return;
        }
    
        try
        {
            if (_peerWatcher == null)
            {
                _peerWatcher = PeerFinder.CreateWatcher();
    
                // Add PeerWatcher event handlers. Only add handlers once.
                _peerWatcher.Added += PeerWatcher_Added;
                _peerWatcher.Removed += PeerWatcher_Removed;
                _peerWatcher.Updated += PeerWatcher_Updated;
                _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
                _peerWatcher.Stopped += PeerWatcher_Stopped;
            }
    
            // Empty the list of discovered peers.
            _discoveredPeers.Clear();
    
            // Start the PeerWatcher.
            _peerWatcher.Start();
    
            _peerWatcherIsRunning = true;
        }
        catch (Exception ex)
        {
            // Exceptions can occur if PeerWatcher.Start is called multiple times or
            // PeerWatcher.Start is called the PeerWatcher is stopping.
        }
    }
    
    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if (_peerWatcher != null)
        {
            // Remove event handlers.
            _peerWatcher.Added -= PeerWatcher_Added;
            _peerWatcher.Removed -= PeerWatcher_Removed;
            _peerWatcher.Updated -= PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped -= PeerWatcher_Stopped;
    
            _peerWatcher = null;
        }
    }
    
    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;
        }
    }
    
  • Stop()
    Stop()
    Stop()
    Stop()

    Stop dynamically searching for peer apps within wireless range.

    public void Stop()public void Stop()Public Function Stop() As void

    Remarks

    private PeerWatcher _peerWatcher;
    private bool _peerWatcherIsRunning = false;
    private bool _peerFinderStarted = false;
    
    // The list of peers discovered by the PeerWatcher.
    ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
    void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
    {
        if (!_peerFinderStarted)
        {
            // PeerFinder must be started first.
            return;
        }
    
        if (_peerWatcherIsRunning)
        {
            // PeerWatcher is already running.
            return;
        }
    
        try
        {
            if (_peerWatcher == null)
            {
                _peerWatcher = PeerFinder.CreateWatcher();
    
                // Add PeerWatcher event handlers. Only add handlers once.
                _peerWatcher.Added += PeerWatcher_Added;
                _peerWatcher.Removed += PeerWatcher_Removed;
                _peerWatcher.Updated += PeerWatcher_Updated;
                _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
                _peerWatcher.Stopped += PeerWatcher_Stopped;
            }
    
            // Empty the list of discovered peers.
            _discoveredPeers.Clear();
    
            // Start the PeerWatcher.
            _peerWatcher.Start();
    
            _peerWatcherIsRunning = true;
        }
        catch (Exception ex)
        {
            // Exceptions can occur if PeerWatcher.Start is called multiple times or
            // PeerWatcher.Start is called the PeerWatcher is stopping.
        }
    }
    
    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if (_peerWatcher != null)
        {
            // Remove event handlers.
            _peerWatcher.Added -= PeerWatcher_Added;
            _peerWatcher.Removed -= PeerWatcher_Removed;
            _peerWatcher.Updated -= PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped -= PeerWatcher_Stopped;
    
            _peerWatcher = null;
        }
    }
    
    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;
        }
    }
    
    void PeerFinder_StopPeerWatcher(object sender, RoutedEventArgs e)
    {
        try
        {
            _peerWatcher.Stop();
        }
        catch (Exception ex)
        {
            // Handle the exception.
        }
    }
    
    function peerFinder_StopPeerWatcher() {
        try {
            _peerWatcher.Stop();
        }
        catch (ex) {
            // Handle the exception.
        }
    }
    

Events

  • Added
    Added
    Added
    Added

    Occurs when a peer app is found within wireless range.

    public event TypedEventHandler Addedpublic event TypedEventHandler AddedPublic Event Added

    Remarks

    Important

    For Windows Phone Store app, calling ConnectAsync(Windows.Networking.Proximity.PeerInformation) from within an Added event handler will fail. Instead, call it outside of this event handler, for example, when the user has explicitly chosen to connect to a peer.

    After you start the PeerWatcher by calling the Start(System.String) method, the Added event is raised for each peer app found within wireless range. The PeerWatcher continues scanning for peer apps within range until you call the Stop() method. Using the Added event, you can display peer apps to the user as they are found.

    private PeerWatcher _peerWatcher;
    private bool _peerWatcherIsRunning = false;
    private bool _peerFinderStarted = false;
    
    // The list of peers discovered by the PeerWatcher.
    ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
    void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
    {
        if (!_peerFinderStarted)
        {
            // PeerFinder must be started first.
            return;
        }
    
        if (_peerWatcherIsRunning)
        {
            // PeerWatcher is already running.
            return;
        }
    
        try
        {
            if (_peerWatcher == null)
            {
                _peerWatcher = PeerFinder.CreateWatcher();
    
                // Add PeerWatcher event handlers. Only add handlers once.
                _peerWatcher.Added += PeerWatcher_Added;
                _peerWatcher.Removed += PeerWatcher_Removed;
                _peerWatcher.Updated += PeerWatcher_Updated;
                _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
                _peerWatcher.Stopped += PeerWatcher_Stopped;
            }
    
            // Empty the list of discovered peers.
            _discoveredPeers.Clear();
    
            // Start the PeerWatcher.
            _peerWatcher.Start();
    
            _peerWatcherIsRunning = true;
        }
        catch (Exception ex)
        {
            // Exceptions can occur if PeerWatcher.Start is called multiple times or
            // PeerWatcher.Start is called the PeerWatcher is stopping.
        }
    }
    
    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if (_peerWatcher != null)
        {
            // Remove event handlers.
            _peerWatcher.Added -= PeerWatcher_Added;
            _peerWatcher.Removed -= PeerWatcher_Removed;
            _peerWatcher.Updated -= PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped -= PeerWatcher_Stopped;
    
            _peerWatcher = null;
        }
    }
    
    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;
        }
    }
    
    private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo)
    {
        var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
        {
            lock (this)
            {
                _discoveredPeers.Add(peerInfo);
            }
        });
    }
    
    function peerWatcher_Added(peerInfo) {
        _discoveredPeers.Add(peerInfo);
    }
    
    function peerWatcher_Added(peerInfo) {
        _discoveredPeers.Add(peerInfo);
    }
    
  • EnumerationCompleted
    EnumerationCompleted
    EnumerationCompleted
    EnumerationCompleted

    Occurs after a scan operation is complete and all peer apps within wireless range have been found.

    public event TypedEventHandler EnumerationCompletedpublic event TypedEventHandler EnumerationCompletedPublic Event EnumerationCompleted

    Remarks

    EnumerationCompleted event is raised when an enumeration looking for peer apps within range is complete.

    Important

    For Windows Phone Store app, calling ConnectAsync(Windows.Networking.Proximity.PeerInformation) from within an EnumerationCompleted event handler will fail. Instead, call it outside of this event handler, for example, when the user has explicitly chosen to connect to a peer.

    private PeerWatcher _peerWatcher;
    private bool _peerWatcherIsRunning = false;
    private bool _peerFinderStarted = false;
    
    // The list of peers discovered by the PeerWatcher.
    ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
    void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
    {
        if (!_peerFinderStarted)
        {
            // PeerFinder must be started first.
            return;
        }
    
        if (_peerWatcherIsRunning)
        {
            // PeerWatcher is already running.
            return;
        }
    
        try
        {
            if (_peerWatcher == null)
            {
                _peerWatcher = PeerFinder.CreateWatcher();
    
                // Add PeerWatcher event handlers. Only add handlers once.
                _peerWatcher.Added += PeerWatcher_Added;
                _peerWatcher.Removed += PeerWatcher_Removed;
                _peerWatcher.Updated += PeerWatcher_Updated;
                _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
                _peerWatcher.Stopped += PeerWatcher_Stopped;
            }
    
            // Empty the list of discovered peers.
            _discoveredPeers.Clear();
    
            // Start the PeerWatcher.
            _peerWatcher.Start();
    
            _peerWatcherIsRunning = true;
        }
        catch (Exception ex)
        {
            // Exceptions can occur if PeerWatcher.Start is called multiple times or
            // PeerWatcher.Start is called the PeerWatcher is stopping.
        }
    }
    
    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if (_peerWatcher != null)
        {
            // Remove event handlers.
            _peerWatcher.Added -= PeerWatcher_Added;
            _peerWatcher.Removed -= PeerWatcher_Removed;
            _peerWatcher.Updated -= PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped -= PeerWatcher_Stopped;
    
            _peerWatcher = null;
        }
    }
    
    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;
        }
    }
    
    private void PeerWatcher_EnumerationCompleted(PeerWatcher sender, object o)
    {
        var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
        {
            lock (this)
            {
                if (_discoveredPeers.Count == 0)
                {
                    // No peers discovered for this enumeration.
                }
            }
        });
    }
    
    function peerWatcher_EnumerationCompleted()
    {
        if (_discoveredPeers.length == 0)
        {
            // No peers discovered for this enumeration.
        }
    }
    
  • Removed
    Removed
    Removed
    Removed

    Occurs when a peer app is no longer within wireless range.

    public event TypedEventHandler Removedpublic event TypedEventHandler RemovedPublic Event Removed

    Remarks

    The Removed event is raised when a peer app is removed from the collection of enumerated peer apps. This may happen if a user switches apps or if a device is no longer within range.

    private PeerWatcher _peerWatcher;
    private bool _peerWatcherIsRunning = false;
    private bool _peerFinderStarted = false;
    
    // The list of peers discovered by the PeerWatcher.
    ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
    void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
    {
        if (!_peerFinderStarted)
        {
            // PeerFinder must be started first.
            return;
        }
    
        if (_peerWatcherIsRunning)
        {
            // PeerWatcher is already running.
            return;
        }
    
        try
        {
            if (_peerWatcher == null)
            {
                _peerWatcher = PeerFinder.CreateWatcher();
    
                // Add PeerWatcher event handlers. Only add handlers once.
                _peerWatcher.Added += PeerWatcher_Added;
                _peerWatcher.Removed += PeerWatcher_Removed;
                _peerWatcher.Updated += PeerWatcher_Updated;
                _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
                _peerWatcher.Stopped += PeerWatcher_Stopped;
            }
    
            // Empty the list of discovered peers.
            _discoveredPeers.Clear();
    
            // Start the PeerWatcher.
            _peerWatcher.Start();
    
            _peerWatcherIsRunning = true;
        }
        catch (Exception ex)
        {
            // Exceptions can occur if PeerWatcher.Start is called multiple times or
            // PeerWatcher.Start is called the PeerWatcher is stopping.
        }
    }
    
    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if (_peerWatcher != null)
        {
            // Remove event handlers.
            _peerWatcher.Added -= PeerWatcher_Added;
            _peerWatcher.Removed -= PeerWatcher_Removed;
            _peerWatcher.Updated -= PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped -= PeerWatcher_Stopped;
    
            _peerWatcher = null;
        }
    }
    
    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;
        }
    }
    
    private void PeerWatcher_Removed(PeerWatcher sender, PeerInformation peerInfo)
    {
        var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
        {
            lock (this)
            {
                // Find and remove the peer form the list of discovered peers.
                for (int i = 0; i < _discoveredPeers.Count; i++)
                {
                    if (_discoveredPeers[i].Id == peerInfo.Id)
                    {
                        _discoveredPeers.RemoveAt(i);
                    }
                }
            }
        });
    }
    
    function peerWatcher_Removed(peerInfo)
    {
        // Find and remove the peer form the list of discovered peers.
        var match = null;
        var i;
        _discoveredPeers.forEach(function (e, i) { 
            if (e.id === peerInfo.id) { match = e; index = i; } 
        }); 
        if (match) { 
            _discoveredPeers.splice(index, 1); 
        } 
    }
    
  • Stopped
    Stopped
    Stopped
    Stopped

    Occurs when the PeerWatcher object has been stopped.

    public event TypedEventHandler Stoppedpublic event TypedEventHandler StoppedPublic Event Stopped

    Remarks

    You can call the Stop() method to stop the PeerWatcher object. You can register for the Stopped event to be informed when the PeerWatcher has stopped. You must wait for the Stopped event before you can call the Start() method to restart the PeerWatcher.

    private PeerWatcher _peerWatcher;
    private bool _peerWatcherIsRunning = false;
    private bool _peerFinderStarted = false;
    
    // The list of peers discovered by the PeerWatcher.
    ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
    void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
    {
        if (!_peerFinderStarted)
        {
            // PeerFinder must be started first.
            return;
        }
    
        if (_peerWatcherIsRunning)
        {
            // PeerWatcher is already running.
            return;
        }
    
        try
        {
            if (_peerWatcher == null)
            {
                _peerWatcher = PeerFinder.CreateWatcher();
    
                // Add PeerWatcher event handlers. Only add handlers once.
                _peerWatcher.Added += PeerWatcher_Added;
                _peerWatcher.Removed += PeerWatcher_Removed;
                _peerWatcher.Updated += PeerWatcher_Updated;
                _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
                _peerWatcher.Stopped += PeerWatcher_Stopped;
            }
    
            // Empty the list of discovered peers.
            _discoveredPeers.Clear();
    
            // Start the PeerWatcher.
            _peerWatcher.Start();
    
            _peerWatcherIsRunning = true;
        }
        catch (Exception ex)
        {
            // Exceptions can occur if PeerWatcher.Start is called multiple times or
            // PeerWatcher.Start is called the PeerWatcher is stopping.
        }
    }
    
    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if (_peerWatcher != null)
        {
            // Remove event handlers.
            _peerWatcher.Added -= PeerWatcher_Added;
            _peerWatcher.Removed -= PeerWatcher_Removed;
            _peerWatcher.Updated -= PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped -= PeerWatcher_Stopped;
    
            _peerWatcher = null;
        }
    }
    
    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;
        }
    }
    
    private void PeerWatcher_Stopped(PeerWatcher sender, object o)
    {
        _peerWatcherIsRunning = false;
        var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
        {
            // Update UI now that the PeerWatcher is stopped.
        });
    }
    
    function peerWatcher_Stopped()
    {
        _peerWatcherIsRunning = false;
    
        // Update UI now that the PeerWatcher is stopped.
    }
    
  • Updated
    Updated
    Updated
    Updated

    Occurs when the DisplayName or DiscoveryData for a peer app within wireless range has changed.

    public event TypedEventHandler Updatedpublic event TypedEventHandler UpdatedPublic Event Updated

    Remarks

    The Updated event is raised when the DisplayName or DiscoveryData for a peer app changes. You can use the Id property to uniquely identify the peer app and update your UI.

    Important

    For Windows Phone Store app, calling ConnectAsync(Windows.Networking.Proximity.PeerInformation) from within an Updated event handler will fail. Instead, call it outside of this event handler, for example, when the user has explicitly chosen to connect to a peer.

    private PeerWatcher _peerWatcher;
    private bool _peerWatcherIsRunning = false;
    private bool _peerFinderStarted = false;
    
    // The list of peers discovered by the PeerWatcher.
    ObservableCollection<PeerInformation> _discoveredPeers = new ObservableCollection<PeerInformation>();
    void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e)
    {
        if (!_peerFinderStarted)
        {
            // PeerFinder must be started first.
            return;
        }
    
        if (_peerWatcherIsRunning)
        {
            // PeerWatcher is already running.
            return;
        }
    
        try
        {
            if (_peerWatcher == null)
            {
                _peerWatcher = PeerFinder.CreateWatcher();
    
                // Add PeerWatcher event handlers. Only add handlers once.
                _peerWatcher.Added += PeerWatcher_Added;
                _peerWatcher.Removed += PeerWatcher_Removed;
                _peerWatcher.Updated += PeerWatcher_Updated;
                _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted;
                _peerWatcher.Stopped += PeerWatcher_Stopped;
            }
    
            // Empty the list of discovered peers.
            _discoveredPeers.Clear();
    
            // Start the PeerWatcher.
            _peerWatcher.Start();
    
            _peerWatcherIsRunning = true;
        }
        catch (Exception ex)
        {
            // Exceptions can occur if PeerWatcher.Start is called multiple times or
            // PeerWatcher.Start is called the PeerWatcher is stopping.
        }
    }
    
    protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    {
        if (_peerWatcher != null)
        {
            // Remove event handlers.
            _peerWatcher.Added -= PeerWatcher_Added;
            _peerWatcher.Removed -= PeerWatcher_Removed;
            _peerWatcher.Updated -= PeerWatcher_Updated;
            _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted;
            _peerWatcher.Stopped -= PeerWatcher_Stopped;
    
            _peerWatcher = null;
        }
    }
    
    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;
        }
    }
    
    private void PeerWatcher_Updated(PeerWatcher sender, PeerInformation peerInfo)
    {
        var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
        {
            lock (this)
            {
                // Find and update the peer in the list of discovered peers.
                for (int i = 0; i < _discoveredPeers.Count; i++)
                {
                    if (_discoveredPeers[i].Id == peerInfo.Id)
                    {
                        _discoveredPeers[i] = peerInfo;
                    }
                }
            }
        });
    }
    
    function peerWatcher_Updated(peerInfo)
    {
        // Find and update the peer in the list of discovered peers.
        var match = null;
        var i;
        _discoveredPeers.forEach(function (e, i) { 
            if (e.id === peerInfo.id) { match = e; index = i; } 
        }); 
        if (match) { 
            _discoveredPeers.setAt(index, peerInfo); 
        } 
    }
    

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.ThreadingAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute

Details

Assembly

Windows.Networking.Proximity.dll