Search code examples
c#wpfwindows-10-universalbtle

Is it possible to connect to a BTLE device in a WPF application?


So I am trying to connect to a Polar H7 Heart Rate Monitor, and I need to use a WPF application to do it. I'm using Windows 10.

Now I have done this with a UWP application already and it works perfectly, but I'd like to use WPF (if it is possible) instead to do the same.

I found this post on how to use Windows 10 APIs in WPF/winforms/etc and thought perfect, that's what I need. I successfully added the Bluetooth API to my WPF project and threw in the code that was working for my UWP project, but it doesn't work.

Here's a snippet of my code up to where it stops working:

        DeviceInformation _devicePolar = null;
        string StatusInformation;
        string StatusInformation2;
        var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.HeartRate));
        foreach(var d in devices)
        {
            Debug.WriteLine(d.Name + " " + d.Id);
        }
        if (null == devices || devices.Count <= 0) return;
        foreach (var device in devices.Where(device => device.Name == "Polar H7 DCB16C16"))
        {
            _devicePolar = device;
            StatusInformation = string.Format("Found {0}", _devicePolar.Name);
            break;
        }
        if (_devicePolar == null) return;
        var service = await GattDeviceService.FromIdAsync(_devicePolar.Id);
        if (service == null) return;

Now, this call

await GattDeviceService.FromIdAsync(_devicePolar.Id) 

is returning null. I was actually having the same problem when I made the UWP application and I found out it was because I forgot to throw in this

  <Capabilities>
    <Capability Name="internetClient" />
    <DeviceCapability Name="bluetooth" />
  </Capabilities>

Into my manifest file. Once I put that in (on my UWP application) it worked fine. But there seems to be no equivalent place for this piece in a WPF application. Now going back to the blog post on how to add Win 10 libraries to WPF, this bit seems to be telling me that was the case all along:

The second set of APIs that you can’t use are ones that depend on an app’s package identity. UWP apps have package identities while PC software does not. Package identity information can be found in the app manifest file.

And looking further into the Microsoft topic linked in the post it does specify about Bluetooth that "Not all APIs are currently supported for packaged apps." Which is terribly non-specific.

Also should mention, before trying this I tried to use the 32feet library, but my device was not showing up at all while other devices (not LE) were, so I am assuming that 32feet just doesn't support BTLE. I found this asking the same about 32feet and that is what led me to try what I have detailed above instead, but I'm still not 100% clear on whether or not I can simply use 32feet to connect to BTLE devices.

So my question is, am I right that what am I trying to do (WPF application using Windows 10 API to connect to Bluetooth device) can't currently be done? If not, what am I doing wrong?


Solution

  • Update after two years the question posted: I think there's still no library supporting WPF Bluetooth LE. A pretty straight forward proof is that, Windows settings, which is under UWP, supports Bluetooth LE devices, while Control Panel on Windows 10 doesn't.

    The only solution I can come up with now is buying a Bluetooth dongle which has virtual COM port feature, so ideally I can talk to the BLE device like using serial connection. Haven't tested yet, will update later if this works.