有啥方法可以从 Windows 10 中的 c# 桌面应用程序使用蓝牙 LE?

     2023-02-19     273

关键词:

【中文标题】有啥方法可以从 Windows 10 中的 c# 桌面应用程序使用蓝牙 LE?【英文标题】:Is there any way to use Bluetooth LE from a c# desktop app in windows 10?有什么方法可以从 Windows 10 中的 c# 桌面应用程序使用蓝牙 LE? 【发布时间】:2016-09-16 22:07:01 【问题描述】:

我在网上找到的所有关于蓝牙 LE 的东西都需要一个通用的 windows 应用程序,这完全不适合我。

有没有办法在 C# 中使用蓝牙 LE,而不必像在 UWP 上那样编写整个应用程序?

【问题讨论】:

【参考方案1】:

您可以在 C# 桌面应用程序中使用 C# API!我有一个sample here in GitHub。

一般来说,要访问 C# APIS,请向您的项目添加两个引用:

    C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

请注意,#2 版本取决于您使用的 .NET 版本。

不支持后台任务,但所有其他蓝牙 C# 功能应该在那里。

【讨论】:

我试过了。在 Windows 8 上工作,但无法在 Windows 10 上工作。我不知道我做错了什么。此外,蓝牙 LE api 在 Windows 8 中受到严重限制。因此,虽然我可以针对 Windows 8 并仍然在 Windows 10 中运行,但这对我来说不起作用。我需要使用 Windows 10 的蓝牙 LE api @CedricMamo 自己尝试过这样做 - 现在更新答案! 似乎 #2 可以在我的操作系统上的不同路径下找到 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll 从 PowerShell 访问它需要什么? 如果我添加这两个引用,我仍然无法访问Windows.Devices【参考方案2】:

自 XP 时代以来,我一直在使用并围绕 Microsoft 部分蓝牙 API 解决方案工作。我从 BTFramework 中找到了一个我在许多平台和多种语言中广泛使用的库。 BTFramework 的蓝牙经典和低功耗 API 包易于使用、非常可靠,并且它们对我发现的任何缺陷都反应迅速。因此,我们的商业产品从蓝牙端出现的故障为零。 BTFramework 的团队已经着手解决微软对该标准的部分实施。顺便说一下,我们主要用 C# dot NET 编写代码。而且我们在应用程序中使用了很多线程。

【讨论】:

感谢您的参考。有时,解决一个特别棘手的问题的第三方解决方案是明智的答案。我会检查他们...【参考方案3】:
Look at the code below this shows how to scan and connect to a ble device using winform application

using System;
using System.Windows.Forms;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;

namespace TestBle

    public partial class Form1 : Form
    
        public Form1()
        
            InitializeComponent();
        

        private async void button1_ClickAsync(object sender, EventArgs e)
        
            string[] requestedProperties =  "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" ;

            DeviceWatcher deviceWatcher =
                        DeviceInformation.CreateWatcher(
                                BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
                                requestedProperties,
                                DeviceInformationKind.AssociationEndpoint);

            // Register event handlers before starting the watcher.
            // Added, Updated and Removed are required to get all nearby devices
            deviceWatcher.Added += DeviceWatcher_Added;
            deviceWatcher.Updated += DeviceWatcher_Updated;
            deviceWatcher.Removed += DeviceWatcher_Removed;
            deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
            deviceWatcher.Stopped += DeviceWatcher_Stopped;

            // Start the watcher.
            deviceWatcher.Start();
        

        private void DeviceWatcher_Stopped(DeviceWatcher sender, object args)
        
            //throw new NotImplementedException();
        

        private void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object args)
        
            string a = "";
        

        private void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
        
            string a = "";
        

        private void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
        
            string a = "";
        

        private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
        
            string[] split = args.Id.Split('-');
            if(split[1] == "84:2e:14:aa:65:13")
            
                BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(args.Id);
                GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();

                if (result.Status == GattCommunicationStatus.Success)
                
                    var services = result.Services;
                    // ...
                
            `enter code here`

        
    

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

有啥方法可以使用 c# 在 Windows 中关闭“互联网”?

】有啥方法可以使用c#在Windows中关闭“互联网”?【英文标题】:Anywaytoturnthe"internetoff"inwindowsusingc#?有什么方法可以使用c#在Windows中关闭“互联网”?【发布时间】:2009-08-0702:16:34【问题描述】:我正在寻找指向c#中API的... 查看详情

C#中的方法与属性-有啥区别[重复]

】C#中的方法与属性-有啥区别[重复]【英文标题】:MethodvsPropertyinC#-what\'sthedifference[duplicate]C#中的方法与属性-有什么区别[重复]【发布时间】:2010-11-2014:49:43【问题描述】:可能重复:PropertiesvsMethods在方法中你也可以输入一些代... 查看详情

有啥方法可以使用 C# 获取 Windows Phone 设备的 IMEI 号码

】有啥方法可以使用C#获取WindowsPhone设备的IMEI号码【英文标题】:IsthereanywaytofetchtheIMEINumberoftheWindowsPhonedeviceUsingC#有什么方法可以使用C#获取WindowsPhone设备的IMEI号码【发布时间】:2017-12-1623:08:36【问题描述】:有什么方法可以使... 查看详情

如何使用 C# 从 Windows 10 日历中检索 UWP 中的约会

】如何使用C#从Windows10日历中检索UWP中的约会【英文标题】:HowtoretrieveappointmentsinUWPfromaWindows10calendarusingC#【发布时间】:2016-09-0617:52:35【问题描述】:我正在使用C#在Windows10中创建通用Windows应用程序,该应用程序在Windows日历应... 查看详情

有啥方法可以为 c# .NET 中的特定数据库表自动生成 BLL 类?

】有啥方法可以为c#.NET中的特定数据库表自动生成BLL类?【英文标题】:IsthereanywayIcanautomaticallygenerateBLLclassesforaparticulardatabasetableinc#.NET?有什么方法可以为c#.NET中的特定数据库表自动生成BLL类?【发布时间】:2019-10-2523:34:48【问... 查看详情

有啥方法可以从 Windows 7 上的 Windows 服务启动 GUI 应用程序?

】有啥方法可以从Windows7上的Windows服务启动GUI应用程序?【英文标题】:IsthereanywaytostartaGUIapplicationfromawindowsserviceonWindows7?有什么方法可以从Windows7上的Windows服务启动GUI应用程序?【发布时间】:2011-07-0103:03:56【问题描述】:我... 查看详情

C# 中的这个非托管代码有啥问题?

】C#中的这个非托管代码有啥问题?【英文标题】:What\'swrongwiththisunmanagedcodeinC#?C#中的这个非托管代码有什么问题?【发布时间】:2017-10-0106:16:37【问题描述】:我试图从层次结构中的每个控件中获取文本。如果我使用unsafe方法... 查看详情

win10有啥方法可以自定义windows更新吗

参考技术A1、同时按windows+r键,打开运行界面;  2、打开策略组,并打开管理模板;  3、选择打开windows组件;  4、打开windows更新;  5、在右侧的窗口中选择配置自动更新;  6、在图示位置,单击“策略设置”; ... 查看详情

C# 和 MySQL .NET 连接器 - 有啥方法可以防止泛型类中的 SQL 注入攻击?

】C#和MySQL.NET连接器-有啥方法可以防止泛型类中的SQL注入攻击?【英文标题】:C#andMySQL.NETConnector-AnywayofpreventingSQLInjectionattacksinagenericclass?C#和MySQL.NET连接器-有什么方法可以防止泛型类中的SQL注入攻击?【发布时间】:2011-02-1601:... 查看详情

有啥方法可以确定 Windows 中可移动驱动器的速度吗?

】有啥方法可以确定Windows中可移动驱动器的速度吗?【英文标题】:Anywaytodeterminespeedofaremovabledriveinwindows?有什么方法可以确定Windows中可移动驱动器的速度吗?【发布时间】:2008-10-0815:42:51【问题描述】:有什么方法可以在不实... 查看详情

有啥方法可以使 SQLPlus 中的输出更宽(在 Windows 上)?

】有啥方法可以使SQLPlus中的输出更宽(在Windows上)?【英文标题】:IsthereanywaytomaketheoutputinSQLPluswider(onWindows)?有什么方法可以使SQLPlus中的输出更宽(在Windows上)?【发布时间】:2014-11-1219:18:38【问题描述】:为了更轻松地在SQ... 查看详情

有啥方法可以从 Firestore 中的数组更新特定索引

】有啥方法可以从Firestore中的数组更新特定索引【英文标题】:IsthereanywaytoupdateaspecificindexfromthearrayinFirestore有什么方法可以从Firestore中的数组更新特定索引【发布时间】:2019-05-2823:29:55【问题描述】:有没有办法从Firebase/firestor... 查看详情

有啥方法可以从存储库中的 Spring Security 获取当前 userId?

】有啥方法可以从存储库中的SpringSecurity获取当前userId?【英文标题】:AnywaytogetcurrentuserIdfromSpringSecurityinarepository?有什么方法可以从存储库中的SpringSecurity获取当前userId?【发布时间】:2020-12-1022:11:37【问题描述】:我有一个带... 查看详情

不处理 C# 方法的返回值可以吗?这个例子中有啥好的做法?

】不处理C#方法的返回值可以吗?这个例子中有啥好的做法?【英文标题】:IsitOKnottohandlereturnedvalueofaC#method?Whatisgoodpracticeinthisexample?不处理C#方法的返回值可以吗?这个例子中有什么好的做法?【发布时间】:2011-10-1600:49:28【问... 查看详情

有啥方法可以保护在开源项目中进行的 Web 服务调用? [关闭]

】有啥方法可以保护在开源项目中进行的Web服务调用?[关闭]【英文标题】:Anywaytosecureawebservicecallmadeinanopen-sourceproject?[closed]有什么方法可以保护在开源项目中进行的Web服务调用?[关闭]【发布时间】:2011-12-2216:59:10【问题描述... 查看详情

c#中的format和console.writeline有啥区别

C#中的format和console.writeline有什么区别Console.WriteLine是把信息输出到控制台上,而Format是格式化字符串的方法,可以通过0,1这种方式占位。在Console.WriteLine方法中,需要输出的字符串,默认已经使用了Format方法,可以直接使用Console... 查看详情

当实例缺少公共构造函数时,有啥方法可以从 .NET 中的 AppDomain 创建实例?

】当实例缺少公共构造函数时,有啥方法可以从.NET中的AppDomain创建实例?【英文标题】:AnywaytocreateaninstancefromanAppDomainin.NETwhentheninstancelackspublicconstructor?当实例缺少公共构造函数时,有什么方法可以从.NET中的AppDomain创建实例?... 查看详情

c#中string和string有啥区别?

一个开头大写,一个开头小写!在C#中,string是System.String的别名,所以基本上在使用时是没有差别的。习惯上,我们把字符串当作对象时(有值的对象实体),我们用string。而我们把它当类时(需要字符串类中定义的方法),我... 查看详情