C++ Windows 8.1蓝牙LE can';无法获取设备接口
我正在尝试检索windows 8.1计算机上所有配对蓝牙设备的名称和句柄 我可以获取名称,但SetupDienumDeviceInterface始终返回false。我在某个地方读到,我需要在SetupDIGetClassDevs函数中包含DIGCF_DEVICEINTERFACE,但它仍然不起作用 这是我的密码:C++ Windows 8.1蓝牙LE can';无法获取设备接口,c++,visual-c++,bluetooth,windows-8.1,bluetooth-lowenergy,C++,Visual C++,Bluetooth,Windows 8.1,Bluetooth Lowenergy,我正在尝试检索windows 8.1计算机上所有配对蓝牙设备的名称和句柄 我可以获取名称,但SetupDienumDeviceInterface始终返回false。我在某个地方读到,我需要在SetupDIGetClassDevs函数中包含DIGCF_DEVICEINTERFACE,但它仍然不起作用 这是我的密码: HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i; // Create a HDEVINFO with all p
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(
&GUID_DEVCLASS_BLUETOOTH,
0, 0, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return;//1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i,
&DeviceInfoData); i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER){
// Change the buffer size.
if (buffer) delete(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = new wchar_t[buffersize * 2];
}
else{
// Insert error handling here.
break;
}
}
HWND deviceList = GetDlgItem(GetActiveWindow(), LIST_BOX);
if (deviceList && buffersize > 0)
{
SendMessage(deviceList, LB_ADDSTRING, 0, (LPARAM)buffer);
}
if (buffer) delete(buffer);
// WORKS UNTIL HERE BUT ENUMERATING THROUGH INTERFACES ALWAYS RETURNS FALSE
SP_DEVICE_INTERFACE_DATA devIntData;
HDEVINFO hDevInfo2 = SetupDiGetClassDevs(
&GUID_DEVCLASS_BLUETOOTH,
0, 0, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (SetupDiEnumDeviceInterfaces(hDevInfo2,
&DeviceInfoData,
&GUID_BLUETOOTHLE_DEVICE_INTERFACE,
i,
&devIntData))
{
DWORD reqSize;
SP_DEVINFO_DATA buffer;
while (SetupDiGetDeviceInterfaceDetail(hDevInfo2,
&devIntData,
NULL,
NULL,
&reqSize,
&buffer))
{
OutputDebugString(L"DeviceINTERFACE");
}
}
}
我已尝试将设备枚举置于名称枚举循环之外,但它仍然返回false。我还希望句柄和名称相关联,以便在相同的上下文中找到它们
如果任何人在windows 8.1中有关于完整蓝牙LE工作流的任何示例代码(查找名称、查找句柄、查找服务、查找特征、写入特征),并能与我分享,我将不胜感激。谢谢。解决了这个问题,没有为我的缓冲区正确分配内存 编辑:添加代码
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(
&GUID_DEVCLASS_BLUETOOTH,
0, 0, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return;//1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i,
&DeviceInfoData); i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
//This loop gets the name with SPDRP_FRIENDLYNAME
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER){
// Change the buffer size.
if (buffer) delete(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = new wchar_t[buffersize * 2];
}
else{
// Insert error handling here.
break;
}
}
DWORD DataT2;
LPTSTR buffer2 = NULL;
DWORD buffersize2 = 0;
//This Loop gets the Bluetooth Address with SPDRP_HARDWAREID
// NOTE: there is more information than just the address you will have
// to do some string manipulation to have just the address
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_HARDWAREID,
&DataT2,
(PBYTE)buffer2,
buffersize2,
&buffersize2))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER){
// Change the buffer size.
if (buffer2) delete(buffer2);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer2 = new wchar_t[buffersize2 * 2];
}
else{
// Insert error handling here.
break;
}
}
if (buffersize > 0)
{
//do what you need with the info
//name is in buffer
//address is in buffer2
}
}
接下来,我在另一个函数中获取句柄,因为您需要枚举接口,而不是使用for循环中的setupDienumDeviceInterface(而不是SetupDiEnumDeviceInfo)枚举信息
使用蓝牙地址,我将两者匹配并适当存储是否有办法共享您的解决方案?我在查找设备时遇到问题name@JakubWisniewski添加了我的代码,为我解决了这个问题。。。希望这有助于。。。当前的MSDN BLE api非常麻烦您能否使用SetupDiEnumDeviceInterface共享您的解决方案?应该与此相同,但您应该使用SetupDiEnumDeviceInterface函数,而不是使用SetupDiEnumDeviceInfo函数,并使用不同的注册表属性。我再也没有这个项目了抱歉。