diff --git a/src/BLEMouseWatcher/Program.cs b/src/BLEMouseWatcher/Program.cs index f3571b0..bb902ee 100644 --- a/src/BLEMouseWatcher/Program.cs +++ b/src/BLEMouseWatcher/Program.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; + namespace BLEMouseWatcher { class Program @@ -19,7 +20,10 @@ class Program static string[] requestedBLEProperties = { }; static void Main(string[] args) - { + { + ManualResetEvent watcherStopEvent = new ManualResetEvent(false); + ManualResetEvent disconnectEvent = new ManualResetEvent(false); + // Start endless BLE device watcher var watcher = DeviceInformation.CreateWatcher(allBleDevicesStr, requestedBLEProperties, DeviceInformationKind.AssociationEndpoint); watcher.Added += (DeviceWatcher sender, DeviceInformation devInfo) => @@ -33,45 +37,71 @@ static void Main(string[] args) sender.Stop(); }; watcher.Stopped += (DeviceWatcher sender, object arg) => - { + { + watcherStopEvent.Set(); }; - int sleepMs = 5000; - - // Main loop - while (true) - { - Thread.Sleep(sleepMs); - - if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Stopped || - watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Created) - { - if (IsMonitoredBluetoothLEDeviceConnected()) - { - sleepMs = 10000; - } - else - { - watcher.Start(); - sleepMs = 7000; - } - - Thread.Sleep(sleepMs); + while (true) + { + BluetoothLEDevice bluetoothLEDevice = findMonitoredBluetoothLEDevice(); + if (bluetoothLEDevice == null) + { + Thread.Sleep(15000); + } + else + { + if (bluetoothLEDevice.ConnectionStatus == BluetoothConnectionStatus.Disconnected) + { + disconnectEvent.Set(); + } + + bluetoothLEDevice.ConnectionStatusChanged += (BluetoothLEDevice sender, object arg) => + { + if (sender.ConnectionStatus == BluetoothConnectionStatus.Disconnected) + { + disconnectEvent.Set(); + } + else + { + disconnectEvent.Reset(); + if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Started) + { + watcher.Stop(); + } + } + }; + + while (true) + { + disconnectEvent.WaitOne(); + + if (watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Stopped || + watcher.Status == Windows.Devices.Enumeration.DeviceWatcherStatus.Created) + { + watcherStopEvent.Reset(); + watcher.Start(); + } + + watcherStopEvent.WaitOne(); + Thread.Sleep(500); + } } } } - static bool IsMonitoredBluetoothLEDeviceConnected() - { - Task task = DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected)).AsTask(); + + static BluetoothLEDevice findMonitoredBluetoothLEDevice() + { + Task task = DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()).AsTask(); foreach (DeviceInformation device in task.Result) { if (monitoredDevices.Contains(device.Name)) - { - return true; + { + Task bleDevice = BluetoothLEDevice.FromIdAsync(device.Id).AsTask(); + return bleDevice.Result; } } - return false; + return null; } } } diff --git a/src/BLEMouseWatcher/Properties/AssemblyInfo.cs b/src/BLEMouseWatcher/Properties/AssemblyInfo.cs index be01361..67d70e3 100644 --- a/src/BLEMouseWatcher/Properties/AssemblyInfo.cs +++ b/src/BLEMouseWatcher/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.0.0.0")] -[assembly: AssemblyFileVersion("2.0.0.0")] +[assembly: AssemblyVersion("3.0.0.0")] +[assembly: AssemblyFileVersion("3.0.0.0")]