获取 C# .NET 框架中的蓝牙设备列表

Ask*_*man 4 .net c# bluetooth

我创建了一个简单的 C# .NET 框架控制台应用程序。

在其中,我试图获取当前连接到计算机的已配对蓝牙设备的列表。但是,我无法弄清楚如何从代码中访问任何蓝牙服务。

我环顾了互联网,我能找到的只是一种在通用 Windows 项目 (UWP) 中执行此操作的方法,在那里我可以简单地使用using Windows.Devices.Bluetooth包含我需要的所有内容的命名空间,但是在 .NET 框架控制台应用程序中,这命名空间不可用。

我不需要任何使用蓝牙的高级方法,我只需要一个当前连接和配对的蓝牙设备的列表。

小智 7

尝试使用以下代码:

            BluetoothClient client = new BluetoothClient();
            List<string> items = new List<string>();
            BluetoothDeviceInfo[] devices = client.DiscoverDevicesInRange();
            foreach (BluetoothDeviceInfo d in devices)
            {
                items.Add(d.DeviceName);
            }
Run Code Online (Sandbox Code Playgroud)

您将通过 Package Manager Console安装32feet.NET来获得 BluetoothClient 的参考。

PM> Install-Package 32feet.NET -Version 3.5.0
Run Code Online (Sandbox Code Playgroud)

安装后,您将在 References 中获得InTheHand.Net.Personal dll,然后使用 InTheHand.Net.Sockets添加命名空间在你的代码中

现在您将能够访问BluetoothClient

希望这可以帮助!!