c++ - Windows 8.1 蓝牙 LE 无法获取设备接口(interface)

标签 c++ visual-c++ bluetooth windows-8.1 bluetooth-lowenergy

我正在尝试检索 Windows 8.1 机器上所有配对蓝牙设备的名称和句柄。

我可以获取名称,但 SetupDiEnumDeviceInterfaces 始终返回 false。我在某处读到我需要在 SetupDIGetClassDevs 函数中包含 DIGCF_DEVICEINTERFACE,但它仍然不起作用。

这是我的代码:

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
    }
}

接下来,我在另一个函数中获取句柄,因为您需要在接口(interface)上枚举,而不是在 for 循环中使用 SetupDiEnumDeviceInterfaces 而不是 SetupDiEnumDeviceInfo 来枚举信息 使用蓝牙地址我将两者匹配并适当存储

关于c++ - Windows 8.1 蓝牙 LE 无法获取设备接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25434320/

相关文章:

c++ - 使用 tensorflow 的 C++ API 构建错误

c++ - 在模板实例化期间重载查找

android - 三星S7没有;发现任何蓝牙设备

android - 在 Android java 中,如何长按蓝牙设备调用按钮?

iOS 蓝牙服务建议

c++ - 什么是类型字符串(模板元编程),它有什么作用?

c++ - 如何链接GLX?

c++ - C/C++ 为什么对二进制数据使用 unsigned char?

c++ - 如何将文件传递给函数?

c - 为什么int转char没有溢出警告