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;
}