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

Vishal2 Bansal 225 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.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,086 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,822 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,268 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 33,446 Reputation points Microsoft Vendor
    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 86,701 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.