DoNetDrive.Connector.WindowsBLE 1.1.0

dotnet add package DoNetDrive.Connector.WindowsBLE --version 1.1.0
                    
NuGet\Install-Package DoNetDrive.Connector.WindowsBLE -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DoNetDrive.Connector.WindowsBLE" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DoNetDrive.Connector.WindowsBLE" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="DoNetDrive.Connector.WindowsBLE" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DoNetDrive.Connector.WindowsBLE --version 1.1.0
                    
#r "nuget: DoNetDrive.Connector.WindowsBLE, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=DoNetDrive.Connector.WindowsBLE&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=DoNetDrive.Connector.WindowsBLE&version=1.1.0
                    
Install as a Cake Tool

DoNetDrive.Connector.SerialPort

介绍

此类库是基于Win10 1809 以上版本SDK开发的用于蓝牙BLE通讯基础库

软件架构

基于 net8.0-windows10.0.17763.0 ;

使用说明


using DoNetDrive.Common.Extensions;
using DoNetDrive.Connector.WindowsBLE;
using DoNetDrive.Core;
using DoNetDrive.Core.Command.Byte;
using DoNetDrive.Core.Connector;
using DotNetty.Buffers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.System;

namespace WinFormsBLETest
{
    public partial class Form2 : Form
    {
        private BLESearchService SearchService;

        private HashSet<ulong> BLEAddrs = new HashSet<ulong>();

        private ConnectorAllocator mConnectorAllocator;

        private ConnectorObserverHandler Observer;
        private BLEDetail ConnectDetail;
        public Form2()
        {
            InitializeComponent();

            mConnectorAllocator = ConnectorAllocator.GetAllocator();
            mConnectorAllocator.AddBLEConnectorFactory();
            mConnectorAllocator.ConnectorClosedEvent += MConnectorAllocator_ConnectorClosedEvent;

            Observer = new ConnectorObserverHandler();
            Observer.DisposeResponseEvent += Observer_DisposeResponseEvent;
            Observer.DisposeRequestEvent += Observer_DisposeRequestEvent;
        }

        private void MConnectorAllocator_ConnectorClosedEvent(object sender, INConnectorDetail connector)
        {
            AddLog($" {connector} 连接已断开 ");
            this.Invoke(() => {
                ConnectDetail = null;
                btnOpen.Enabled = true;
                btnClose.Enabled = false;
            });
        }

        /// <summary>
        /// 接收到数据回调
        /// </summary>
        /// <param name="connector"></param>
        /// <param name="msg"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Observer_DisposeRequestEvent(INConnector connector, string msg)
        {
            AddLog("接收:" + msg);
        }

        /// <summary>
        /// 发送数据回调
        /// </summary>
        /// <param name="connector"></param>
        /// <param name="msg"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Observer_DisposeResponseEvent(INConnector connector, string msg)
        {
            AddLog("发送:" + msg);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (SearchService == null)
            {
                SearchService = new BLESearchService(SearchBLECallBlack);
            }
            ClearBLEDevice();
            var ret = SearchService.StartBleDeviceWatcher();
            if (ret == false)
            {
                AddLog("电脑可能不支持蓝牙或未开启蓝牙功能");
                var uri = new Uri("ms-settings-bluetooth:turnon");
                Launcher.LaunchUriAsync(uri);
                return;
            }
                

            btnSearch.Enabled = false;
            
            Task.Run(async () =>
            {
                await Task.Delay(15000);
                SearchService.StopBleDeviceWatcher();
                this.Invoke(() => btnSearch.Enabled = true);
            });
        }


        private class BLEItem
        {
            public ulong Addr;
            public string Name;
            public override string ToString()
            {
                return $"{Name} - {Addr:X12}";
            }
        }

        private void SearchBLECallBlack(ulong BLEAddr, string BLEName)
        {
            this.Invoke(() =>
            {
                if (!BLEAddrs.Contains(BLEAddr))
                {
                    if (!string.IsNullOrEmpty(BLEName))
                    {
                        var item = new BLEItem()
                        {
                            Addr = BLEAddr,
                            Name = BLEName
                        };
                        cmbBLEList.Items.Add(item);
                        BLEAddrs.Add(BLEAddr);
                        AddLog($"搜索到蓝牙设备:{item}");
                    }

                }
            });
        }

        private void ClearBLEDevice()
        {
            cmbBLEList.Items.Clear();
            BLEAddrs.Clear();
        }

        private void AddLog(string txt)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(() => AddLog(txt));
                return;
            }

            string sLog = $"{DateTime.Now:HH:mm:ss.fff} - {txt} \r\n";

            if (txtLog.TextLength > 20000)
            {
                txtLog.Text = txtLog.Text.Substring(10000);
            }

            txtLog.AppendText(sLog);


        }

        private async void btnOpen_Click(object sender, EventArgs e)
        {

            var item = cmbBLEList.SelectedItem as BLEItem;
            if (item == null)
            {
                ShowMsgBox("请选择需要连接的蓝牙设备名称!");
                return;
            }

            string sUUID = txtServiceUUID.Text;

            if (string.IsNullOrEmpty(sUUID))
            {
                ShowMsgBox("请输入要连接的蓝牙服务UUID!");
                return;
            }


            BLEDetail dtl = new BLEDetail(item.Addr, item.Name, sUUID);
            try
            {
                btnOpen.Enabled = false;

                var BLEConn = await mConnectorAllocator.OpenConnectorAsync(dtl) as BLEConnector;

                if (BLEConn == null) return;

                BLEConn.AddRequestHandle(Observer);

                ConnectDetail = dtl;

                AddLog("打开蓝牙设备连接成功!");
                
                btnClose.Enabled = true;
            }
            catch (Exception ex)
            {
                btnOpen.Enabled = true;
                AddLog("打开蓝牙设备连接时发生错误!" + ex.Message);
            }


        }
        private async void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                await mConnectorAllocator.CloseConnectorAsync(ConnectDetail);
                ConnectDetail = null;
                AddLog("关闭蓝牙设备连接成功!");
                btnOpen.Enabled = true;
                btnClose.Enabled = false;
            }
            catch (Exception ex)
            {

                AddLog("关闭蓝牙设备连接失败!" + ex.Message);
            }
        }

        private void ShowMsgBox(string sTxt)
        {
            MessageBox.Show("请选择需要连接的蓝牙设备名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private async void btnSend_Click(object sender, EventArgs e)
        {
            if (ConnectDetail == null)
            {
                ShowMsgBox("请先连接蓝牙!");
                return;
            }

            string txt = txtSendData.Text;
            if (string.IsNullOrEmpty(txt))
            {
                ShowMsgBox("请先输入待发送内容!");
                return;
            }

            byte[] sendBytes;
            if (chkHEX.Checked)
            {
                if (!txt.IsHex())
                {
                    ShowMsgBox("输入的内容不是十六进制,请重新输入!");
                    return;
                }
                if (chkNewline.Checked)
                {
                    byte[] hexBuf = txt.HexToByte();
                    byte[] newLinebytes = System.Text.Encoding.ASCII.GetBytes("\r\n");
                    sendBytes = hexBuf.Concat(newLinebytes).ToArray();
                }
                else
                    sendBytes = txt.HexToByte();



            }
            else
            {
                if (chkNewline.Checked)
                {
                    txt = txt + "\r\n";
                }
                sendBytes = System.Text.Encoding.ASCII.GetBytes(txt);
            }

            using ByteCommandDetail cmdDtl = new ByteCommandDetail(ConnectDetail);

            var buf = Unpooled.WrappedBuffer(sendBytes);
            using var par = new ByteCommandParameter(buf);
            using var cmd = new ByteCommand(cmdDtl, par);

            try
            {
                await mConnectorAllocator.AddCommandAsync(cmd);
                AddLog("发送完毕!");
            }
            catch (Exception ex)
            {

                AddLog("发送时发生错误:" + ex.Message);
            }


        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
    }
}

版本记录

ver 1.0.0

根据Win10 SDK 实现基于Win10 1809 版本以上系统的基于蓝牙BLE的通讯实现

Product Compatible and additional computed target framework versions.
.NET net8.0-windows10.0.17763 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 107 12/20/2024
1.0.0 218 1/6/2024