Как правило, мое приложение должно распознавать устройства USB и Bluetooth одновременно.В приведенном ниже коде я столкнулся с проблемой использования USB и Bluetooth GUID одновременно в статическом классе.Для этого статического класса требуется GUID, чтобы событие могло распознавать устройства USB и Bluetooth.Можно ли сделать универсальный класс для объединения обоих в один статический класс?Или есть какой-либо общий GUID, который можно использовать как для USB, так и для Bluetooth.c # Код ниже:
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;
}