如何从蓝牙LE设备获得服务和特性

时间:2015-01-11 18:43:33

标签: c++ windows visual-c++ bluetooth

我在Windows 8.1中编写桌面应用程序,我需要使用BluetoothGATTGetServices功能从配对设备获取服务。要做到这一点,我需要处理设备,我已经完成并使用CreateFile函数处理。但是当我尝试获取服务函数返回错误:HRESULT_FROM_WIN32(ERROR_NOT_SUPORTED)。有谁知道如何解决它或告诉我做错了什么。 我试图获得服务的设备是iPhone 5s。

以下是代码示例:

 HRESULT WinBluetoothDeviceProvider::GetBleHandle(OUT HANDLE* hBluetooth, WinBluetoothDevice* blDev)
{
    GUID bthGuid;
    CLSIDFromString(TEXT(BLE_INTERFACE_GUID), &bthGuid); // BLE_INTERFACE_GUID  this is GUID of my Device Interface GUID
    //#define BLE_INTERFACE_GUID "{00f40965-e89d-4487-9890-87c3abb211f4}"

    HRESULT result = S_OK;
    HDEVINFO hDevInfo;
    SP_DEVINFO_DATA devInfoData;
    HANDLE hBle;

    hDevInfo = SetupDiGetClassDevs(&bthGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
    if (hDevInfo == INVALID_HANDLE_VALUE)
        return E_FAIL;

    devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    SP_DEVICE_INTERFACE_DATA devInterfData = { 0 };
    devInterfData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    DWORD DataT;
    LPTSTR buffer = NULL;
    DWORD buffersize = 0;


    devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &bthGuid, i, &devInterfData); i++)
    {
        DWORD d = GetLastError();
        DWORD size = 0;

        if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInterfData, NULL, 0, &size, 0))
        {
            if (GetLastError() == ERROR_NO_MORE_ITEMS)
            {
                result = S_OK;
                break;
            }

            PSP_DEVICE_INTERFACE_DETAIL_DATA pInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, size);
            pInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

            if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInterfData, pInterfaceDetailData, size, &size, &devInfoData))
            {
                result = E_FAIL;
                break;
            }

            hBle = CreateFile(pInterfaceDetailData->DevicePath, 
                GENERIC_READ | GENERIC_WRITE,
                FILE_SHARE_READ | FILE_SHARE_WRITE,
                NULL,
                OPEN_EXISTING,
                FILE_ATTRIBUTE_NORMAL,
                NULL);

            USHORT serviceBuffActual;
            PBTH_LE_GATT_SERVICE servicesBuff = new _BTH_LE_GATT_SERVICE;
            HRESULT result = S_OK;

            result = BluetoothGATTGetServices(hBle,
                0, NULL, &serviceBuffActual, BLUETOOTH_GATT_FLAG_NONE);
            // this function always retunr ERROR_NOT_SUPPORTED

            LocalFree(pInterfaceDetailData);
        }
    }
    SetupDiDestroyDeviceInfoList(hDevInfo);

    return result;
}

0 个答案:

没有答案