How can I find out a COM port number of a bluetooth device in c#?

后端 未结 6 1007
逝去的感伤
逝去的感伤 2020-12-10 02:44

My company developed a device that communicates with a PC via Bluetooth using a virtual COM port.

Now we need a user to pair a device with a PC (MS Windows OS) firs

6条回答
  •  醉酒成梦
    2020-12-10 03:18

    I manage to get the bluetooth name and the COM port by fiddling the registry key

    The pseudo code to obtain the bluetooth information is below:

    • enumerate all the COM port available in the PNP
    • obtain the device classGuid
    • search the bluetooth address from the classGuid
    • when the bluetooth address is known, the bluetooth name can be obtained from the this registry SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Devices

    The code is below, just call the GetBluetoothPort(), it will return a list of bluetooth devices, and you could connect them by passing the COM port number to the SerialPort class

    public static string[] GetBluetoothPort()
    {
        Regex regexPortName = new Regex(@"(COM\d+)");
    
        List portList = new List();
    
        ManagementObjectSearcher searchSerial = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity");
    
        foreach (ManagementObject obj in searchSerial.Get()) {
            string name = obj["Name"] as string;
            string classGuid = obj["ClassGuid"] as string;
            string deviceID = obj["DeviceID"] as string;
    
            if (classGuid != null && deviceID != null) {
                if (String.Equals(classGuid, "{4d36e978-e325-11ce-bfc1-08002be10318}", StringComparison.InvariantCulture)) {
                    string[] tokens = deviceID.Split('&');
    
                    if (tokens.Length >= 4) {
                        string[] addressToken = tokens[4].Split('_');
                        string bluetoothAddress = addressToken[0];
    
                        Match m = regexPortName.Match(name);
                        string comPortNumber = "";
                        if (m.Success) {
                            comPortNumber = m.Groups[1].ToString();
                        }
    
                        if (Convert.ToUInt64(bluetoothAddress, 16) > 0) {
                            string bluetoothName = GetBluetoothRegistryName(bluetoothAddress);
                            portList.Add(String.Format("{0} {1} ({2})", bluetoothName, bluetoothAddress, comPortNumber));
                        }
                    }
                }                    
            }
        }
    
        return portList.ToArray();
    }
    
    private static string GetBluetoothRegistryName(string address)
    {
        string deviceName = "";
    
        string registryPath = @"SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\Devices";
        string devicePath = String.Format(@"{0}\{1}", registryPath, address);
    
        using (RegistryKey key = Registry.LocalMachine.OpenSubKey(devicePath)) {
            if (key != null) {
                Object o = key.GetValue("Name");
    
                byte[] raw = o as byte[];
    
                if (raw != null) {
                    deviceName = Encoding.ASCII.GetString(raw);
                }
            }
        }
    
        return deviceName;
    }
    

提交回复
热议问题