是否可以在 WPF 应用程序中连接到 BTLE 设备?

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

所以我正在尝试连接到 Polar H7 心率监测器,我需要使用 WPF 应用程序来完成它。我正在使用 Windows 10.

现在我已经使用 UWP 应用程序完成了此操作并且它运行良好,但我想使用 WPF(如果可能的话)来执行相同的操作。

我发现 this post 关于如何在 WPF/winforms/etc 中使用 Windows 10 APIs 并认为完美,这就是我需要的。我已成功将蓝牙 API 添加到我的 WPF 项目并加入了适用于我的 UWP 项目的代码,但它不起作用。

这是我的代码片段,直到它停止工作的地方:

        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;

现在,这个电话

await GattDeviceService.FromIdAsync(_devicePolar.Id) 

正在返回 null。我在制作 UWP 应用程序时实际上遇到了同样的问题,我发现这是因为我忘记了这个

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

进入我的清单文件。一旦我将其放入(在我的 UWP 应用程序中),它就可以正常工作。但是在 WPF 应用程序中似乎没有这部分的等效位置。现在回到关于如何将 Win 10 库添加到 WPF 的博客 post,这一点似乎告诉我一直都是这样:

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.

并进一步研究 post 中链接的 Microsoft 主题,它确实指定了关于蓝牙的 "Not all APIs are currently supported for packaged apps." 这是非常不具体的。

还应该提到,在尝试之前,我尝试使用 32feet 库,但我的设备根本没有显示,而其他设备(不是 LE)显示,所以我假设 32feet 不支持 BTLE .我发现 this 对 32feet 提出了同样的要求,这就是促使我尝试上面详述的内容的原因,但我仍然不是 100% 清楚我是否可以简单地使用 32feet 连接到 BTLE 设备.

所以我的问题是,我想做的事情(使用 Windows 10 API 连接到蓝牙设备的 WPF 应用程序)目前无法完成,我说得对吗?如果不是,我做错了什么?

问题发布两年后更新:我认为仍然没有支持 WPF Bluetooth LE 的库。一个非常直接的证据是,UWP 下的 Windows 设置支持蓝牙 LE 设备,而 Windows 10 上的控制面板不支持。

我现在能想出的唯一解决办法是购买一个具有虚拟 COM 端口功能的蓝牙加密狗,这样理想情况下我可以像使用串行连接一样与 BLE 设备通信。还没有测试,如果有效,稍后会更新。