Есть ли способ перечислить все порты bluetooth com и получить их имена?
И по имени я не имею в виду COM10
, в этом случае я имею в виду GNSS:51622 'GNSS Server'
.
Используя 32Feet, я смог найти имена портов, но до сих пор не удастся сопоставить их с фактическим COM-портом.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Connecting to Bluetooth");
var client = new BluetoothClient();
Console.WriteLine("DiscoverDevices");
var devices = client.DiscoverDevices();
Console.WriteLine("Enumerating");
foreach (var device in devices)
{
if (!device.DeviceName.StartsWith("GNSS"))
continue;
Console.WriteLine(device.DeviceName);
try
{
Console.WriteLine("Getting serial ports");
var serviceRecords = device.GetServiceRecords(BluetoothService.SerialPort);
foreach (var serviceRecord in serviceRecords)
{
var name = GetName(serviceRecord);
Console.WriteLine(name);
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to get SerialPort");
Console.WriteLine(ex.ToString());
}
}
Console.ReadKey();
}
private static string GetName(ServiceRecord serviceRecord)
{
var nameAttribute = serviceRecord.SingleOrDefault(a => a.Id == 0);
var name = serviceRecord.GetPrimaryMultiLanguageStringAttributeById(nameAttribute.Id);
return name;
}
}
Вывод:
Connecting to Bluetooth
DiscoverDevices
Enumerating
GNSS:51622
Getting serial ports
COM1
COM2
COM3
GNSS Server