NET中BLE的GattDeviceServicesResult的正确库或运行时 [英] Correct library or runtime for GattDeviceServicesResult for BLE in .NET

查看:403
本文介绍了NET中BLE的GattDeviceServicesResult的正确库或运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 我现在有理由相信您不能在Enterprise LTSB上使用Windows Creator,因为该版本不够高并且无法更新!如果有人可以确认这一点,并说明如果可以使用Creator,那么我应该如何开发蓝牙应用程序,将不胜感激.

UPDATE I now have reason to believe that you can't use Windows Creator on Enterprise LTSB because the version isn't high enough and can't be updated! If someone could confirm this AND state how I should go about developing a Bluetooth app given that I can use Creator it would be much appreciated.

找不到Windows运行时类型'Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceServicesResult'"

"Could not find Windows Runtime type 'Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceServicesResult'"

我在开发人员机器上开发了一些示例代码,以通过C#/.Net中的BLE(蓝牙低功耗)进行通信.当我将此发行版文件夹转移到另一台计算机上并运行该程序时,当我尝试启动BLE通信时会收到上述消息.我猜想我缺少一些关键的库/dll(或.NETCore或.Net Framework),但是呢?我已经搜索了,找不到答案.

I developed some sample code on my developer machine to communicate through BLE (Bluetooth Low Energy) in C#/.Net. When I transfer this release folder to another machine and run the program, I get the message above when I try to initiate BLE communications. I'm guessing that I'm missing some key library/dll (or .NETCore or .Net Framework), but what? I've searched and can't find an answer.

一个可能有用的线索(或可能不相关)是,在我的项目中,我在目录C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETCore \ v4中引用了System.Runtime.WindowsRuntime. 5.1 \ System.Runtime.WindowsRuntime.dll 另一台计算机没有v4.5.1,而只有v4.5.但是,当我尝试安装v4.5.1时,会被告知我已经有一个较新的版本!也许有人可以告诉我如何在计算机上获取.netcore v4.5.1?

A possibly useful clue (or possibly irrelevant) is that that in my project I have a reference to System.Runtime.WindowsRuntime in directory C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5.1\System.Runtime.WindowsRuntime.dll The other machine does not have a v4.5.1 but only a v4.5. However, when I try and install v4.5.1 I'm told that I already have a newer version! Perhaps someone could tell me how to get .netcore v4.5.1 on a machine?

谢谢, 戴夫

更多信息..在原始项目中,存在带有路径C:\ Program Files(x86)\ Windows Kits \ 10 \ UnionMetadata \ 10.0.17134.0 \ Windows.winmd的引用"Windows". 如果在此使用对象浏览器,则会看到Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceServicesResult 我正在尝试使我的代码在其上运行的系统上也有相同的文件(相同版本).不知道为什么它在运行时没有看到"它.

More information. In the original project, there is a reference "Windows" with path C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0\Windows.winmd If you use Object Browser on this you'll see Windows.Devices.Bluetooth.GenericAttributeProfile.GattDeviceServicesResult This same file (same version) is also on the system I'm trying to get my code to run on. Not sure why it's not "seeing" it at run time.

显然,其他人有这个问题.请参阅:来自WPF应用程序的蓝牙LE GattDeviceServicesResult TypeLoadException

Apparently, others have had this problem. See: Bluetooth LE GattDeviceServicesResult TypeLoadException from WPF application

更多基于研究的信息 我在生产计算机上安装了Visual Studio,以为可以消除开发人员计算机和生产计算机之间的差异.仍然没有运气.但是,我要指出的是,其余的区别之一是我的开发人员计算机具有Windows 10 Pro.生产机器(平板电脑)具有Windows 10 Enterprise 2016 LTSB

Yet more information based on research I installed Visual Studio on the production machine thinking that would remove one difference between my developer machine and the production machine. Still no luck. However, I will point out that one of the remaining differences is that my developer machine has Windows 10 Pro. The production machine (which is a Tablet) has Windows 10 Enterprise 2016 LTSB

推荐答案

似乎Windows 10 Enterprise不支持Windows.Devices.Bluetooth命名空间中的很多方法.我为此找到的解决方案要求Windows 10 Enterprise用户配对其BLE设备.这使我们可以使用过时的BluetoothLEDevice.GattServices属性.我们捕获了TypeLoadException并退回到Windows 10 Enterprise支持的方法.

It seems that Windows 10 Enterprise does not support quite a few of the methods in Windows.Devices.Bluetooth namespace. The solution that I found for this requires Windows 10 Enterprise users to pair their BLE devices. This allows us to use the obsolete BluetoothLEDevice.GattServices property. We catch the TypeLoadException and fall back to the methods supported in Windows 10 Enterprise.

    private async Task<IReadOnlyCollection<GattDeviceService>> GetGattService(BluetoothLEDevice device, Guid uuid)
    {
        try
        {
            // Try to get the services async first
            return await GetGattServicesAsync(device, uuid);
        }
        catch(TypeLoadException e)
        {
            // Not supported in version of windows. Fall back to old way
            return GetGattDeviceServices(device, uuid);
        }
    }

    private async Task<IReadOnlyList<GattDeviceService>> GetGattServicesAsync(BluetoothLEDevice device, Guid uuid)
    {
        var result = await Device.GetGattServicesForUuidAsync(uuid);
        if(result.Status == GattCommunicationStatus.Success)
        {
            return result.Services;
        }
        return null;
    }

    private IReadOnlyList<GattDeviceService> GetGattDeviceServices(BluetoothLEDevice device, Guid uuid)
    {
        var result = device.GattServices.Where(s => s.Uuid == uuid);
        if(result != null)
        {
            return result.ToList().AsReadOnly();
        }
        return null;
    }

在尝试从服务中获取特征时,我们需要做同样的事情.而不是调用GattDeviceService.GetCharacteristicsForUuidAsync,我们调用GattDeviceService.GetAllCharacteristics().我还发现一些实例,其中某些PC不支持任何BluetoothLEDevice.GetDeviceSelector()方法,这在创建观察程序时引起了问题.必须配对设备有点不便.不幸的是,我无法找到任何有关此文件的文档.除了几个视频此处,他们讨论了不再需要配对BLE设备这使我相信LTSB不支持它.

We need to do the same when attempting to get the characteristics from the services. Instead of calling GattDeviceService.GetCharacteristicsForUuidAsync we call GattDeviceService.GetAllCharacteristics(). I also found instances where some PCs did not support any of the BluetoothLEDevice.GetDeviceSelector() methods, which was causing problems creating the watcher. Having to pair the device is a bit of an inconvenience. Unfortunately I have not been able to find any documentation on this. Other than a couple videos here where they discuss not having to pair BLE devices anymore which led me to believe that it was not supported in LTSB.

这篇关于NET中BLE的GattDeviceServicesResult的正确库或运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆