打开VS->工具->NuGet包管理器->程序包管理器设置->常规->默认包管理格式(设置为PackageRefrence)
解决方案里引用处,右键选择管理NuGet程序包,浏览->搜索 Microsoft.Windows.SDK.Contracts并安装。需要几分钟时间。
代码实现基本大同小异,主要是UWP API的引入方式。
重点说明: 这一步中,很多博主的采用的方式如下:
1.修改工程文件.csproj, 在文件中增加
<TargetPlatformVersion>10.0</TargetPlatformVersion>
2.引用 Windows.Devices、Windows.Foundation、Windows.Security、Windows.Services、Windows.Storage等组件进行环境搭建的。
本人在Windows11+Vs2022环境中实测,编译时无法通过,会出现无法引用System.Runtime.dll组件、IDisposable 没有实现等错误。采用Nuget方式引用,解决上述问题。也遇到上述问题的码友可以参考本文的方式。
using System;
using System.Collections.Generic;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Foundation;
using Windows.Security.Cryptography;
namespace BleSolution
{
public class BleCore:IDisposable
{
private bool asyncLock = false;
/// <summary>
/// 搜索蓝牙设备对象
/// </summary>
private BluetoothLEAdvertisementWatcher deviceWatcher;
/// <summary>
/// 当前连接的服务
/// </summary>
public GattDeviceService CurrentService { get; set; }
/// <summary>
/// 当前连接的蓝牙设备
/// </summary>
public BluetoothLEDevice CurrentDevice { get; set; }
/// <summary>
/// 写特征对象
/// </summary>
public GattCharacteristic CurrentWriteCharacteristic { get; set; }
/// <summary>
/// 通知特征对象
/// </summary>
public GattCharacteristic CurrentNotifyCharacteristic { get; set; }
/// <summary>
/// 特性通知类型通知启用
/// </summary>
private const GattClientCharacteristicConfigurationDescriptorValue CHARACERISTIC_NOTIFICATION_TYPE = GattClientCharacteristicConfigurationDescriptorValue.Notify;
/// <summary>
/// 存储检测到的设备
/// </summary>
private List<BluetoothLEDevice> DeviceList = new List<BluetoothLEDevice>();
/// <summary>
/// 定义搜索蓝牙设备委托
/// </summary>
/// <param name="type"></param>
/// <param name="bluetoothLEDevice"></param>
public delegate void DeviceWatcherChangeEvent(MsgType type, BluetoothLEDevice bluetoothLEDevice);
/// <summary>
/// 搜索蓝牙事件
/// </summary>
public event DeviceWatcherChangeEvent DeviceWatcherChanged;
/// <summary>
/// 获取服务委托
/// </summary>
/// <param name="gattDeviceService"></param>
public delegate void GattDeviceServiceAddEvent(GattDeviceService gattDeviceService);
/// <summary>
/// 获取服务事件
/// </summary>
public event GattDeviceServiceAddEvent GattDeviceServiceAdded;
/// <summary>
/// 获取特征委托
/// </summary>
/// <param name="characteristic"></param>
public delegate void CharacteristicAddedEvent(GattCharacteristic characteristic);
/// <summary>
/// 获取特征事件
/// </summary>
public event CharacteristicAddedEvent CharacteristicAdded;
/// <summary>
/// 提示信息委托
/// </summary>
/// <param name="type"></param>
/// <param name="message"></param>
/// <param name="data"></param>
public delegate void MessageChangedEvent(MsgType type, string message, byte[] data = null);
/// <summary>
/// 提示信息事件
/// </summary>
public event MessageChangedEvent MessageChanged;
/// <summary>
/// 当前连接的蓝牙MAC
/// </summary>
private string CurrentDeviceMAC { get; set; }
public BleCore()
{
}
/// <summary>
/// 搜索蓝牙设备
/// </summary>
public void StartBleDeviceWatcher()
{
deviceWatcher = new BluetoothLEAdvertisementWatcher();
deviceWatcher.ScanningMode = BluetoothLEScanningMode.Active;
deviceWatche

本文介绍了如何在Windows11+VisualStudio2022环境下通过NuGet包管理器设置PackageReference,以解决使用UWPAPI时出现的系统运行时库引用问题。特别是针对BluetoothLE设备搜索,文章提供了详细的步骤和代码示例,包括搜索设备、连接、读写特征等操作,并解决了编译错误和组件引用问题。

1059

被折叠的 条评论
为什么被折叠?



