使用C#进行低功耗蓝牙BLE开发

本文介绍了如何在Windows11+VisualStudio2022环境下通过NuGet包管理器设置PackageReference,以解决使用UWPAPI时出现的系统运行时库引用问题。特别是针对BluetoothLE设备搜索,文章提供了详细的步骤和代码示例,包括搜索设备、连接、读写特征等操作,并解决了编译错误和组件引用问题。
  1. 打开VS->工具->NuGet包管理器->程序包管理器设置->常规->默认包管理格式(设置为PackageRefrence)

  1. 解决方案里引用处,右键选择管理NuGet程序包,浏览->搜索 Microsoft.Windows.SDK.Contracts并安装。需要几分钟时间。

  1. 代码实现基本大同小异,主要是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
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lontran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值