gpt4 book ai didi

c# - BluetoothGATTGetCharacteristics 在 BluetoothApis.dll 中抛出访问冲突读取位置 0xFFFFFFF7

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:23 26 4
gpt4 key购买 nike

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

获取访问冲突错误 -

hr = BluetoothGATTGetCharacteristics(
hLEDevice,
pServiceBuffer,
0,
NULL,
&charBufferSize,
BLUETOOTH_GATT_FLAG_NONE);

原因是,我无法通过此设备类 ID 的 SetupDiEnumDeviceInterfaces 循环。

更多细节的主要功能-

#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\r\n");
}
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\r\n");
}
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\r\n");
}
}

获取错误

Unhandled exception at 0x0F5CE138 (BluetoothApis.dll) in ConsoleApplication.exe: 0xC0000005: Access violation reading location 0xFFFFFFF7

任何人都可以提出可能是什么原因吗?

我尝试使用 C# 桌面应用程序,但运气不好,在“await GattDeviceService.FromIdAsync(device.Id)”行也出现 FileNotFound 错误

最佳答案

这个问题很老了,但是还是没有答案。这是由于 GetBLEHandle 函数返回 NULL。 AGuid 是 BLE 设备的 GUID(在 Microsoft 示例中它是 HRM - 心率监测器),您没有连接此 GUID 的设备。因此 GetBLEHandleNULLDEVICE_NOT_FOUND。如果您使用 NULL deviceHandle 调用 BluetoothGATTGetServices,您将收到此类异常。

关于c# - BluetoothGATTGetCharacteristics 在 BluetoothApis.dll 中抛出访问冲突读取位置 0xFFFFFFF7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com