Не удается обнаружить устройства BluetoothLE в консольном приложении Win32 C ++ на Windows 10 - PullRequest
0 голосов
/ 08 января 2020

Я пытаюсь работать с устройствами Bluetooth LE, используя Windows GATT API в Windows 10. Но я никогда не обнаруживаю никаких устройств. Запуск этого кода всегда возвращает ERROR_NO_MORE_ITEMS. Я пробовал с Windows ноутбуком со встроенным Bluetooth 4.0, а также с USB-ключом Bluetooth. Я вижу Bluetooth LE Enumerator в Диспетчере устройств, и драйвер показывает, что он установлен и работает.

Примечание. Я пытаюсь создать .dll, поэтому не хочу использовать UWP.

#include <vector>
#include <cstdio>
#include <iostream>
#include <windows.h>
#include <SetupAPI.h>
#include <Bluetoothleapis.h>



bool enumDevices() {

    HDEVINFO handle = INVALID_HANDLE_VALUE;
    GUID BluetoothClassGUID = GUID_BLUETOOTHLE_DEVICE_INTERFACE;

    handle = SetupDiGetClassDevs(
        &BluetoothClassGUID,
        NULL,
        NULL,
        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

    if (handle == INVALID_HANDLE_VALUE) {
        std::wcout << "  No Device class" << std::endl;
        return FALSE;
    }

    GUID BluetoothInterfaceGUID = GUID_BLUETOOTHLE_DEVICE_INTERFACE;

    std::vector<SP_DEVICE_INTERFACE_DATA> ble_interfaces;
    std::vector<std::wstring> ble_paths;

    // Enumerate device of LE_DEVICE interface class
    for (int i = 0;; i++) {
        std::string error;
        SP_DEVICE_INTERFACE_DATA device_interface_data = { 0 };
        device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
        BOOL success = SetupDiEnumDeviceInterfaces(
            handle,
            NULL,
            (LPGUID)&BluetoothInterfaceGUID,
            (DWORD)i,
            &device_interface_data);

        if (!success) {
            DWORD last_error = GetLastError();
            if (last_error == ERROR_NO_MORE_ITEMS) {
                std::wcout << "  No more devices found" << std::endl;
                break;
            }
            else {
                std::wcout << "  Error enumerating device interfaces: " << last_error;
                return FALSE;
            }
        }

        // Retrieve required # of bytes for interface details
        ULONG required_length = 0;
        success = SetupDiGetDeviceInterfaceDetail(
            handle,
            &device_interface_data,
            NULL,
            0,
            &required_length,
            NULL);

        if (!success) {
            DWORD last_error = GetLastError();
            return FALSE;

        }

        UINT8* interface_data = new UINT8[required_length];
        memset(interface_data, 0, required_length);

        PSP_DEVICE_INTERFACE_DETAIL_DATA device_interface_detail_data = reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(interface_data);
        device_interface_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

        SP_DEVINFO_DATA device_info_data = { 0 };
        device_info_data.cbSize = sizeof(SP_DEVINFO_DATA);

        ULONG actual_length = required_length;
        success = SetupDiGetDeviceInterfaceDetail(
            handle,
            &device_interface_data,
            device_interface_detail_data,
            actual_length,
            &required_length,
            &device_info_data);

        //Store data
        std::wstring strpath = std::wstring(device_interface_detail_data->DevicePath);
        OLECHAR* bstrGuid;
        StringFromCLSID(device_interface_data.InterfaceClassGuid, &bstrGuid);
        std::wcout << "  Path: " << strpath << " GUID:" << bstrGuid << std::endl;
        ble_paths.push_back(strpath);
        ble_interfaces.push_back(device_interface_data);
    }
}


int main() {
    enumDevices();
}
...