I want to enable/Disable (i.e. lock/unlock) the Bluetooth through the C# wpf Application

Vishal2 Bansal 265 Reputation points
2025-01-14T13:11:39.5233333+00:00

Hi

I want to enable/disable the Bluetooth programmatically in c# .NET WPF app.

I am not able to find correct implementation.

Pls help.

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2025-01-14T14:09:54.5133333+00:00

    Hi @Vishal2 Bansal ,

    .NET does not provide built-in APIs for directly controlling Bluetooth.

    You can try using the Windows.Devices.Bluetooth namespace.

    using System;
    using System.Runtime.InteropServices;
    using Windows.Devices.Bluetooth;
    using Windows.Devices.Enumeration;
    
    namespace BluetoothExample
    {
        public class BluetoothManager
        {
            [DllImport("bthprops.cpl", CharSet = CharSet.Unicode, SetLastError = true)]
            private static extern int BluetoothEnableDiscovery(IntPtr hwndCaller, bool fEnabled);
    
            [DllImport("bthprops.cpl", CharSet = CharSet.Unicode, SetLastError = true)]
            private static extern int BluetoothEnableIncomingConnections(IntPtr hwndCaller, bool fEnabled);
    
            public static void EnableBluetooth()
            {
                BluetoothEnableDiscovery(IntPtr.Zero, true);
                BluetoothEnableIncomingConnections(IntPtr.Zero, true);
            }
    
            public static void DisableBluetooth()
            {
                BluetoothEnableDiscovery(IntPtr.Zero, false);
                BluetoothEnableIncomingConnections(IntPtr.Zero, false);
            }
        }
    }
    
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2025-01-14T15:23:38.5666667+00:00

    This works for me (Windows 10 22H2, Microsoft.Windows.CsWinRT package) :

                {
                    var result222 = await Radio.RequestAccessAsync();
                    IReadOnlyList<Radio> radios = await Radio.GetRadiosAsync();
                    Radio? BluetoothRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth);
                    RadioAccessStatus result;
                    foreach (Radio module in radios)
                    {
                        if (module.Kind == RadioKind.Bluetooth)
                        {
                            if (module.State == RadioState.Off)
                                result = await module.SetStateAsync(RadioState.On);
    
                            else
                                result = await module.SetStateAsync(RadioState.Off);
                        }
                    }
                }
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.