C# 是否有Windows事件来确定蓝牙设备是否配对
我正在构建一个允许我与蓝牙设备配对的应用程序。 现在我正试图在C#中找到一个事件,它允许我检测设备何时正在添加或准备添加(请参见img:Windows 10弹出窗口)C# 是否有Windows事件来确定蓝牙设备是否配对,c#,bluetooth,C#,Bluetooth,我正在构建一个允许我与蓝牙设备配对的应用程序。 现在我正试图在C#中找到一个事件,它允许我检测设备何时正在添加或准备添加(请参见img:Windows 10弹出窗口) 有人知道我在找哪一个吗?我有点晚了,但这里有一些关于我在纯原始API(C++)中使用WM_DEVICECHANGE消息所做工作的片段 您需要截获WM_DEVICECHANGE消息(显然) 您首先需要“注册”正确的GUID: GUID_BTHPORT_DEVICE_INTERFACE{0850302A-B344-4fda-9BE
有人知道我在找哪一个吗?我有点晚了,但这里有一些关于我在纯原始API(C++)中使用WM_DEVICECHANGE消息所做工作的片段
- 您需要截获WM_DEVICECHANGE消息(显然)
- 您首先需要“注册”正确的GUID:
- GUID_BTHPORT_DEVICE_INTERFACE{0850302A-B344-4fda-9BE9-90576B8D46F0}截获有关无线电本身的事件
- GUID_BTH_设备_接口{00F40965-E89D-4487-9890-87C3ABB211F4}截获任何蓝牙设备和/或
- GUID\u蓝牙设备\u接口 {781aee18-7733-4ce4-add0-91f41c67b592}截获有关BLE的事件 设备
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_Device Interface and
- DBT_设备移动完成| DBT_设备接口
- GUID\u蓝牙\u无线电\u在\u范围内
- GUID\u蓝牙\u无线电\u超出\u范围
- GUID\u蓝牙\u L2CAP\u事件
- GUID\u蓝牙\u HCI\u事件
- GUID\u蓝牙\u HCI\u供应商\u事件
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);
}
}
[...]
#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
此时,您将能够通过处理WM_DEVICECHANGE处理程序的DBT_CUSTOMEVENT | DBT_DEVTYP_HANDLE移植来接收这些事件,如下所示
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);
}
}
[...]
#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
希望这有帮助 ,“定制配对”部分,这很有帮助。似乎我需要的东西在
windows.Devices.Enumeration
程序集中。根据应用程序类型,有几种方法。对于UWP,您必须使用设备枚举器来获得有关硬件更改的通知。自定义配对不允许处理出现在应用程序外部的配对。对于经典应用程序,最好使用本机Bluetooth API,该API允许处理您需要的所有事件。您也可以查看配置管理器。或者捕获WM_DEVICECHANGE消息。啊,是的,windows.Devices.Enumeration
只能在UWP中发挥作用,但在经典应用程序中我需要它@MikePetrichenko顺便说一句,配置管理器是什么意思?。或者最好使用经典的蓝牙API。好多了。或简单查询WMI。