如何通过蓝牙棒连接移动设备的COM端口?我已经可以得到设备的名称了,例如。‘诺基亚C2-01’,使用32feet library的device.DeviceName,但我如何才能使它看起来像这样?"Nokia c2-01 connected through COM7"
发布于 2015-12-18 01:55:17
首先,您需要使用以下命令获取设备地址:
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
https://stackoverflow.com/questions/33007854
复制相似问题