Basically my application should recognize USB and Bluetooth devices at same time. I am facing issue in below code to use USB and Bluetooth GUID at same time in static class. This static class require GUID to enable event to recognize USB and Bluetooth devices. Is it possible to make a generic class to merge both in one static class? Or is there any generic GUID which can be used for both USB and Bluetooth. c# Code below:
static class UsbNotification
{
public const int DbtDevicearrival = 0x8000; // system detected a new device
public const int DbtDeviceremovecomplete = 0x8004; // device is gone
public const int WmDevicechange = 0x0219; // device change event
private const int DbtDevtypDeviceinterface = 5;
private static readonly Guid GuidDevinterfaceUSBDevice = new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"); // USB devices
private static readonly Guid GuidDevinterfacebluetoothpair = new Guid("00F40965-E89D-4487-9890-87C3ABB211F4");//Bluetooth
private static IntPtr notificationHandle;
/// <summary>
/// Registers a window to receive notifications when USB devices are plugged or unplugged.
/// </summary>
/// <param name="windowHandle">Handle to the window receiving notifications.</param>
public static void RegisterUsbDeviceNotification(IntPtr windowHandle)
{
DevBroadcastDeviceinterface dbi = new DevBroadcastDeviceinterface
{
DeviceType = DbtDevtypDeviceinterface,
Reserved = 0,
ClassGuid = GuidDevinterfacebluetoothpair,//I want to assign GUID to use both USB and Bluetooth
Name = 0
};
dbi.Size = Marshal.SizeOf(dbi);
IntPtr buffer = Marshal.AllocHGlobal(dbi.Size);
Marshal.StructureToPtr(dbi, buffer, true);
notificationHandle = RegisterDeviceNotification(windowHandle, buffer, 0);
}
/// <summary>
/// Unregisters the window for USB device notifications
/// </summary>
public static void UnregisterUsbDeviceNotification()
{
UnregisterDeviceNotification(notificationHandle);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr RegisterDeviceNotification(IntPtr recipient, IntPtr notificationFilter, int flags);
[DllImport("user32.dll")]
private static extern bool UnregisterDeviceNotification(IntPtr handle);
[StructLayout(LayoutKind.Sequential)]
private struct DevBroadcastDeviceinterface
{
internal int Size;
internal int DeviceType;
internal int Reserved;
internal Guid ClassGuid;
internal short Name;
}
}
protected override void OnSourceInitialized(System.EventArgs e)
{
base.OnSourceInitialized(e);
// Adds the windows message processing hook and registers USB device add/removal notification.
HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
if (source != null)
{
source.AddHook(WndProc);
UsbNotification.RegisterUsbDeviceNotification(source.Handle);//USB and Bluetooth
}
}
private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == UsbNotification.WmDevicechange)
{
switch ((int)wParam)
{
case UsbNotification.DbtDeviceremovecomplete:
MessageBox.Show("bluetooth/USB is unplugged");
break;
case UsbNotification.DbtDevicearrival:
MessageBox.Show("bluetooth/USB is plugged");
break;
}
}
handled = false;
return IntPtr.Zero;
}