windows ble蓝牙扫描C#代码
时间: 2023-12-23 15:04:48 浏览: 41
以下是 Windows 上使用 C# 实现的 BLE 蓝牙扫描代码示例:
```c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;
namespace BLEScanner
{
class Program
{
static void Main(string[] args)
{
// 枚举所有蓝牙设备
BluetoothRadio.BluetoothRadioMode mode = BluetoothRadio.PrimaryRadio.Mode;
BluetoothClient client = new BluetoothClient();
BluetoothDeviceInfo[] devices = client.DiscoverDevices();
// 输出设备信息
foreach (BluetoothDeviceInfo device in devices)
{
Console.WriteLine("Name: " + device.DeviceName);
Console.WriteLine("Address: " + device.DeviceAddress);
Console.WriteLine("Class: " + device.ClassOfDevice);
Console.WriteLine();
}
}
}
}
```
该示例使用了 C# 中的 InTheHand.Net.Bluetooth 库,可以枚举所有可连接的蓝牙设备,并输出设备名称、地址和设备类别等信息。需要注意的是,该程序需要以管理员权限运行才能访问蓝牙设备。