Serial over Bluetooth on Windows: how to get connection status

Mario Raffin 0 Reputation points
2024-01-12T16:54:11.1566667+00:00

Using a serial over bluetooth device (ESP32 in my case), the Windows driver shows the connection status in the bluetooth enumeration as show below (Connected/Not Connected). 7LYIB

How is it possible to have the same information programmatically, using .NET, without opening the corresponding COM port? Thank you.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,774 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,093 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,281 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 5,570 Reputation points Microsoft Employee
    2024-01-12T17:02:14.97+00:00

    Hello Mario. In .NET, you can use the DeviceInformation class to get the connection status of a paired Bluetooth device without opening the corresponding COM port. Here is a sample code snippet:

    DeviceInformationCollection PairedBluetoothDevices = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelectorFromPairingState(true));
    

    This code will return a collection of DeviceInformation objects representing all paired Bluetooth devices. You can then filter this collection by the device name to connect to the device you want. Please note that this method does not require opening the COM port associated with the Bluetooth device. It simply queries the system for the pairing state of the device. Remember to add the necessary using directives at the top of your file:

    using Windows.Devices.Bluetooth; 
    using Windows.Devices.Enumeration; 
    

    Also, ensure that your project has the necessary capabilities enabled in the Package.appxmanifest file (like bluetooth, etc.). Please note that this code is for UWP apps and may not work in a traditional .NET Framework or .NET Core console application. If you’re not developing a UWP app, you might need to use a library like 32feet.NET which provides a managed interface to the Win32 Bluetooth APIs. I hope this helps! Let me know if you have any other questions.


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.