0

I use 32 feet net to get bluetooth devices but I also need their com ports. I have tried the code below but it drops the connection when doing the detection. Specifically "string pnpId = mo.GetPropertyValue("PNPDeviceID").ToString();". Is there another way of finding a com port based on a bluetooth id? Is there a way to fix this code? I've been working on this for like 10 hours now...

private static string GetBluetoothPort(string deviceAddress) {

  const string Win32_SerialPort = "Win32_SerialPort";
  Console.WriteLine("querrieing");
  SelectQuery q = new SelectQuery(Win32_SerialPort);
  Console.WriteLine("querrieing done");

  ManagementObjectSearcher s = new ManagementObjectSearcher(q);
  foreach(object cur in s.Get()) {
    ManagementObject mo = (ManagementObject) cur;
    string pnpId = mo.GetPropertyValue("PNPDeviceID").ToString();

    if (pnpId.Contains(deviceAddress)) {
      object captionObject = mo.GetPropertyValue("Caption");
      string caption = captionObject.ToString();
      int index = caption.LastIndexOf("(COM");
      if (index > 0) {
        string portString = caption.Substring(index);
        string comPort = portString.
        Replace("(", string.Empty).Replace(")", string.Empty);
        Console.WriteLine("done with getting port");
        return comPort;
      }
    }
  }
  return null;
}   
ICE_Plane
  • 25
  • 6
  • 1
    See the bottom of this [post](https://stackoverflow.com/a/74895268/10024425) for some PowerShell commands that can be used for testing. _Win32_SerialPort_ is probably not where you'll find the information you're looking for. – Tu deschizi eu inchid May 26 '23 at 19:35

0 Answers0