Search code examples
c#wpfbluetoothwindows-7power-management

How do we change Wireless Bluetooth Radio Power Management Programmatically C#


I am having problem with app that uses an external bluetooth device. When device is idle for some time Windows Turn the power to the bluetooth radio. When i uncheck Under Power Management Tab Of the Bluetooth Radio [Allow the computer to turn off this device to save power.] It works. See Image. Same needed to be achieved from C# code. This need to be done from Win7 onwards. Power Management Option I am not familiar with power management option with windows c#. What are my options here? Is there a event or WMI class i can consume ?

I should be able to change these settings when my WPF app running. And restore it while closing.


Solution

  • You can do this pretty easily with WMI in C#. Make sure you add reference to System.Management (and a using System.Management; statement)

        //BTHUSB will identify physical bluetooth adapters only, if you want all bluetooth devices use 'WHERE PNPClass='Bluetooth' or specific device 'WHERE Name='Intel(R) Wireless Bluetooth(R)'
        ManagementObjectCollection PhysicalBluetoothAdapterResults = new ManagementObjectSearcher("root\\CIMV2", "SELECT DeviceID FROM Win32_PnPEntity WHERE Service='BTHUSB'").Get();
        foreach(ManagementObject PhysicalBluetoothAdapter in PhysicalBluetoothAdapterResults)
        {
            string DeviceID = PhysicalBluetoothAdapter.Properties["DeviceID"].Value.ToString().Replace("\\","\\\\");
            ManagementObjectCollection AdapterPowerOptionResults = new ManagementObjectSearcher("root\\WMI", $"SELECT * FROM MSPower_DeviceEnable WHERE InstanceName LIKE '{DeviceID}_%'").Get();
            foreach(ManagementObject AdapterPowerOption in AdapterPowerOptionResults)
            {
                AdapterPowerOption.Properties["enable"].Value = false;
                AdapterPowerOption.Put();
            }
        }
    

    Hope this helps.

    -Paul