Everything I found online regarding bluetooth LE requires a universal windows app, which is completely unsuitable for me.
Is there a way to use bluetooth LE from c# without having to write my whole app as on UWP?
Everything I found online regarding bluetooth LE requires a universal windows app, which is completely unsuitable for me.
Is there a way to use bluetooth LE from c# without having to write my whole app as on UWP?
You can use C# APIs in C# Desktop applications! I have a sample here in GitHub.
In general, to get access to the C# APIS, add two references to your project:
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
Note that the #2 version depends on the version of .NET that you're using.
Background tasks will not be supported, but all other Bluetooth C# features should be there.
I have worked with and around the Microsoft partial Bluetooth API solutions since the XP days. I found a library from BTFramework that I have used extensively, on many platforms and in several languages. The Bluetooth Classic and Low Energy API packages from BTFramework have been easy to use, very reliable, and they've been super-responsive to any defects I did find. Our commercial product, as a result, has zero failures arising from the Bluetooth side. The team at BTFramework has taken on the job of working around Microsoft's partial implementations of the standard. We mainly code in C# dot NET, by the way. And we use many threads in the application.
Look at the code below this shows how to scan and connect to a ble device using winform application
using System;
using System.Windows.Forms;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
namespace TestBle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void button1_ClickAsync(object sender, EventArgs e)
{
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
DeviceWatcher deviceWatcher =
DeviceInformation.CreateWatcher(
BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
// Register event handlers before starting the watcher.
// Added, Updated and Removed are required to get all nearby devices
deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Removed += DeviceWatcher_Removed;
deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
deviceWatcher.Stopped += DeviceWatcher_Stopped;
// Start the watcher.
deviceWatcher.Start();
}
private void DeviceWatcher_Stopped(DeviceWatcher sender, object args)
{
//throw new NotImplementedException();
}
private void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
{
string a = "";
}
private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
{
string a = "";
}
private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
string a = "";
}
private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
{
string[] split = args.Id.Split('-');
if(split[1] == "84:2e:14:aa:65:13")
{
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(args.Id);
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
if (result.Status == GattCommunicationStatus.Success)
{
var services = result.Services;
// ...
}
}`enter code here`
}
}
}