From 0b2db009f02572e04f6ac658066388aefb4390f7 Mon Sep 17 00:00:00 2001 From: Ondra Medek Date: Mon, 12 Feb 2018 20:20:07 +0100 Subject: [PATCH] Add watcher.Stopped event --- Program.cs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Program.cs b/Program.cs index e720320..16a83e4 100644 --- a/Program.cs +++ b/Program.cs @@ -1,23 +1,18 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Windows.System; - using Windows.Devices.Bluetooth.Advertisement; namespace ConsoleApplication1 { - class Program + internal class Program { - static void Main(string[] args) + private static void Main(string[] args) { // Start the program var program = new Program(); // Close on key press - Console.ReadLine(); + Console.WriteLine("Scanning ... Press any key to cancel (finish)."); + Console.ReadKey(); } public Program() @@ -33,13 +28,16 @@ public Program() // Stop watching if the value drops below -90 (user walked away) watcher.SignalStrengthFilter.OutOfRangeThresholdInDBm = -90; - // Register callback for when we see an advertisements - watcher.Received += OnAdvertisementReceived; - // Wait 5 seconds to make sure the device is really out of range watcher.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(5000); watcher.SignalStrengthFilter.SamplingInterval = TimeSpan.FromMilliseconds(2000); + // Register callback for when we see an advertisements + watcher.Received += OnAdvertisementReceived; + + // Register callback for scan stop or error, especially + watcher.Stopped += OnStopped; + // Starting watching for advertisements watcher.Start(); } @@ -52,5 +50,12 @@ private void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, Bl Console.WriteLine(String.Format(" FR_NAME: {0}", eventArgs.Advertisement.LocalName)); Console.WriteLine(); } + + private void OnStopped(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementWatcherStoppedEventArgs args) + { + // Tell the user when the advertisement finishes + Console.WriteLine(String.Format("Advertisement stopped with: {0}", args.Error)); + Console.WriteLine("Press any key to finish."); + } } -} +} \ No newline at end of file