BluetoothGattgetCharacterics thf toff Access Access Access A

BluetoothGATTGetCharacteristics throwing Access violation reading location 0xFFFFFFF7 in BluetoothApis.dll

本文关键字:Access toff thf BluetoothGattgetCharacterics      更新时间:2023-10-16

我正在尝试编写Win32控制台应用程序,该应用程序将连接iPhone蓝牙以进行某些BLE服务。如果我在这里犯了任何错误,请纠正我 -

在 -

上获取访问违规错误
hr = BluetoothGATTGetCharacteristics(
    hLEDevice,
    pServiceBuffer,
    0,
    NULL,
    &charBufferSize,
    BLUETOOTH_GATT_FLAG_NONE);

原因是,我无法通过- SetupDiEnumDeviceInterfaces进行此设备classid。

更多详细信息的主要功能 -

#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 memoryrn");
}
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 memoryrn");
    }
    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 mismatchrn");
    }
}

获取错误

在0x0f5ce138(bluetoothapis.dll)中未经处理的异常 consoleapplication.exe:0xc0000005:访问侵犯地点访问位置 0xffffff7

任何人都可以建议是什么原因?

我尝试使用C#桌面应用程序,但没有运气在" await GattDeviceService.FromIdAsync(device.Id)"行中获得FileNotFound错误

问题是旧的,但仍然没有答案。这是由于GetBLEHandle函数返回null。AGuid是BLE设备的GUID(在Microsoft样品中,它是HRM-心率监视器),您没有此GUID连接的设备。因此,GetBLEHandleNULLDEVICE_NOT_FOUND。如果您使用NULL DeviceHandle致电BluetoothGATTGetServices,您会收到此类例外。