I have this class to enumerate Bluetooth COM ports:
public sealed class BluetoothComPort
{
public static IReadOnlyList<bluetoothcomport> FindAll()
{
List<bluetoothcomport> ports = new List<bluetoothcomport>();
IntPtr handle = NativeMethods.SetupDiGetClassDevs(ref NativeMethods.GUID_DEVCLASS_PORTS, null, IntPtr.Zero, NativeMethods.DIGCF.PRESENT);
if (handle != IntPtr.Zero)
{
try
{
NativeMethods.SP_DEVINFO_DATA dat = new NativeMethods.SP_DEVINFO_DATA();
dat.cbSize = Marshal.SizeOf(dat);
uint i = 0;
while (NativeMethods.SetupDiEnumDeviceInfo(handle, i++, ref dat))
{
string remoteServiceName = string.Empty;
StringBuilder sbid = new StringBuilder(256);
int size;
NativeMethods.SetupDiGetDeviceInstanceId(handle, ref dat, sbid, sbid.Capacity, out size);
Debug.WriteLine(sbid);
long addr = GetBluetoothAddressFromDevicePath(sbid.ToString());
if (addr != long.MinValue && addr != 0)
{
IntPtr hkey = NativeMethods.SetupDiOpenDevRegKey(handle, ref dat, NativeMethods.DICS.GLOBAL, 0, NativeMethods.DIREG.DEV, 1);
var key = Microsoft.Win32.RegistryKey.FromHandle(new Microsoft.Win32.SafeHandles.SafeRegistryHandle(hkey, true));
object name = key.GetValue("PortName");
var pkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\BTHPORT\\Parameters\\Devices\\" + addr.ToString("x12"));
if (pkey != null)
{
foreach (string nm in pkey.GetSubKeyNames())
{
if (nm.StartsWith("ServicesFor"))
{
var skey = pkey.OpenSubKey(nm);
string s = sbid.ToString();
int ifirst = s.IndexOf("{");
string uuid = s.Substring(ifirst, s.IndexOf("}") - ifirst + 1);
var ukey = skey.OpenSubKey(uuid);
string iid = s.Substring(s.LastIndexOf("_") + 1);
var instKey = ukey.OpenSubKey(iid);
object o = instKey.GetValue("PriLangServiceName");
if (o != null)
{
byte[] chars = o as byte[];
remoteServiceName = Encoding.UTF8.GetString(chars).Trim();
}
}
}
}
ports.Add(new BluetoothComPort(sbid.ToString(), addr, name.ToString(), remoteServiceName));
key.Dispose();
}
}
}
finally
{
NativeMethods.SetupDiDestroyDeviceInfoList(handle);
}
}
return ports;
}
private string _deviceId;
private long _bluetoothAddress;
private string _portName;
private string _remoteServiceName;
internal BluetoothComPort(string deviceId, long bluetoothAddress, string portName, string remoteServiceName)
{
_deviceId = deviceId;
_bluetoothAddress = bluetoothAddress;
_portName = portName;
_remoteServiceName = remoteServiceName;
}
public string DeviceId
{
get
{
return _deviceId;
}
}
public long BluetoothAddress
{
get
{
return _bluetoothAddress;
}
}
public string PortName
{
get
{
return _portName;
}
}
public string RemoteServiceName
{
get
{
return _remoteServiceName;
}
}
private static long GetBluetoothAddressFromDevicePath(string path)
{
if (path.StartsWith("BTHENUM"))
{
int start = path.LastIndexOf('&');
int end = path.LastIndexOf('_');
string addressString = path.Substring(start + 1, (end - start) - 1);
return long.Parse(addressString, System.Globalization.NumberStyles.HexNumber);
}
return long.MinValue;
}
private static class NativeMethods
{
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr SetupDiGetClassDevs(
ref Guid classGuid,
[MarshalAs(UnmanagedType.LPTStr)] string enumerator,
IntPtr hwndParent,
DIGCF flags);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetupDiEnumDeviceInfo(
IntPtr deviceInfoSet,
uint memberIndex,
ref SP_DEVINFO_DATA deviceInfoData);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetupDiDestroyDeviceInfoList(IntPtr deviceInfoSet);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetupDiGetDeviceInstanceId(
IntPtr deviceInfoSet,
ref SP_DEVINFO_DATA deviceInfoData,
System.Text.StringBuilder deviceInstanceId,
int deviceInstanceIdSize,
out int requiredSize);
internal static Guid GUID_DEVCLASS_PORTS = new Guid("{4d36e978-e325-11ce-bfc1-08002be10318}");
[DllImport("setupapi.dll", SetLastError = true)]
internal static extern IntPtr SetupDiOpenDevRegKey(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData,
DICS Scope, int HwProfile, DIREG KeyType, int samDesired);
[Flags]
internal enum DICS
{
GLOBAL = 0x00000001,
CONFIGSPECIFIC = 0x00000002,
}
internal enum DIREG
{
DEV = 0x00000001,
DRV = 0x00000002,
}
[Flags()]
internal enum DIGCF
{
PRESENT = 0x00000002,
ALLCLASSES = 0x00000004,
PROFILE = 0x00000008,
}
[StructLayout(LayoutKind.Sequential)]
internal struct SP_DEVINFO_DATA
{
internal int cbSize;
internal Guid ClassGuid;
internal uint DevInst;
internal IntPtr Reserved;
}
}
}
Please can you explain me how to use this code to enumerate the Bluetooth COM ports?
Thank you
What I have tried:
I'm not able to use that code.