Skip to content

Commit

Permalink
Much faster response when detecting and connecting the mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatUsernameAlreadyExist committed Dec 20, 2024
1 parent 722fe52 commit d42b3c7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 31 deletions.
88 changes: 59 additions & 29 deletions src/BLEMouseWatcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;


namespace BLEMouseWatcher
{
class Program
Expand All @@ -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) =>
Expand All @@ -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<DeviceInformationCollection> task = DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected)).AsTask();

static BluetoothLEDevice findMonitoredBluetoothLEDevice()
{
Task<DeviceInformationCollection> task = DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()).AsTask();
foreach (DeviceInformation device in task.Result)
{
if (monitoredDevices.Contains(device.Name))
{
return true;
{
Task<BluetoothLEDevice> bleDevice = BluetoothLEDevice.FromIdAsync(device.Id).AsTask();
return bleDevice.Result;
}
}
return false;
return null;
}
}
}
4 changes: 2 additions & 2 deletions src/BLEMouseWatcher/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

0 comments on commit d42b3c7

Please sign in to comment.