static void Main(string[] args) { Console.WriteLine("Hello World!"); var watcher = new DnaBluetoothLEAdvertisementWatcher(new GattServiceIds()); watcher.StartedListening += () => { Console.WriteLine("Started listening"); }; watcher.StoppedListening += () => { Console.WriteLine("Stopped listening"); }; watcher.NewDeviceDiscovered += (device) => { Console.WriteLine($"New device: {device}"); }; watcher.DeviceNameChanged += (device) => { Console.WriteLine($"Device name Changed : {device}"); }; watcher.StartListening(); Console.ReadLine(); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { var watcher = new DnaBluetoothLEAdvertisementWatcher(); watcher.StartedListening += () => { _logger.LogInformation("Started Listening"); }; watcher.StoppedListening += () => { _logger.LogInformation("Stopped Listening"); }; watcher.NewDeviceDiscovered += (device) => { _logger.LogInformation($"New device: {device}"); }; watcher.DeviceNameChanged += (device) => { _logger.LogInformation($"Device name changed: {device}"); }; watcher.StartListening(); _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); await Task.Delay(1000, stoppingToken); } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); // New watcher var watcher = new DnaBluetoothLEAdvertisementWatcher(); // Hook into events watcher.StartedListening += () => { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("Started listening"); }; watcher.StoppedListening += () => { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Stopped listening"); }; watcher.NewDeviceDiscovered += (device) => { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"New device: {device}"); }; watcher.DeviceNameChanged += (device) => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Device name changed: {device}"); }; watcher.DeviceTimeout += (device) => { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Device timeout: {device}"); }; // Start listening watcher.StartListening(); while (true) { // Pause until we press enter Console.ReadLine(); // Get discovered devices var devices = watcher.DiscoveredDevices; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine($"{devices.Count} devices......"); foreach (var device in devices) { Console.WriteLine(device); } } }
static void Main(string[] args) { var watcher = new DnaBluetoothLEAdvertisementWatcher(); watcher.HeartbeatTimeout = 9999999; watcher.StartedListening += () => { Console.WriteLine("Started listening"); }; watcher.StoppedListening += () => { Console.WriteLine("Stopped listening"); }; watcher.NewDeviceDiscovered += (device) => { Console.WriteLine($"New device discovered: {device}"); }; watcher.DeviceNameChanged += (device) => { Console.WriteLine($"Name changed: {device}"); }; watcher.StartListening(); while (true) { Console.ReadLine(); var devices = watcher.DiscoveredDevices; Console.WriteLine($"{devices.Count} devices"); } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var tcs = new TaskCompletionSource <bool>(); Task.Run(async() => { try { // new watcher var watcher = new DnaBluetoothLEAdvertisementWatcher(new GattServiceIds()); // hook into start event watcher.StartedListening += () => { Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("Started Listening"); }; // hook into stop event watcher.StoppedListening += () => { Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Stopped Listening"); }; watcher.NewDeviceDiscovered += (device) => { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"New device: {device}"); }; watcher.DeviceNameChanged += (device) => { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Device name changed: {device}"); }; watcher.DeviceTimeout += (device) => { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Device timed out: {device}"); }; // start listening watcher.StartListening(); while (true) { // pause until we press enter var command = Console.ReadLine(); if (string.IsNullOrEmpty(command)) { // get discovered devices var devices = watcher.DiscoveredDevices; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine($"{devices.Count} devices...."); // show devices in console foreach (var device in devices) { Console.WriteLine(device); } } // C to connect else if (command == "c") { // attempt to find contour device var contourDevice = watcher.DiscoveredDevices.FirstOrDefault( f => f.Name.ToLower().StartsWith("lg")); // if we don't find it... if (contourDevice == null) { // let the user know Console.WriteLine("No contour device found for connecting"); continue; } // try and connect Console.WriteLine("connecting to Contour Device..."); try { // try and connect await watcher.PairToDeviceAsync(contourDevice.DeviceId); } catch (Exception ex) { Console.WriteLine("Failed to pair to contour device."); Console.WriteLine(ex); } } // Q to quit else if (command == "q") { break; } } // finish console application tcs.TrySetResult(true); } finally { // if anything goes wrong, exit out tcs.TrySetResult(false); } }); tcs.Task.Wait(); }