0

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;
        }
rosh_021
  • 23
  • 1
  • 9
  • You may want to reconsider. Both are serial ports but the data format are different and the driver handles some of the protocol. To make a common interface you may need to look at the raw data and then you are bypassing functionality that is being done in the driver. You question is too broad to be able to answer without knowing the details of the devices. – jdweng Sep 25 '19 at 14:21
  • Hi jdweg - Devices which i am using, can be any USB device like example a Scanner. Any Bluetooth devices which gets paired. Above code is working fine if i am using individual instance with different GUID. But want to club into one common functionality. Thanks in advance. – rosh_021 Sep 25 '19 at 15:13
  • You have to register notification for all USB interface and then filter dev class GUID in WM_DEVICECHANGE message handler. – Mike Petrichenko Sep 25 '19 at 16:01
  • It depends on the application layer protocol. In some cases it will work and other it will not work. – jdweng Sep 25 '19 at 16:03
  • @jdweng : Bluetooth device is not serial port device. As well as USB device is not serial port device (let say HID or AV). – Mike Petrichenko Sep 25 '19 at 16:04
  • @ jdweng: you are wrong again. Device type/class does not depend on app level protocol. – Mike Petrichenko Sep 25 '19 at 16:04
  • Hi @MikePetrichenko - Could you please provide some example code? As i am not able to get much info in WmDevicechange. – rosh_021 Sep 26 '19 at 12:45
  • Unfortunately it is very specific code that we use in our product. It needs a lot of changes before can be published as sample. We use DEV_BROADCAST_DEVICEINTERFACE flag in notification registration. And thenin message processing we use the dbcc_classguid amd dbcc_name fields to check device class. Using dbcc_name and dbcc_classguid with SetupAPI we can get all the information about device. Please note, that on Win 10 and above it is better to use CfgMgr API instead. – Mike Petrichenko Sep 27 '19 at 08:38

0 Answers0