在“设备和打印机”窗口中获取蓝牙设备的名称

时间:2014-09-18 09:17:10

标签: c# visual-studio bluetooth

我知道有些问题涉及在设备管理器上收集设备的友好名称,但我不能这样做,因为该设备简称为“Stardard Serial over Bluetooth link(COM)”而且我有很多具有相同引用的虚拟端口。我想要设备的名称,如“设备和打印机”窗口上所示:

How to get the device name from here

我在C#中这样做,目前只是获取系统上可用COM端口的列表,并从内存中选择我知道的那个。

3 个答案:

答案 0 :(得分:1)

我设法使用32Feet.Net让它工作。

您可以通过

搜索设备
    BluetoothClient client = new BluetoothClient();
        devices = client.DiscoverDevicesInRange();
        foreach (BluetoothDeviceInfo d in devices)
        {
            items.Add(d.DeviceName);
        }

这将列出您在“设备和打印机”窗口中看到的友好名称,而不是“标准串行通过蓝牙链接”。

如果您想要像我这样的COM端口或任何其他信息,那么您只需执行WMI查询,例如

    System.Management.ManagementObjectSearcher Searcher = new System.Management.ManagementObjectSearcher("Select * from WIN32_SerialPort");
        foreach (System.Management.ManagementObject Port in Searcher.Get())
        {
             //your comparison or code here
        }

答案 1 :(得分:0)

我正在使用32feet.Net库使用我的自定义代码,这有助于我在C#控制台应用程序中获取设备友好名称以及该设备附带的COM端口信息。

我正在使用下面的代码来检测Topaz-Signature设备,其友好名称是“T-S460-BT2”。你可以替换这个

  

string FriendlyDeviceName =“T-S460-BT2”;

在您要搜索的设备名称的代码中。

using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Bluetooth.Widcomm;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Text.RegularExpressions;

namespace SearchDevice
{
    class Program
    {
        static void Main(string[] args)
        {

            string FriendlyDeviceName =  "T-S460-BT2";

            if (BluetoothRadio.IsSupported)
            {
                BluetoothClient client = new BluetoothClient();
                BluetoothDeviceInfo[] devices;
                devices = client.DiscoverDevicesInRange();
                foreach (BluetoothDeviceInfo d in devices)
                {
                    if (Regex.IsMatch(d.DeviceName, FriendlyDeviceName, RegexOptions.IgnoreCase))
                    {
                        try
                        {
                            string query = string.Format("SELECT Name, DeviceID, PNPDeviceID from WIN32_SerialPort");
                            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                            ManagementObjectCollection osDetailsCollection = searcher.Get();
                            foreach (ManagementObject mo in osDetailsCollection)
                            {
                                string PNPDeviceID = (string)mo.GetPropertyValue("PNPDeviceID");
                                if (PNPDeviceID != null && Regex.IsMatch(PNPDeviceID, d.DeviceAddress + "", RegexOptions.IgnoreCase))
                                {
                                    Console.WriteLine("{0}", ((string)mo.GetPropertyValue("DeviceId")).Replace("COM", ""));
                                }
                            }
                        }
                        catch (Exception exx)
                        {

                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Not Supported");
            }

            Console.ReadLine();

        }
    }
}

答案 2 :(得分:0)

我设法通过摆弄注册表项来获取蓝牙名称,地址和COM端口号而不使用32feet.net库。

然后,您可以通过传递COM端口号使用SerialPort类连接蓝牙设备。

获取蓝牙信息的伪代码如下:

  • 枚举PNP中可用的所有COM端口
  • 获取设备classGuid
  • 从classGuid
  • 搜索蓝牙地址
  • 当蓝牙地址已知时,可以从 - 此注册表SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Devices
  • 获取蓝牙名称

我已在以下链接中发布了我的代码:

https://stackoverflow.com/a/36298213/2297825