5

I have created a simple C# .NET framework console application.

In it, I am trying to get a list of paired bluetooth devices currently connected to the computer. However, I cannot figure out, how to access any bluetooth services from within the code.

I looked around the internet, and all I could find was a way to do this in an universal windows project (UWP), where I can simply use the using Windows.Devices.Bluetooth namespace which contains everything I would need, however in the .NET framework console application, this namespace is not available.

I don't need any advanced way to work with the bluetooth, all I need is a list of currently connected and paired bluetooth devices.

Askerman
  • 787
  • 1
  • 12
  • 31
  • 2
    I am curious: you say it would be simple using Windows.Devices.Bluetooth. Could you give me a hint which methods would make that possilbe? https://stackoverflow.com/questions/51526156/getting-a-list-of-already-connected-bluetooth-devices-on-windows-10 – Xan-Kun Clark-Davis Jul 25 '18 at 19:42

1 Answers1

9

Try using below code :

            BluetoothClient client = new BluetoothClient();
            List<string> items = new List<string>();
            BluetoothDeviceInfo[] devices = client.DiscoverDevicesInRange();
            foreach (BluetoothDeviceInfo d in devices)
            {
                items.Add(d.DeviceName);
            }

You will get the reference of BluetoothClient by installing 32feet.NET through Package Manager Console.

PM> Install-Package 32feet.NET -Version 3.5.0

After Installation you will get InTheHand.Net.Personal dll in the References and then add the namespace using InTheHand.Net.Sockets; in your code

Now you will be able to access BluetoothClient

Hope this helps!!

jANVI
  • 718
  • 3
  • 12
  • This is great, thank you. Could you please extend the answer to include a way to detect if those devices are within range or not? For example I want to look at the list of paired BT devices and see if any of them is actually physically present and turned on. (DiscoverDevicesInRange only lists devices which have the discovery mode on, but not paired devices which have discovery mode off, but are accessible anyway since they are paired) – Askerman Aug 16 '17 at 12:05
  • 1
    Have you tried client.DiscoverDevices() instead of client.DiscoverDevicesInRange(); also there are various overloaded version of DiscoverDevices which may solve your problem – jANVI Aug 16 '17 at 12:26
  • 1
    I did indeed tried all those, but the problem is that it always returns the paired devices, even if they are turned off (I also tried the attribute Connected in the device info which all items in the device list have, but it always returns false, even if the device is within range and has bluetooth on) – Askerman Aug 16 '17 at 12:38