我正在尝试将BLE功能添加到经典(WinForms?)C#桌面应用程序中,并添加了引用(Windows.winmd和System.Runtime.WindowsRuntime),以允许我访问Microsoft最近为以下目的引入的新BLE API。 Windows 10 UWP应用程序。我需要创建一个经典的桌面应用程序,因为我需要使用一个较旧的驱动程序设备包装器(teVirtualMIDI),并且想要创建一个.exe,而不是一个应用程序包。
我正在从以下位置引用上述库...
C:\ Program Files(x86)\ Windows Kits \ 10 \ UnionMetadata \ Facade \ Windows.WinMD
C:\ Program Files(x86)\参考程序集\ Microsoft \ Framework.NETCore \ v4.5 \ System.Runtime.WindowsRuntime.dll
C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETCore \ v4.5 \ System.Runtime.WindowsRuntime.UI.Xaml.dll
在这一点上,我只希望能够像在本博文中所做的那样,在调试输出窗口中查看连接的服务和特征。
https://blogs.msdn.microsoft.com/cdndevs/2017/04/28/uwp-working-with-bluetooth-devices-part-1/
由于BLE API需要执行异步操作,似乎出现了错误,但老实说我很茫然。我到目前为止编写的代码包含在下面。本质上,我在尝试调用“ GetGattServicesAsync()”方法时收到错误,因为Visual Studio说“ BluetoothLEDevice”类不包含此类定义。但是,该方法包含在联机文档中,我想知道为什么我无法访问它。
希望我能提供足够的信息,对于解决此问题的任何帮助将不胜感激。谢谢大家提供的所有有益建议!
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Windows.Devices.Bluetooth;
using Windows.Devices.Midi;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace BDBMidiClient
{
public class BLEHandlingDiscovery : Page
{
//private ObservableCollection<BluetoothLEAttributeDisplay> ServiceCollection = new ObservableCollection<BluetoothLEAttributeDisplay>();
//private ObservableCollection<BluetoothLEAttributeDisplay> CharacteristicCollection = new ObservableCollection<BluetoothLEAttributeDisplay>();
public ObservableCollection<BluetoothLEDeviceDisplay> KnownDevices = new ObservableCollection<BluetoothLEDeviceDisplay>();
//private List<DeviceInformation> UnknownDevices = new List<DeviceInformation>();
//private DeviceWatcher deviceWatcher;
//private BluetoothLEDevice bluetoothLeDevice = null;
//private GattCharacteristic selectedCharacteristic;
private void StartBLEDeviceWatcher()
{
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
DeviceWatcher deviceWatcher =
DeviceInformation.CreateWatcher(
BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
/*
DeviceWatcher deviceWatcher =
DeviceInformation.CreateWatcher(
"System.ItemNameDisplay:~~\"BDB\"",
requestedProperties,
DeviceInformationKind.AssociationEndpoint);*/
deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Removed += DeviceWatcher_Removed;
deviceWatcher.Start();
//Debug.WriteLine(requestedProperties);
}
private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation deviceInfo)
{
Guid gattService = new Guid();
var device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id);
var services=await device.GetGattServicesAsync();
foreach (var service in services.Services)
{
Debug.WriteLine($"Service: {service.Uuid}");
var characteristics = await service.GetCharacteristicsAsync();
foreach (var character in characteristics.Characteristics)
{
Debug.WriteLine($"Characteristic: {character.Uuid}");
}
}
}
private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
{
}
private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
{
}
async void ConnectToBLEDevice(DeviceInformation deviceInformation)
{
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync("BDB");
}
private BluetoothLEDeviceDisplay FindBluetoothLEDeviceDisplay(string id)
{
foreach (BluetoothLEDeviceDisplay bleDeviceDisplay in KnownDevices)
{
if (bleDeviceDisplay.Id == id)
{
return bleDeviceDisplay;
}
}
return null;
}
}
答案 0 :(得分:0)
The doc说该API属于“ Windows 10 Creators Update(引入的v10.0.15063.0)”。因此,请尝试从“ C:\ Program Files(x86)\ Windows Kits \ 10 \ UnionMetadata \ 10.0.15063.0 \ Windows.winmd”中添加一个