c # - BluetoothGATTGetCharacteristics kaster Access-læsningsstedet 0xFFFFFFF7 i BluetoothApis.dll

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forsøger at skrive en Win32-konsol program, der vil forbinde iPhone Bluetooth til nogle BLE-tjenester. Ret mig venligst, hvis jeg gør en fejl her-


Få adgang til adgangsfejl ved -


hr = BluetoothGATTGetCharacteristics(
    hLEDevice,
    pServiceBuffer,
    0,
    NULL,
    &charBufferSize,
    BLUETOOTH\_GATT\_FLAG\_NONE);


Årsagen er, at jeg ikke kan løbe igennem - SetupDiEnumDeviceInterfaces for denne enhed classId.


Hovedfunktion til mere detaljerede


#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#include <bthdef.h>
#include <Bluetoothleapis.h>
#pragma comment(lib, "SetupAPI")
#pragma comment(lib, "BluetoothApis.lib")

HANDLE GetBLEHandle(\_\_in GUID AGuid)
{
    HDEVINFO hDI;
    SP\_DEVICE\_INTERFACE\_DATA did;
    SP\_DEVINFO\_DATA dd;
    GUID BluetoothInterfaceGUID = AGuid;
    HANDLE hComm = NULL;

    hDI = SetupDiGetClassDevs(&BluetoothInterfaceGUID, NULL, NULL, DIGCF\_DEVICEINTERFACE | DIGCF\_PRESENT);

    if (hDI == INVALID\_HANDLE\_VALUE) return NULL;

    did.cbSize = sizeof(SP\_DEVICE\_INTERFACE\_DATA);
    dd.cbSize = sizeof(SP\_DEVINFO\_DATA);

    for (DWORD i = 0; SetupDiEnumDeviceInterfaces(hDI, NULL, &BluetoothInterfaceGUID, i, &did); i++)
    {
        SP\_DEVICE\_INTERFACE\_DETAIL\_DATA DeviceInterfaceDetailData;

        DeviceInterfaceDetailData.cbSize = sizeof(SP\_DEVICE\_INTERFACE\_DETAIL\_DATA);

        DWORD size = 0;

        if (!SetupDiGetDeviceInterfaceDetail(hDI, &did, NULL, 0, &size, 0))
        {
            int err = GetLastError();

            if (err == ERROR\_NO\_MORE\_ITEMS) break;

            PSP\_DEVICE\_INTERFACE\_DETAIL\_DATA pInterfaceDetailData = (PSP\_DEVICE\_INTERFACE\_DETAIL\_DATA)GlobalAlloc(GPTR, size);

            pInterfaceDetailData->cbSize = sizeof(SP\_DEVICE\_INTERFACE\_DETAIL\_DATA);

            if (!SetupDiGetDeviceInterfaceDetail(hDI, &did, pInterfaceDetailData, size, &size, &dd))
                break;

            hComm = CreateFile(
                pInterfaceDetailData->DevicePath,
                GENERIC\_WRITE | GENERIC\_READ,
                FILE\_SHARE\_READ | FILE\_SHARE\_WRITE,
                NULL,
                OPEN\_EXISTING,
                0,
                NULL);

            GlobalFree(pInterfaceDetailData);
        }
    }

    SetupDiDestroyDeviceInfoList(hDI);
    return hComm;
}


int main(int argc, char *argv[], char *envp[]){

GUID AGuid = GUID\_DEVCLASS\_BLUETOOTH;



//now get the handle 
HANDLE hLEDevice = GetBLEHandle(AGuid);


//Step 2: Get a list of services that the device advertises
// first send 0,NULL as the parameters to BluetoothGATTServices inorder to get the number of
// services in serviceBufferCount
USHORT serviceBufferCount;
////////////////////////////////////////////////////////////////////////////
// Determine Services Buffer Size
////////////////////////////////////////////////////////////////////////////

HRESULT hr = BluetoothGATTGetServices(
    hLEDevice,
    0,
    NULL,
    &serviceBufferCount,
    BLUETOOTH\_GATT\_FLAG\_NONE);

if (HRESULT\_FROM\_WIN32(ERROR\_MORE\_DATA) != hr) {
    printf("BluetoothGATTGetServices - Buffer Size \%d", hr);
}

PBTH\_LE\_GATT\_SERVICE pServiceBuffer = (PBTH\_LE\_GATT\_SERVICE)
    malloc(sizeof(BTH\_LE\_GATT\_SERVICE) * serviceBufferCount);

if (NULL == pServiceBuffer) {
    printf("pServiceBuffer out of memory
");
}
else {
    RtlZeroMemory(pServiceBuffer,
        sizeof(BTH\_LE\_GATT\_SERVICE) * serviceBufferCount);
}

////////////////////////////////////////////////////////////////////////////
// Retrieve Services
////////////////////////////////////////////////////////////////////////////

USHORT numServices;
hr = BluetoothGATTGetServices(
    hLEDevice,
    serviceBufferCount,
    pServiceBuffer,
    &numServices,
    BLUETOOTH\_GATT\_FLAG\_NONE);

if (S\_OK != hr) {
    printf("BluetoothGATTGetServices - Buffer Size \%d", hr);
}


//Step 3: now get the list of charactersitics. note how the pServiceBuffer is required from step 2
////////////////////////////////////////////////////////////////////////////
// Determine Characteristic Buffer Size
////////////////////////////////////////////////////////////////////////////

USHORT charBufferSize;
hr = BluetoothGATTGetCharacteristics(
    hLEDevice,
    pServiceBuffer,
    0,
    NULL,
    &charBufferSize,
    BLUETOOTH\_GATT\_FLAG\_NONE);

if (HRESULT\_FROM\_WIN32(ERROR\_MORE\_DATA) != hr) {
    printf("BluetoothGATTGetCharacteristics - Buffer Size \%d", hr);
}

PBTH\_LE\_GATT\_CHARACTERISTIC pCharBuffer;
if (charBufferSize > 0) {
    pCharBuffer = (PBTH\_LE\_GATT\_CHARACTERISTIC)
        malloc(charBufferSize * sizeof(BTH\_LE\_GATT\_CHARACTERISTIC));

    if (NULL == pCharBuffer) {
        printf("pCharBuffer out of memory
");
    }
    else {
        RtlZeroMemory(pCharBuffer,
            charBufferSize * sizeof(BTH\_LE\_GATT\_CHARACTERISTIC));
    }


    USHORT numChars;
    hr = BluetoothGATTGetCharacteristics(
        hLEDevice,
        pServiceBuffer,
        charBufferSize,
        pCharBuffer,
        &numChars,
        BLUETOOTH\_GATT\_FLAG\_NONE);

    if (S\_OK != hr) {
        printf("BluetoothGATTGetCharacteristics - Actual Data \%d", hr);
    }

    if (numChars != charBufferSize) {
        printf("buffer size and buffer size actual size mismatch
");
    }
}


Får fejl



  Uhåndteret undtagelse på 0x0F5CE138 (BluetoothApis.dll) i
  ConsoleApplication.exe: 0xC0000005: Få adgang til overtrædelseslæsningssted
  0xFFFFFFF7



Kan nogen foreslå, hvad der kunne være årsagen?


Jeg forsøgte med en C # desktop applikation, men ikke held derhen fik også filen FileNotFound på linjen 'await GattDeviceService.FromIdAsync(device.Id)'

Bedste reference