简体   繁体   English

是否有 Windows 事件来查明蓝牙设备是否正在配对

[英]Is there a Windows event to find out if a bluetooth device is getting paired

I'm building an application that allows me to pair with a bluetooth device.我正在构建一个允许我与蓝牙设备配对的应用程序。 Right now I'm trying to find an event in C# that allows me to detect when a device is being added or ready to be added (see img: Windows 10 popup)现在我正试图在 C# 中找到一个事件,它允许我检测何时添加或准备添加设备(参见 img:Windows 10 弹出窗口)

在此处输入图像描述

Anyone know which one I'm looking for?有人知道我要找哪一个吗?

I'm a bit late, but here are a few snippets about what I'm doing in plain Raw API (in C++), using the WM_DEVICECHANGE messages. 我有点晚了,但是这里有一些关于我在普通Raw API(在C ++中)使用WM_DEVICECHANGE消息做的内容的片段。

  • you need to intercept WM_DEVICECHANGE messages (obviously) 你需要拦截WM_DEVICECHANGE消息(显然)
  • you first need to 'Register' the proper GUIDs: 你首先需要'注册'正确的GUID:
  • GUID_BTHPORT_DEVICE_INTERFACE {0850302A-B344-4fda-9BE9-90576B8D46F0} to intercept events about the Radio itself GUID_BTHPORT_DEVICE_INTERFACE {0850302A-B344-4fda-9BE9-90576B8D46F0}拦截有关收音机本身的事件
  • GUID_BTH_DEVICE_INTERFACE {00F40965-E89D-4487-9890-87C3ABB211F4} to intercept events about any Bluetooth devices and/or GUID_BTH_DEVICE_INTERFACE {00F40965-E89D-4487-9890-87C3ABB211F4}拦截任何蓝牙设备的事件和/或
  • GUID_BLUETOOTHLE_DEVICE_INTERFACE {781aee18-7733-4ce4-add0-91f41c67b592} to intercept events about BLE devices. GUID_BLUETOOTHLE_DEVICE_INTERFACE {781aee18-7733-4ce4-add0-91f41c67b592}拦截有关BLE设备的事件。

(Those are 'Interface GUIDs' in case you need them with the SetupAPIxx/CM_xx routines) (如果您需要使用SetupAPIxx / CM_xx例程,那么这些是'接口GUID')

I'm using the following code to 'register' them: 我正在使用以下代码来“注册”它们:

HDEVNOTIFY UDeviceInfoHandler::RegisterDeviceNotification(  HWND    hwnd,
                                                            GUID    InterfaceClassGuid,
                                                            DWORD   flags)
{
    DEV_BROADCAST_DEVICEINTERFACE DevFilter;
    ::ZeroMemory(&DevFilter, sizeof(DEV_BROADCAST_DEVICEINTERFACE) );
    DevFilter.dbcc_size = sizeof( DEV_BROADCAST_DEVICEINTERFACE );
    DevFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    DevFilter.dbcc_classguid = InterfaceClassGuid;
    return ::RegisterDeviceNotification(hwnd,       //events recipient
                                        &DevFilter, //type of device
                                        flags);     //type of recipient handle
}

bool UDeviceInfoHandler::UnregisterDeviceNotification(HDEVNOTIFY hdevnotify)
{
    return TRUE==::UnregisterDeviceNotification(hdevnotify);
}

At this point, you'll be able 此时,你将能够

  • to see 'Radio' appearing/disappearing (ie by playing with the 'Activate Bluetooth' checkbow in Settings ) 看到“收音机”出现/消失(即通过在“设置”中使用“激活蓝牙”检查弓)
  • to see the BT2.x virtual COM ports appearing/disappearing 看到BT2.x虚拟COM端口出现/消失
  • to see the BLE devices appear/disappear , PROVIDED they are already 'paired' (that is, in brief, added to the Registry ) 看到BLE设备出现/消失,提供它们已经“配对”(简而言之,已添加到注册表中)

by handling the 通过处理

  • DBT_DEVICEARRIVAL|DBT_DEVTYP_DEVICEINTERFACE and DBT_DEVICEARRIVAL | DBT_DEVTYP_DEVICEINTERFACE和
  • DBT_DEVICEREMOVECOMPLETE|DBT_DEVTYP_DEVICEINTERFACE DBT_DEVICEREMOVECOMPLETE | DBT_DEVTYP_DEVICEINTERFACE

messages in your WM_DEVICECHANGE handler. WM_DEVICECHANGE处理程序中的消息。

If you need to access the DBT_CUSTOMEVENT messages pertaining to the Radio/Devices, you'll first need to 'Register' some 'events' for WM_DEVICECHANGE too, but for the 'HANDLE' of the Radio. 如果您需要访问与无线电/设备有​​关的DBT_CUSTOMEVENT消息,您首先需要为WM_DEVICECHANGE'注册'一些'事件',但是对于无线电的'HANDLE'。 You can register the GUIDs of the following events 您可以注册以下事件的GUID

  • GUID_BLUETOOTH_RADIO_IN_RANGE GUID_BLUETOOTH_RADIO_IN_RANGE
  • GUID_BLUETOOTH_RADIO_OUT_OF_RANGE GUID_BLUETOOTH_RADIO_OUT_OF_RANGE
  • GUID_BLUETOOTH_L2CAP_EVENT GUID_BLUETOOTH_L2CAP_EVENT
  • GUID_BLUETOOTH_HCI_EVENT GUID_BLUETOOTH_HCI_EVENT
  • GUID_BLUETOOTH_HCI_VENDOR_EVENT GUID_BLUETOOTH_HCI_VENDOR_EVENT

Use something like 使用类似的东西

void UBthDeviceInfoHandler::RegisterBthNotifications(HWND hwnd,const UGuidItems& guids)
{
    BLUETOOTH_FIND_RADIO_PARAMS radio_params;
    radio_params.dwSize = sizeof(BLUETOOTH_FIND_RADIO_PARAMS);

    HANDLE hRadio;
    HBLUETOOTH_RADIO_FIND hFind = BluetoothFindFirstRadio(&radio_params, &hRadio);
    if (hFind != INVALID_HANDLE_VALUE) 
    {
        do 
        {
            //for every events Guid you need
            HDEVNOTIFY hdevnotify=
                RegisterHandleNotification( hwnd,
                                            hRadio,
                                            Guid,
                                            DEVICE_NOTIFY_WINDOW_HANDLE);
            if (hdevnotify!=NULL){
                  //insert code here
            }
            else{
                  //error handling
            }
            //end for
        } while (BluetoothFindNextRadio(hFind, &hRadio));

        BluetoothFindRadioClose(hFind);
    }
}

At this point, you'll be able to receive those events by handling the DBT_CUSTOMEVENT|DBT_DEVTYP_HANDLE porting of the WM_DEVICECHANGE handler, with something like 此时,您将能够通过处理WM_DEVICECHANGE处理程序的DBT_CUSTOMEVENT | DBT_DEVTYP_HANDLE移植来接收这些事件,类似于

    [...]
    #if (WINVER >= 0x040A)
            case DBT_CUSTOMEVENT:
            //@see https://msdn.microsoft.com/en-us/library/aa363217(v=vs.85).aspx
                if (lParam!=0){
                    PDEV_BROADCAST_HDR phdr = reinterpret_cast<PDEV_BROADCAST_HDR> (lParam);
                    switch (phdr->dbch_devicetype){
                        case DBT_DEVTYP_HANDLE:
                            {
            //@see https://docs.microsoft.com/en-us/windows/desktop/bluetooth/bluetooth-and-wm-devicechange-messages

                                //typedef struct _DEV_BROADCAST_HANDLE {
                                //  DWORD       dbch_size;
                                //  DWORD       dbch_devicetype;
                                //  DWORD       dbch_reserved;
                                //  HANDLE      dbch_handle;     // file handle used in call to RegisterDeviceNotification
                                //  HDEVNOTIFY  dbch_hdevnotify; // returned from RegisterDeviceNotification
                                //  //
                                //  // The following 3 fields are only valid if wParam is DBT_CUSTOMEVENT.
                                //  //
                                //  GUID        dbch_eventguid;
                                //  LONG        dbch_nameoffset; // offset (bytes) of variable-length string buffer (-1 if none)
                                //  BYTE        dbch_data[1];    // variable-sized buffer, potentially containing binary and/or text data
                                //} DEV_BROADCAST_HANDLE, *PDEV_BROADCAST_HANDLE;

                                PDEV_BROADCAST_HANDLE phndl=reinterpret_cast<PDEV_BROADCAST_HANDLE>(phdr);
                                CustomHandleEvent(*phndl);

                            }
                            break;


                  default:
                        break;
                }
            }//endif lParam!=0
            break;
    #endif // WINVER >= 0x040A

the |dbch_eventguid| | dbch_eventguid | field being one of those GUID_BLUETOOTH_RADIO_IN_RANGE etc. events. 字段是GUID_BLUETOOTH_RADIO_IN_RANGE等事件之一。

Well, this is only an overview of what I've discovered so far. 嗯,这只是我迄今为止发现的概述。 Any enhancements/suggestions/additions are more than welcome though. 任何增强/建议/添加都是非常受欢迎的。 I'm currently struggling with a few undocumented CUSTOM_EVENTS, whose GUIDs are 我目前正在努力处理一些未经证实的CUSTOM_EVENTS,其GUID是

//When the Bluetooth radio handle is opened, call the RegisterDeviceNotification function and
//register for notifications on the handle using DBT_DEVTYP_HANDLE as the devicetype.
//When registered, the following GUIDs are sent, 
//and the DEV_BROADCAST_HANDLE::dbch_data member is the associated buffer.

//this unknow event happens while looking for nearby devices in Settings.
DEFINE_GUID(GUID_UNKNOWN_EVENT_GUID1,0x1BBD4010, 0x498C, 0x4E85, 0x85, 0x1B, 0xEA, 0xA0, 0x57, 0x15, 0xC3, 0x7A);

//
DEFINE_GUID(GUID_UNKNOWN_EVENT_GUID2,0xD4EB6503, 0xC001, 0x441A, 0xAE, 0x42, 0xEE, 0x0D, 0xC9, 0x6C, 0x18, 0x85);

//happening when opening Settings|Bluetooth panel
DEFINE_GUID(GUID_UNKNOWN_EVENT_GUID3,0x7A7637FF, 0x531C, 0x4205, 0x97, 0x80, 0x3F, 0x33, 0x5F, 0x65, 0xAD, 0xDD);
//nameoffset=-1 datalen=8

//Settings|Devices -> Bluetooth activation/deactivation
DEFINE_GUID(GUID_UNKNOWN_EVENT_GUID4,0xB74983CD, 0xC2D9, 0x4E38, 0xB8, 0x0E, 0x54, 0x72, 0xFC, 0x10, 0x8B, 0x4B);
//nameoffset=-1 datalen=8

Hope this helps! 希望这可以帮助!

You can use a device watcher.您可以使用设备观察器。

Initialize with:初始化:

var deviceWatcher = DeviceInformation.CreateWatcher(BluetoothDevice.GetDeviceSelector());
deviceWatcher.Added += DeviceWatcher_AddedAsync;
deviceWatcher.Removed += DeviceWatcher_RemovedAsync;

Then you get events on:然后你得到事件:

private void DeviceWatcher_AddedAsync(DeviceWatcher sender, DeviceInformation args)
{
    ...
}

private void DeviceWatcher_RemovedAsync(DeviceWatcher sender, DeviceInformationUpdate args)
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM