Delen via


PeerWatcher Class

Definition

Dynamically discovers peer apps within wireless range.

public ref class PeerWatcher sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class PeerWatcher final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class PeerWatcher
Public NotInheritable Class PeerWatcher
Inheritance
Object Platform::Object IInspectable PeerWatcher
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)
App capabilities
proximity

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.

Important

For Windows Phone 8.x apps, calling PeerFinder.ConnectAsync 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 PeerFinder.Start 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;
    }
}

Properties

Status

Gets the current state of the PeerWatcher object.

Methods

Start()

Begin dynamically searching for peer apps within wireless range.

Stop()

Stop dynamically searching for peer apps within wireless range.

Events

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.

Applies to

See also