// Handles PeerFinder_AdvertiseButton click void PeerFinder_StartAdvertising(object sender, RoutedEventArgs e) { // If PeerFinder is started, stop it, so that new properties // selected by the user (Role/DiscoveryData) can be updated. if (_peerFinderStarted) { PeerFinder.Stop(); _peerFinderStarted = false; } rootPage.NotifyUser("", NotifyType.ErrorMessage); if (!_peerFinderStarted) { // attach the callback handler (there can only be one PeerConnectProgress handler). PeerFinder.TriggeredConnectionStateChanged += new TypedEventHandler <object, TriggeredConnectionStateChangedEventArgs>(TriggeredConnectionStateChangedEventHandler); // attach the incoming connection request event handler PeerFinder.ConnectionRequested += new TypedEventHandler <object, ConnectionRequestedEventArgs>(PeerConnectionRequested); // Set the PeerFinder.Role property // NOTE: this has no effect on the Phone platform if (_launchByTap) { PeerFinder.Role = rootPage.GetLaunchRole(); } else { if (PeerFinder_SelectRole.SelectionBoxItem != null) { switch (PeerFinder_SelectRole.SelectionBoxItem.ToString()) { case "Peer": PeerFinder.Role = PeerRole.Peer; break; case "Host": PeerFinder.Role = PeerRole.Host; break; case "Client": PeerFinder.Role = PeerRole.Client; break; } } } // Set DiscoveryData property if the user entered some text // NOTE: this has no effect on the Phone platform if ((PeerFinder_DiscoveryData.Text.Length > 0) && (PeerFinder_DiscoveryData.Text != "What's happening today?")) { using (var discoveryDataWriter = new Windows.Storage.Streams.DataWriter(new Windows.Storage.Streams.InMemoryRandomAccessStream())) { discoveryDataWriter.WriteString(PeerFinder_DiscoveryData.Text); PeerFinder.DiscoveryData = discoveryDataWriter.DetachBuffer(); } } // start listening for proximate peers PeerFinder.Start(); _peerFinderStarted = true; ToggleAdvertiseControls(true); ShowStartAdvertiseControls(); if (_browseConnectSupported && _triggeredConnectSupported) { rootPage.NotifyUser("Tap another device to connect to a peer or click Browse for Peers button.", NotifyType.StatusMessage); PeerFinder_BrowsePeersButton.Visibility = Visibility.Visible; } else if (_triggeredConnectSupported) { rootPage.NotifyUser("Tap another device to connect to a peer.", NotifyType.StatusMessage); } else if (_browseConnectSupported) { rootPage.NotifyUser("Click Browse for Peers button.", NotifyType.StatusMessage); PeerFinder_BrowsePeersButton.Visibility = Visibility.Visible; } } }