首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有Windows事件来确定蓝牙设备是否成对

是否有Windows事件来确定蓝牙设备是否成对
EN

Stack Overflow用户
提问于 2018-10-24 11:59:05
回答 1查看 2.1K关注 0票数 4

我正在构建一个应用程序,允许我与蓝牙设备配对。现在,我正试图在C#中找到一个事件,该事件允许我检测设备何时被添加或准备添加(参见img: Windows 10弹出窗口)。

有人知道我在找哪一个吗?

EN

回答 1

Stack Overflow用户

发布于 2019-04-03 06:49:09

我有点晚了,但是这里有一些关于我使用普通的原始API (在C++中)使用WM_DEVICECHANGE消息所做的事情的片段。

  • 您需要拦截WM_DEVICECHANGE消息(很明显)
  • 您首先需要“注册”正确的GUID:
  • GUID_BTHPORT_DEVICE_INTERFACE {0850302A-B344-4fda-9BE9-90576B8D46F0}拦截有关无线电本身的事件
  • GUID_BTH_DEVICE_INTERFACE {00F40965-E89D-4487-9890-87C3ABB211F4}拦截任何蓝牙设备和/或
  • GUID_BLUETOOTHLE_DEVICE_INTERFACE {781aee18-7733-4ce4-add0-91f41c67b592}来拦截有关BLE设备的事件。

(如果您需要SetupAPIxx/CM_xx例程,这些是“Interface GUID”)

我使用以下代码来“注册”它们:

代码语言:javascript
运行
复制
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);
}

在这一点上,你可以

  • 看到“收音机”出现/消失(即在设置中使用“激活蓝牙”校验弓)
  • 查看BT2.x虚拟COM端口的出现/消失
  • 查看BLE设备的出现/消失,条件是它们已经“配对”(简而言之,添加到注册表中)

通过处理

  • DBT_DEVICEARRIVAL|DBT_DEVTYP_DEVICEINTERFACE和
  • DBT_DEVICEREMOVECOMPLETE|DBT_DEVTYP_DEVICEINTERFACE

WM_DEVICECHANGE处理程序中的消息。

如果您需要访问与收音机/设备相关的DBT_CUSTOMEVENT消息,那么首先需要为WM_DEVICECHANGE“注册”一些“事件”,但是要“注册”收音机的“句柄”。可以注册下列事件的GUID

  • GUID_BLUETOOTH_RADIO_IN_RANGE
  • GUID_BLUETOOTH_RADIO_OUT_OF_RANGE
  • GUID_BLUETOOTH_L2CAP_EVENT
  • GUID_BLUETOOTH_HCI_EVENT
  • GUID_BLUETOOTH_HCI_VENDOR_EVENT

使用类似的东西

代码语言:javascript
运行
复制
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);
    }
}

此时,您将能够通过处理DBT_CUSTOMEVENT|DBT_DEVTYP_HANDLE处理程序的WM_DEVICECHANGE移植来接收这些事件,如下所示

代码语言:javascript
运行
复制
    [...]
    #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://learn.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

|dbch_eventguid|字段是GUID_BLUETOOTH_RADIO_IN_RANGE等事件之一。

嗯,这只是我到目前为止所发现的东西的概述。但是,任何增强/建议/添加都非常受欢迎。我目前正在与几个无文档的CUSTOM_EVENTS进行斗争,它的GUID是

代码语言:javascript
运行
复制
//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

希望这能有所帮助!

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52968462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档