如何获得通过蓝牙棒连接移动设备的 COM 端口?我已经可以得到设备的名称,例如。'Nokia C2-01'device.DeviceName
使用32feet library
但我怎样才能让它看起来像这样?"Nokia c2-01 connected through COM7"
?
问问题
1204 次
1 回答
0
首先,您需要使用以下方法获取设备地址:
string comPort = GetBluetoothPort(device.DeviceAddress.ToString());
if(!string.IsNullOrWhiteSpace(comPort))
{
// enter desired output here
}
GetBluetoothPort() 方法如下所示:
using System.Management;
private string GetBluetoothPort(string deviceAddress)
{
const string Win32_SerialPort = "Win32_SerialPort";
SelectQuery q = new SelectQuery(Win32_SerialPort);
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);
return comPort;
}
}
}
return null;
}
这将返回端口名称,即 COM7
于 2015-12-17T17:55:17.333 回答