Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public class BluetoothManager
public static async Task Main(string[] args)
{
// Check if the correct command-line arguments are provided.
if (args.Length == 0 || (args[0].ToLower() != "on" && args[0].ToLower() != "off"))
if (args.Length == 0 || (args[0].ToLower() != "on" && args[0].ToLower() != "off" && args[0].ToLower() != "toggle"))
{
Console.WriteLine("Usage: BluetoothSwitcher.exe [on|off]");
Console.WriteLine("Usage: BluetoothSwitcher.exe [on|off|toggle]");
return;
}

Expand All @@ -28,14 +28,13 @@ public static async Task Main(string[] args)

// Find the first radio that is a Bluetooth adapter.
var bluetoothRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth);

if (bluetoothRadio == null)
{
Console.WriteLine("No Bluetooth adapter was found.");
return;
}

// Process the "on" or "off" command.
// Process the "on", "off" or "toggle" command.
string command = args[0].ToLower();
if (command == "on")
{
Expand All @@ -47,11 +46,17 @@ public static async Task Main(string[] args)
await bluetoothRadio.SetStateAsync(RadioState.Off);
Console.WriteLine("Bluetooth was turned off successfully.");
}
else if (command == "toggle")
{
var newState = bluetoothRadio.State == RadioState.On ? RadioState.Off : RadioState.On;

Copilot AI Jan 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The toggle logic doesn't handle intermediate states. The bluetooth radio could be in states other than On or Off (such as Unknown or Disabled). If bluetoothRadio.State is not RadioState.On, the code will attempt to turn it On, which may not be the intended behavior for all states. Consider checking if the current state is valid before toggling.

Suggested change
var newState = bluetoothRadio.State == RadioState.On ? RadioState.Off : RadioState.On;
var currentState = bluetoothRadio.State;
if (currentState != RadioState.On && currentState != RadioState.Off)
{
Console.WriteLine($"Bluetooth cannot be toggled from the current state: {currentState}.");
return;
}
var newState = currentState == RadioState.On ? RadioState.Off : RadioState.On;

Copilot uses AI. Check for mistakes.
await bluetoothRadio.SetStateAsync(newState);
Console.WriteLine($"Bluetooth was toggled to {(newState == RadioState.On ? "on" : "off")} successfully.");
}

Copilot AI Jan 5, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is trailing whitespace at the end of this line. This should be removed for consistency and to follow best practices.

Suggested change
}
}

Copilot uses AI. Check for mistakes.
}
catch (Exception ex)
{
// Handle any unexpected errors.
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}
}