在网上找了很多资料关于Winform如何使用电脑自带蓝牙与设备(手机、仪器、工具、3C电器等等)的低功耗蓝牙(BLE)进行通信的示例,找了很久都没有一个完整的解决方案,最近终于经过自己的不断研究实现了在Winform上实现了与BLE设备的蓝牙通信。
Winform BLE可在Windows 10/11系统下运行。
这里将几个关键点说明下,供大家参考:
1. 准备工作
添加两个引用:
System.Runtime.WindowsRuntime
Windows
路径:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll
C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.19041.0\Windows.winmd
2. 工程中添加命名空间
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
添加完成后,BluetoothLEDevice,DeviceWatcher