-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·113 lines (100 loc) · 3.85 KB
/
Program.cs
File metadata and controls
executable file
·113 lines (100 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.Devices.Bluetooth;
using Windows.Devices.Enumeration;
namespace BluetoothBatteryMonitor
{
internal static class Program
{
private static Mutex? _mutex;
[STAThread]
static void Main(string[] args)
{
if (HasListDevicesFlag(args))
{
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ShowDeviceListAndExitAsync().GetAwaiter().GetResult();
return;
}
// Single instance check
const string mutexName = "Global\\BluetoothBatteryMonitor_SingleInstance";
_mutex = new Mutex(true, mutexName, out bool createdNew);
if (!createdNew)
{
// Another instance is already running
return;
}
try
{
// CRITICAL: SetHighDpiMode must be called FIRST, before any other Application calls
// This is the .NET 6+ way to configure DPI awareness
Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using var monitor = new BatteryMonitor();
Application.Run(monitor);
}
finally
{
_mutex?.ReleaseMutex();
_mutex?.Dispose();
}
}
private static bool HasListDevicesFlag(string[] args)
{
return args.Any(arg =>
{
var trimmed = arg.TrimStart('-', '/');
return string.Equals(trimmed, "listdevices", StringComparison.OrdinalIgnoreCase);
});
}
private static async Task ShowDeviceListAndExitAsync()
{
try
{
var leSelector = BluetoothLEDevice.GetDeviceSelector();
var classicSelector = BluetoothDevice.GetDeviceSelector();
var leDevices = await DeviceInformation.FindAllAsync(leSelector);
var classicDevices = await DeviceInformation.FindAllAsync(classicSelector);
var names = leDevices
.Concat(classicDevices)
.Select(device => device.Name?.Trim())
.Where(name => !string.IsNullOrWhiteSpace(name))
.Select(name => name!)
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(name => name, StringComparer.OrdinalIgnoreCase)
.ToList();
var message = BuildDeviceListMessage(names);
MessageBox.Show(message, "Bluetooth Battery Monitor - Devices", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(
$"Failed to enumerate Bluetooth devices.\n{ex.Message}",
"Bluetooth Battery Monitor - Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private static string BuildDeviceListMessage(System.Collections.Generic.IReadOnlyList<string> names)
{
if (names.Count == 0)
{
return "No Bluetooth devices found.";
}
var builder = new StringBuilder();
builder.AppendLine("Bluetooth devices:");
foreach (var name in names)
{
builder.AppendLine(name);
}
return builder.ToString().TrimEnd();
}
}
}