How to Use Bluetooth Low Energy With WPF or Forms

Hasan Hasanov
2 min readApr 19, 2021

Recently I had an idea of a project which requires using the Bluetooth API. I wanted to create something simple and did not want to use UWP for 2 reasons:

  1. I wanted to have a tray icon. This is also possible with UWP but creating a full-trust application seemed way more complicated for my scenario.
  2. I wanted a simple self-contained application which can run without the need of enabling developer mode for the end users.

My framework of choice for the project was WPF. There was only 1 problem, the BLE classes were not available. After a lot of research and failed attempts there is actually a way to use them with WPF. Here is what I did:

1. Install Windows SDK

First thing first you have to install the Windows SDK. Ideally you want to install it in its default location since you cannot change it later. Believe me I’ve tried!

If you installed it in a different location, you cannot use it with UWP and the Visual Studio for creating for example Full-Trust applications. I’ve also tried that one too.

The SDK will give you the Windows.winmd file. If you want to learn more check out How to read a winmd (WinRT metadata file). Ian Boyd explained it in great detail. There is also the official documentation.

2. Locate Windows.winmd File and Add a Reference

If you followed the default location path the Windows.winmd file is located in C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.19041.0\Windows.winmd. You can go ahead and аdd this file as a reference to your project.

Unfortunately, for me this does not work with .NET5. I tried so many things that if I should write them, I have to make another blog post. For this example and for my project I am using .Net Core 3.1.

Now you can use the BLE classes with your WPF, Windows Forms, and/or Console applications. There is one catch though. You cannot use async await yet.

3. Fix Async methods

In order to use them you have to bring System.Runtime.WindowsRuntime.dll too. If you installed .NET Framework 4 you should have it in your machine. My file’s location is C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.WindowsRuntime.dll

After all of this, you should have a working application.

For the sake of simplicity, we will create a console application.

Create a .NET Core 3.1 Console application. Let’s call it BleConsoleDemo. We will write a simple program that catches all the BLE devices that emits advertising packets.

Do not forget to add System.Runtime.WindowsRuntime.dll and Windows.winmd references as described above.

You can find the example pfoject in my github.

Originally published at https://hasan-hasanov.com on April 19, 2021.

--

--

Hasan Hasanov

HI, I am full time .NET developer. part time teacher and wannabe tech speaker based on Bulgaria