void StartPeerWatcher() { if (peerWatcher == null) { peerWatcher = PeerFinder.CreateWatcher(); // Hook up events, this should only be done once peerWatcher.Added += PeerWatcher_Added; peerWatcher.Removed += PeerWatcher_Removed; peerWatcher.Updated += PeerWatcher_Updated; peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted; peerWatcher.Stopped += PeerWatcher_Stopped; } PeerWatcherStatus status = peerWatcher.Status; if (status == PeerWatcherStatus.Created || status == PeerWatcherStatus.Stopped || status == PeerWatcherStatus.Aborted) { try { foundPeers.SelectionChanged -= PeersSelectionChanged; availablePeers.Clear(); foundPeers.ItemsSource = availablePeers; noPeersFound.Visibility = Visibility.Collapsed; foundPeers.SelectionChanged += PeersSelectionChanged; peerWatcher.Start(); progressBar.Visibility = Visibility.Visible; } catch (Exception err) { proximityStatus.Text = "Error starting PeerWatcher: " + err.ToString(); } } }
private static StreamSocket RunAsClient(string localPeerDisplayName, string remotePeerDisplayName) { PeerFinder.DisplayName = localPeerDisplayName; PeerFinder.AllowInfrastructure = false; PeerFinder.AllowBluetooth = false; PeerFinder.AllowWiFiDirect = true; PeerFinder.Start(); PeerWatcher watcher = PeerFinder.CreateWatcher(); watcher.Added += (sender, peerInfo) => { if (peerInfo.DisplayName == remotePeerDisplayName) { SetRemotePeerInformation(peerInfo); } }; watcher.Start(); Task <StreamSocket> task = Task.Run(() => { return((MResetEvent.WaitOne(30000)) ? PeerFinder.ConnectAsync(RemotePeerInformation).AsTask().Result : throw new Exception("Connect Timeout")); }); EllipsisAnimation(task.Wait, "Connecting"); PeerFinder.Stop(); return(task.Result); }
protected async override void OnNavigatedTo(NavigationEventArgs e) { PeerFinder.AllowBluetooth = true; PeerFinder.AllowWiFiDirect = true; PeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested; PeerFinder.TriggeredConnectionStateChanged += PeerFinder_TriggeredConnectionStateChanged; PeerFinder.Start(); _peerWatcher = PeerFinder.CreateWatcher(); _peerWatcher.Added += _peerWatcher_Added; _peerWatcher.Removed += _peerWatcher_Removed; _peerWatcher.EnumerationCompleted += _peerWatcher_EnumerationCompleted; _peerWatcher.Updated += _peerWatcher_Updated; _peerWatcher.Start(); try { var allpeer = await PeerFinder.FindAllPeersAsync(); this.listviewAllDevice.ItemsSource = allpeer; } catch (Exception ex) { this.textboxDebug.Text += ex.Message + "\n"; } }
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. } }
// Handles PeerFinder_StartPeerWatcherButton click void PeerFinder_StartPeerWatcher(object sender, RoutedEventArgs e) { if (_peerWatcherIsRunning) { rootPage.UpdateLog("Can't start PeerWatcher while it is running!", PeerFinderOutputText); return; } rootPage.NotifyUser("Starting PeerWatcher...", NotifyType.StatusMessage); try { if (_peerWatcher == null) { _peerWatcher = PeerFinder.CreateWatcher(); // Hook up events, this should only be done once _peerWatcher.Added += PeerWatcher_Added; _peerWatcher.Removed += PeerWatcher_Removed; _peerWatcher.Updated += PeerWatcher_Updated; _peerWatcher.EnumerationCompleted += PeerWatcher_EnumerationCompleted; _peerWatcher.Stopped += PeerWatcher_Stopped; } _discoveredPeers.Clear(); PeerFinder_PeerListNoPeers.Visibility = Visibility.Collapsed; PeerFinder_ConnectButton.Visibility = Visibility.Visible; _peerWatcher.Start(); _peerWatcherIsRunning = true; rootPage.UpdateLog("PeerWatcher is running!", PeerFinderOutputText); PeerFinder_StartPeerWatcherButton.Visibility = Visibility.Collapsed; PeerFinder_StopPeerWatcherButton.Visibility = Visibility.Visible; } catch (Exception ex) { // This could happen if the user clicks start multiple times or tries to start while PeerWatcher is stopping Debug.WriteLine("PeerWatcher.Start throws exception" + ex.Message); } }
private void StartWatching() { if (peerWatcher == null) { peerWatcher = PeerFinder.CreateWatcher(); } peerWatcher.Added += (PeerWatcher sender, PeerInformation peer) => { Dispatcher.BeginInvoke(() => { peerApps.Add(new PeerAppInfo(peer)); }); }; peerWatcher.Removed += (PeerWatcher sender, PeerInformation peer) => { Dispatcher.BeginInvoke(() => { foreach (var peerApp in peerApps) { if (peerApp.PeerInfo.Equals(peer)) { peerApps.Remove(peerApp); break; } } }); }; peerWatcher.Start(); }