// Invoked when the main page navigates to a different scenario protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { // If PeerFinder was started, stop it when navigating to a different page. if (_peerFinderStarted) { // detach the callback handler (there can only be one PeerConnectProgress handler). PeerFinder.TriggeredConnectionStateChanged -= new TypedEventHandler <object, TriggeredConnectionStateChangedEventArgs>(TriggeredConnectionStateChangedEventHandler); // detach the incoming connection request event handler PeerFinder.ConnectionRequested -= new TypedEventHandler <object, ConnectionRequestedEventArgs>(PeerConnectionRequested); PeerFinder.Stop(); _socketHelper.CloseSocket(); _peerFinderStarted = false; } if (_peerWatcher != null) { // unregister for events _peerWatcher.Added -= PeerWatcher_Added; _peerWatcher.Removed -= PeerWatcher_Removed; _peerWatcher.Updated -= PeerWatcher_Updated; _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted; _peerWatcher.Stopped -= PeerWatcher_Stopped; _peerWatcher = null; } }
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 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); }
private void PeerWatcher_Stopped(PeerWatcher sender, object o) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { progressBar.Visibility = Visibility.Collapsed; }); }
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 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 async void _peerWatcher_Added(PeerWatcher sender, PeerInformation args) { await this.textboxDebug.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { this.textboxDebug.Text += "Added\n"; }); }
private async void _peerWatcher_EnumerationCompleted(PeerWatcher sender, object args) { await this.textboxDebug.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { this.textboxDebug.Text += "Enum Completed\n"; }); }
private async void _peerWatcher_EnumerationCompleted(PeerWatcher sender, object args) { await this.textboxDebug.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { this.textboxDebug.Text += "Enum Completed\n"; }); }
private void StopWatching() { if (peerWatcher != null) { peerWatcher.Stop(); peerWatcher = null; } }
private async void _peerWatcher_Updated(PeerWatcher sender, PeerInformation args) { await this.textboxDebug.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { this.textboxDebug.Text += "Updated\n"; }); }
// </Snippet_PeerWatcherEnumerationCompleted> // <Snippet_PeerWatcherStopped> private void PeerWatcher_Stopped(PeerWatcher sender, object o) { _peerWatcherIsRunning = false; var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { // Update UI now that the PeerWatcher is stopped. }); }
// </Snippet_StopPeerWatcher> // <Snippet_PeerWatcherAdded> private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo) { var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { lock (this) { _discoveredPeers.Add(peerInfo); } }); }
// </Snippet_PeerWatcherUpdated> // <Snippet_PeerWatcherEnumerationCompleted> 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. } } }); }
private void PeerWatcher_Stopped(PeerWatcher sender, object o) { // This indicates that the PeerWatcher was stopped explicitly through PeerWatcher.Stop, or it was aborted // The Status property indicates the cause of the event rootPage.UpdateLog("PeerWatcher Stopped. Status: " + _peerWatcher.Status, PeerFinderOutputText); // PeerWatcher is now actually stopped and we can start it again, update the UI button state accordingly _peerWatcherIsRunning = false; var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { PeerFinder_StartPeerWatcherButton.Visibility = Visibility.Visible; PeerFinder_StopPeerWatcherButton.Visibility = Visibility.Collapsed; }); }
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; } }
private void PeerWatcher_EnumerationCompleted(PeerWatcher sender, object o) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (availablePeers) { // Notify the user that no peers were found after we have done an initial scan if (availablePeers.Count == 0 && connectedPeers.Count == 0) { noPeersFound.Visibility = Visibility.Visible; directions.Visibility = Visibility.Collapsed; } } }); }
// PeerWatcher events private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo) { rootPage.UpdateLog("Peer added: " + GetTruncatedPeerId(peerInfo.Id) + ", name:" + peerInfo.DisplayName, PeerFinderOutputText); // Update the UI var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { lock (this) { PeerFinder_PeerListNoPeers.Visibility = Visibility.Collapsed; _discoveredPeers.Add(peerInfo); } }); }
private void PeerWatcher_Updated(PeerWatcher sender, PeerInformation peerInfo) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (availablePeers) { for (int i = 0; i < availablePeers.Count; i++) { if (availablePeers[i].Peer.Id == peerInfo.Id) { availablePeers[i] = new AvailablePeer(peerInfo); } } } }); }
// PeerWatcher events private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo) { // Update the UI var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { PeerFinderOutputText.Text += "Peer added: " + GetTruncatedPeerId(peerInfo.Id) + ", name:" + peerInfo.DisplayName + "\n"; ShowPeerAddedControls(); lock (this) { _discoveredPeers.Add(peerInfo); } }); }
private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (availablePeers) { noPeersFound.Visibility = Visibility.Collapsed; availablePeers.Add(new AvailablePeer(peerInfo)); // Don't overlap NFC text, only show this if the ListView is enabled if (foundPeers.IsEnabled) { directions.Visibility = Visibility.Visible; } } }); }
// </Snippet_PeerWatcherRemoved> // <Snippet_PeerWatcherUpdated> 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; } } } }); }
// </Snippet_PeerWatcherAdded> // <Snippet_PeerWatcherRemoved> 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); } } } }); }
private void PeerWatcher_EnumerationCompleted(PeerWatcher sender, object o) { rootPage.UpdateLog("PeerWatcher Enumeration Completed", PeerFinderOutputText); // All peers that were visible at the start of the scan have been found // Stopping PeerWatcher here is similar to FindAllPeersAsync // Notify the user that no peers were found after we have done an initial scan var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { lock (this) { if (_discoveredPeers.Count == 0) { PeerFinder_PeerListNoPeers.Visibility = Visibility.Visible; } } }); }
private void PeerWatcher_Stopped(PeerWatcher sender, object o) { // This indicates that the PeerWatcher was stopped explicitly through PeerWatcher.Stop, or it was aborted // The Status property indicates the cause of the event // PeerWatcher is now actually stopped and we can start it again, update the UI button state accordingly _peerWatcherIsRunning = false; var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { rootPage.NotifyUser("PeerWatcher stopped", NotifyType.StatusMessage); // If we navigate away from the page, PeerWatcher is stopped and _peerWatcher is set to null if (_peerWatcher != null) { PeerFinderOutputText.Text += "PeerWatcher Stopped. Status: " + _peerWatcher.Status + "\n"; TogglePeerWatcherStartControls(false); } }); }
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. } }
private void PeerWatcher_Removed(PeerWatcher sender, PeerInformation peerInfo) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (availablePeers) { for (int i = 0; i < availablePeers.Count; i++) { if (availablePeers[i].Peer.Id == peerInfo.Id) { availablePeers.RemoveAt(i); } } if (availablePeers.Count == 0) { directions.Visibility = Visibility.Collapsed; } } }); }
private void PeerWatcher_Updated(PeerWatcher sender, PeerInformation peerInfo) { rootPage.UpdateLog("Peer updated: " + GetTruncatedPeerId(peerInfo.Id) + ", name:" + peerInfo.DisplayName, PeerFinderOutputText); // Update the UI var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { lock (this) { for (int i = 0; i < _discoveredPeers.Count; i++) { if (_discoveredPeers[i].Id == peerInfo.Id) { _discoveredPeers[i] = peerInfo; } } } }); }
// 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 PeerWatcher_Removed(PeerWatcher sender, PeerInformation peerInfo) { // Update the UI var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { PeerFinderOutputText.Text += "Peer removed: " + GetTruncatedPeerId(peerInfo.Id) + ", name:" + peerInfo.DisplayName + "\n"; lock (this) { for (int i = 0; i < _discoveredPeers.Count; i++) { if (_discoveredPeers[i].Id == peerInfo.Id) { _discoveredPeers.RemoveAt(i); } } } }); }
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(); }
private void PeerWatcher_Stopped(PeerWatcher sender, object o) { // This indicates that the PeerWatcher was stopped explicitly through PeerWatcher.Stop, or it was aborted // The Status property indicates the cause of the event // PeerWatcher is now actually stopped and we can start it again, update the UI button state accordingly _peerWatcherIsRunning = false; var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { rootPage.NotifyUser("PeerWatcher stopped", NotifyType.StatusMessage); // If we navigate away from the page, PeerWatcher is stopped and _peerWatcher is set to null if (_peerWatcher != null) { PeerFinderOutputText.Text += "PeerWatcher Stopped. Status: " + _peerWatcher.Status + "\n"; TogglePeerWatcherStartControls(false); } }); }
private void PeerWatcher_Removed(PeerWatcher sender, PeerInformation peerInfo) { // Update the UI var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { PeerFinderOutputText.Text += "Peer removed: " + GetTruncatedPeerId(peerInfo.Id) + ", name:" + peerInfo.DisplayName + "\n"; lock (this) { for (int i = 0; i < _discoveredPeers.Count; i++) { if (_discoveredPeers[i].Id == peerInfo.Id) { _discoveredPeers.RemoveAt(i); } } } }); }
private void PeerWatcher_EnumerationCompleted(PeerWatcher sender, object o) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (availablePeers) { // Notify the user that no peers were found after we have done an initial scan if (availablePeers.Count == 0 && connectedPeers.Count == 0) { noPeersFound.Visibility = Visibility.Visible; directions.Visibility = Visibility.Collapsed; } } }); }
private void PeerWatcher_Stopped(PeerWatcher sender, object o) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { progressBar.Visibility = Visibility.Collapsed; }); }
public PeerWatcherEvents(PeerWatcher This) { this.This = This; }
private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo) { var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { lock (this) { discoveredPeers.Add(peerInfo); // now compare to the peerList // splitName[0] contains the pin and the peername, splitName[1] has the longitude and latitudes string[] splitName = peerInfo.DisplayName.Split(displayNameSeperator, StringSplitOptions.None); if (getPIN(splitName[0]) == sessionPin) { // session pins are the same therefore this peer's location information must be refreshed. // on the initial added case, there won't be a coordinate yet. so we check to see if splitname is length 2. if (splitName.Length == 2) { peerLocations[getPeerName(splitName[0])] = getCoordinates(splitName[1]); myMapIcons[getPeerName(splitName[0])].Location = new Geopoint(getGeoposition(splitName[1])); myMapIcons[getPeerName(splitName[0])].Visible = true; } else { // this is the first run so the name won't have the coordinate componenent. in which case we'll give the coordinate 0,0 myMapIcons[getPeerName(splitName[0])].Location = new Geopoint(new BasicGeoposition() { Longitude = 0, Latitude = 0 }); myMapIcons[getPeerName(splitName[0])].Visible = false; } } } }); }
// 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); } }
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. } }
private void PeerWatcher_Updated(PeerWatcher sender, PeerInformation peerInfo) { rootPage.UpdateLog("Peer updated: " + GetTruncatedPeerId(peerInfo.Id) + ", name:" + peerInfo.DisplayName, PeerFinderOutputText); // Update the UI var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { lock (this) { for (int i = 0; i < _discoveredPeers.Count; i++) { if (_discoveredPeers[i].Id == peerInfo.Id) { _discoveredPeers[i] = peerInfo; } } } }); }
// PeerWatcher events private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo) { rootPage.UpdateLog("Peer added: " + GetTruncatedPeerId(peerInfo.Id) + ", name:" + peerInfo.DisplayName, PeerFinderOutputText); // Update the UI var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { lock (this) { PeerFinder_PeerListNoPeers.Visibility = Visibility.Collapsed; _discoveredPeers.Add(peerInfo); } }); }
private void PeerWatcher_EnumerationCompleted(PeerWatcher sender, object o) { rootPage.UpdateLog("PeerWatcher Enumeration Completed", PeerFinderOutputText); // All peers that were visible at the start of the scan have been found // Stopping PeerWatcher here is similar to FindAllPeersAsync // Notify the user that no peers were found after we have done an initial scan var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { lock (this) { if (_discoveredPeers.Count == 0) { PeerFinder_PeerListNoPeers.Visibility = Visibility.Visible; } } }); }
private void PeerWatcher_Stopped(PeerWatcher sender, object o) { // This indicates that the PeerWatcher was stopped explicitly through PeerWatcher.Stop, or it was aborted // The Status property indicates the cause of the event rootPage.UpdateLog("PeerWatcher Stopped. Status: " + _peerWatcher.Status, PeerFinderOutputText); // PeerWatcher is now actually stopped and we can start it again, update the UI button state accordingly _peerWatcherIsRunning = false; var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { PeerFinder_StartPeerWatcherButton.Visibility = Visibility.Visible; PeerFinder_StopPeerWatcherButton.Visibility = Visibility.Collapsed; }); }
// Invoked when the main page navigates to a different scenario protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { // If PeerFinder was started, stop it when navigating to a different page. if (_peerFinderStarted) { // detach the callback handler (there can only be one PeerConnectProgress handler). PeerFinder.TriggeredConnectionStateChanged -= new TypedEventHandler<object, TriggeredConnectionStateChangedEventArgs>(TriggeredConnectionStateChangedEventHandler); // detach the incoming connection request event handler PeerFinder.ConnectionRequested -= new TypedEventHandler<object, ConnectionRequestedEventArgs>(PeerConnectionRequested); PeerFinder.Stop(); _socketHelper.CloseSocket(); _peerFinderStarted = false; } if (_peerWatcher != null) { // unregister for events _peerWatcher.Added -= PeerWatcher_Added; _peerWatcher.Removed -= PeerWatcher_Removed; _peerWatcher.Updated -= PeerWatcher_Updated; _peerWatcher.EnumerationCompleted -= PeerWatcher_EnumerationCompleted; _peerWatcher.Stopped -= PeerWatcher_Stopped; _peerWatcher = null; } }
// PeerWatcher events private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo) { // Update the UI var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { PeerFinderOutputText.Text += "Peer added: " + GetTruncatedPeerId(peerInfo.Id) + ", name:" + peerInfo.DisplayName + "\n"; ShowPeerAddedControls(); lock (this) { _discoveredPeers.Add(peerInfo); } }); }
private void PeerWatcher_Added(PeerWatcher sender, PeerInformation peerInfo) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (availablePeers) { noPeersFound.Visibility = Visibility.Collapsed; availablePeers.Add(new AvailablePeer(peerInfo)); // Don't overlap NFC text, only show this if the ListView is enabled if (foundPeers.IsEnabled) { directions.Visibility = Visibility.Visible; } } }); }
/// <summary> /// Preserves state associated with this page in case the application is suspended or the /// page is discarded from the navigation cache. Values must conform to the serialization /// requirements of <see cref="SuspensionManager.SessionState"/>. /// </summary> /// <param name="sender">The source of the event; typically <see cref="NavigationHelper"/></param> /// <param name="e">Event data that provides an empty dictionary to be populated with /// serializable state.</param> private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e) { ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings; HardwareButtons.BackPressed -= HardwareButtons_BackPressed; myGeolocator.PositionChanged -= OnPositionChanged; 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; } string display_name = localSettings.Values[setting_phone_no] as string; // check if somehow the phone number is less than 4 chars long. This will be functionally invalid, in the settings page, there should be a check that makes you enter a phone number that's at least valid. if (display_name.Length >= 4) { display_name = display_name.Substring(display_name.Length - 4, 4); } else { // padd it until it is of length 4 for (int i = 0; i < (4 - display_name.Length); i++) { display_name += "0"; } } display_name = "[" + display_name + "]" + (localSettings.Values[setting_username] as string); PeerFinder.DisplayName = display_name; PeerFinder.Stop(); peerFinderStarted = false; }
private void PeerWatcher_Removed(PeerWatcher sender, PeerInformation peerInfo) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (availablePeers) { for (int i = 0; i < availablePeers.Count; i++) { if (availablePeers[i].Peer.Id == peerInfo.Id) { availablePeers.RemoveAt(i); } } if (availablePeers.Count == 0) { directions.Visibility = Visibility.Collapsed; } } }); }
private void PeerWatcher_Stopped(PeerWatcher sender, object o) { peerWatcherIsRunning = false; var result = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { // Update UI now that the PeerWatcher is stopped. }); }
private void PeerWatcher_Updated(PeerWatcher sender, PeerInformation peerInfo) { var ignored = messageDispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lock (availablePeers) { for (int i = 0; i < availablePeers.Count; i++) { if (availablePeers[i].Peer.Id == peerInfo.Id) { availablePeers[i] = new AvailablePeer(peerInfo); } } } }); }
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; // see if this removed peer was part of this session via the PIN string[] splitName = peerInfo.DisplayName.Split(displayNameSeperator, StringSplitOptions.None); if (getPIN(splitName[0]) == sessionPin) { // session pins are the same therefore this peer's location information must be refreshed. peerLocations[getPeerName(splitName[0])] = getCoordinates(splitName[1]); // other than refreshing the coordinates, we also have to change the color of the peerList for this peer to red to reflect this change. // <==================================================================== DO STUFF HEREERERE myMapIcons[getPeerName(splitName[0])].Location = new Geopoint(getGeoposition(splitName[1])); } } } } }); }
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. } else { // there are discovered peers so filter them based } } }); }