Skip to content

Portable Tool for an Ideal Windows Setup

refactor bluetooth management via Windows.Devices.Radios API

Greedeksa3ceabe5f3a6
diff --git a/.Source/GTweak/Core/ViewModel/SystemViewModel.cs b/.Source/GTweak/Core/ViewModel/SystemViewModel.cs
index 933f9df6..eaecfe8f 100644
--- a/.Source/GTweak/Core/ViewModel/SystemViewModel.cs
+++ b/.Source/GTweak/Core/ViewModel/SystemViewModel.cs
@@ -3,13 +3,15 @@ using System.Windows;
using GTweak.Core.Base;
using GTweak.Core.Model;
using GTweak.Utilities.Configuration;
+using GTweak.Utilities.Managers;
using GTweak.Utilities.Tweaks;
namespace GTweak.Core.ViewModel
{
internal class SystemViewModel : ViewModelPageBase<SystemModel, SystemTweaks>
{
- public Visibility RealtekSupportAvailable => HardwareData.VendorDetection.Realtek ? Visibility.Visible : Visibility.Collapsed;
+ public Visibility RealtekSupport => HardwareData.VendorDetection.Realtek ? Visibility.Visible : Visibility.Collapsed;
+ public Visibility BluetoothSupport => BluetoothManager.IsAvailable ? Visibility.Visible : Visibility.Collapsed;
protected override IReadOnlyDictionary<string, object> GetControlStates() => SystemTweaks.ControlStates;
diff --git a/.Source/GTweak/GTweak.csproj b/.Source/GTweak/GTweak.csproj
index c4ca3dd4..1f425df0 100644
--- a/.Source/GTweak/GTweak.csproj
+++ b/.Source/GTweak/GTweak.csproj
@@ -120,6 +120,17 @@
<HintPath>..\..\packages\WPF-UI.Abstractions.4.3.0\lib\net472\Wpf.Ui.Abstractions.dll</HintPath>
</Reference>
</ItemGroup>
+ <ItemGroup>
+ <AvailableWinMDs Include="$(MSBuildProgramFiles32)\Windows Kits\10\UnionMetadata\*\Windows.winmd" />
+ </ItemGroup>
+ <PropertyGroup>
+ <LatestWinMDPath>%(AvailableWinMDs.Identity)</LatestWinMDPath>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Windows">
+ <HintPath>$(LatestWinMDPath)</HintPath>
+ </Reference>
+ </ItemGroup>
<ItemGroup>
<None Include=".editorconfig" />
<None Include="Resources\AppsCatalog.xml" />
@@ -205,6 +216,7 @@
<Compile Include="Utilities\Helpers\UnlockHandleHelper.cs" />
<Compile Include="Utilities\Maintenance\ClearingMemory.cs" />
<Compile Include="Utilities\Maintenance\RegistryExporter.cs" />
+ <Compile Include="Utilities\Managers\BluetoothManager.cs" />
<Compile Include="Utilities\Tweaks\ConfidentialityTweaks.cs" />
<Compile Include="Utilities\Tweaks\DefenderManager\AclPermissionsBackup.cs" />
<Compile Include="Utilities\Tweaks\DefenderManager\WindowsDefender.cs" />
diff --git a/.Source/GTweak/Utilities/Managers/BluetoothManager.cs b/.Source/GTweak/Utilities/Managers/BluetoothManager.cs
new file mode 100644
index 00000000..de95e7f5
--- /dev/null
+++ b/.Source/GTweak/Utilities/Managers/BluetoothManager.cs
@@ -0,0 +1,80 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Threading.Tasks;
+using GTweak.Utilities.Controls;
+using Windows.Devices.Radios;
+using Windows.Foundation;
+
+namespace GTweak.Utilities.Managers
+{
+ internal class BluetoothManager
+ {
+ private static Radio _bluetooth;
+
+ internal static bool IsAvailable { get; private set; }
+ internal static bool IsEnabled => _bluetooth?.State == RadioState.On;
+
+ static BluetoothManager()
+ {
+ AppDomain.CurrentDomain.ProcessExit += delegate
+ {
+ if (_bluetooth != null)
+ {
+ Marshal.ReleaseComObject(_bluetooth);
+ _bluetooth = null;
+ }
+ };
+ }
+
+ internal static void Initialize()
+ {
+ try
+ {
+ if (Await(Radio.RequestAccessAsync()) != RadioAccessStatus.Allowed)
+ {
+ IsAvailable = false;
+ return;
+ }
+
+ IReadOnlyList<Radio> radios = Await(Radio.GetRadiosAsync());
+ _bluetooth = radios.FirstOrDefault(r => r.Kind == RadioKind.Bluetooth);
+ IsAvailable = _bluetooth != null;
+ }
+ catch { IsAvailable = false; }
+ }
+
+ internal static void SetState(bool state)
+ {
+ Task.Run(delegate
+ {
+ try
+ {
+ if (_bluetooth == null)
+ {
+ return;
+ }
+
+ RadioState targetState = state ? RadioState.On : RadioState.Off;
+ if (_bluetooth.State == targetState)
+ {
+ return;
+ }
+
+ Await(_bluetooth.SetStateAsync(targetState));
+ }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
+ });
+ }
+
+ private static T Await<T>(IAsyncOperation<T> operation)
+ {
+ using ManualResetEventSlim resetEvent = new ManualResetEventSlim(false);
+ operation.Completed = delegate { resetEvent.Set(); };
+ resetEvent.Wait();
+ return operation.GetResults();
+ }
+ }
+}
diff --git a/.Source/GTweak/Utilities/Tweaks/SystemTweaks.cs b/.Source/GTweak/Utilities/Tweaks/SystemTweaks.cs
index 610e9059..b32e1706 100644
--- a/.Source/GTweak/Utilities/Tweaks/SystemTweaks.cs
+++ b/.Source/GTweak/Utilities/Tweaks/SystemTweaks.cs
@@ -29,7 +29,7 @@ namespace GTweak.Utilities.Tweaks
internal sealed class SystemTweaks : FirewallManager
{
- private static bool _isNetshState = false, _isBluetoothStatus = false, _isTickState = false;
+ private static bool _isNetshState = false, _isTickState = false;
private static string _currentPowerGuid = string.Empty;
internal readonly static Dictionary<string, object> ControlStates = new Dictionary<string, object>();
@@ -147,7 +147,7 @@ namespace GTweak.Utilities.Tweaks
!RegistryHelp.GetValue($@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\{_currentPowerGuid}", "Description", string.Empty).Contains("-18") &&
!RegistryHelp.GetValue($@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes\{_currentPowerGuid}", "FriendlyName", string.Empty).Contains("-19");
- _сontrolWriter.Button[19] = _isBluetoothStatus;
+ _сontrolWriter.Button[19] = BluetoothManager.IsEnabled;
_сontrolWriter.Button[20] =
RegistryHelp.CheckValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\mpssvc", "Start", "4");
@@ -193,18 +193,6 @@ namespace GTweak.Utilities.Tweaks
(HardwareData.OS.Build >= 26200m && RegistryHelp.CheckValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Dwm", "EnableOverlay", "0"));
}
-
- internal static void ViewBluetoothStatus()
- {
- try
- {
- using ManagementObjectCollection managementObjCollection = new ManagementObjectSearcher("SELECT DeviceID FROM Win32_PnPEntity WHERE Service='BthLEEnum'").Get();
- _isBluetoothStatus = managementObjCollection.Cast<ManagementObject>().Any();
-
- }
- catch { _isBluetoothStatus = false; }
- }
-
internal static void ViewNetshState()
{
try
@@ -250,7 +238,6 @@ namespace GTweak.Utilities.Tweaks
}
}
-
[DllImport("user32.dll")]
private static extern bool SystemParametersInfo(uint _uiAction, uint _uiParam, uint[] _pvParam, uint _fWinIni);
internal async void ApplyTweaks(string tweak, bool isDisabled, bool canShowWindow = true)
@@ -437,22 +424,7 @@ namespace GTweak.Utilities.Tweaks
SetPowercfg(isDisabled);
break;
case "TglButton19":
- CommandExecutor.RunCommand(@"Add-Type -AssemblyName System.Runtime.WindowsRuntime
- $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
- Function Await($WinRtTask, $ResultType) {
- $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
- $netTask = $asTask.Invoke($null, @($WinRtTask))
- $netTask.Wait(-1) | Out-Null
- $netTask.Result
- }
- [Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
- [Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
- Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
- $radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
- $bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
- [Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
- Await ($bluetooth.SetStateAsync(" + (isDisabled ? "'off'" : "'on'") + ")) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null", true);
- _isBluetoothStatus = !isDisabled;
+ BluetoothManager.SetState(!isDisabled);
break;
case "TglButton20":
RegistryHelp.Write(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Services\mpssvc", "Start", isDisabled ? 4 : 2, RegistryValueKind.DWord);
diff --git a/.Source/GTweak/View/SystemView.xaml b/.Source/GTweak/View/SystemView.xaml
index f7e58d31..eaa9c046 100644
--- a/.Source/GTweak/View/SystemView.xaml
+++ b/.Source/GTweak/View/SystemView.xaml
@@ -56,7 +56,7 @@
<uc:ToggleButton x:Name="TglButton4" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton4].State}" TextResource="{DynamicResource tgl4_sys}" Description="{DynamicResource tgl4_desc_sys}" />
<uc:ToggleButton x:Name="TglButton5" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton5].State}" TextResource="{DynamicResource tgl5_sys}" Description="{DynamicResource tgl5_desc_sys}" />
<uc:ToggleButton x:Name="TglButton6" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton6].State}" TextResource="{DynamicResource tgl6_sys}" Description="{DynamicResource tgl6_desc_sys}" />
- <uc:ToggleButton x:Name="TglButton7" Visibility="{Binding RealtekSupportAvailable}" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton7].State}" TextResource="{DynamicResource tgl7_sys}" Description="{DynamicResource tgl7_desc_sys}" />
+ <uc:ToggleButton x:Name="TglButton7" Visibility="{Binding RealtekSupport}" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton7].State}" TextResource="{DynamicResource tgl7_sys}" Description="{DynamicResource tgl7_desc_sys}" />
<uc:ToggleButton x:Name="TglButton8" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton8].State}" TextResource="{DynamicResource tgl8_sys}" Description="{DynamicResource tgl8_desc_sys}" />
<uc:ToggleButton x:Name="TglButton9" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton9].State}" TextResource="{DynamicResource tgl9_sys}" Description="{DynamicResource tgl9_desc_sys}" />
<uc:ToggleButton x:Name="TglButton10" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton10].State}" TextResource="{DynamicResource tgl10_sys}" Description="{DynamicResource tgl10_desc_sys}" />
@@ -68,7 +68,7 @@
<uc:ToggleButton x:Name="TglButton16" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton16].State}" TextResource="{DynamicResource tgl16_sys}" Description="{DynamicResource tgl16_desc_sys}" />
<uc:ToggleButton x:Name="TglButton17" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton17].State}" TextResource="{DynamicResource tgl17_sys}" Description="{DynamicResource tgl17_desc_sys}" />
<uc:ToggleButton x:Name="TglButton18" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton18].State}" TextResource="{DynamicResource tgl18_sys}" Description="{DynamicResource tgl18_desc_sys}" />
- <uc:ToggleButton x:Name="TglButton19" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton19].State}" TextResource="{DynamicResource tgl19_sys}" Description="{DynamicResource tgl19_desc_sys}" />
+ <uc:ToggleButton x:Name="TglButton19" Visibility="{Binding BluetoothSupport}" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton19].State}" TextResource="{DynamicResource tgl19_sys}" Description="{DynamicResource tgl19_desc_sys}" />
<uc:ToggleButton x:Name="TglButton20" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton20].State}" TextResource="{DynamicResource tgl20_sys}" Description="{DynamicResource tgl20_desc_sys}" />
<uc:ToggleButton x:Name="TglButton21" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton21].State}" TextResource="{DynamicResource tgl21_sys}" Description="{DynamicResource tgl21_desc_sys}" />
<uc:ToggleButton x:Name="TglButton22" Style="{StaticResource ToggleStyle}" State="{Binding [TglButton22].State}" TextResource="{DynamicResource tgl22_sys}" Description="{DynamicResource tgl22_desc_sys}" />
diff --git a/.Source/GTweak/Windows/LoadingWindow.xaml.cs b/.Source/GTweak/Windows/LoadingWindow.xaml.cs
index cb9aae16..899157eb 100644
--- a/.Source/GTweak/Windows/LoadingWindow.xaml.cs
+++ b/.Source/GTweak/Windows/LoadingWindow.xaml.cs
@@ -6,6 +6,7 @@ using GTweak.Utilities.Configuration;
using GTweak.Utilities.Controls;
using GTweak.Utilities.Helpers;
using GTweak.Utilities.Maintenance;
+using GTweak.Utilities.Managers;
using GTweak.Utilities.Tweaks;
using Wpf.Ui.Controls;
@@ -63,7 +64,7 @@ namespace GTweak.Windows
ReportStep(2, 35);
ExecuteWithLogging(SystemTweaks.ViewConfigTick, nameof(SystemTweaks.ViewConfigTick));
- ExecuteWithLogging(SystemTweaks.ViewBluetoothStatus, nameof(SystemTweaks.ViewBluetoothStatus));
+ ExecuteWithLogging(BluetoothManager.Initialize, nameof(BluetoothManager.Initialize));
ExecuteWithLogging(SystemTweaks.ViewNetshState, nameof(SystemTweaks.ViewNetshState));
ReportStep(3, 55);