我正在为Win 8平板编写程序。我需要连接一个外部BLE设备。
该设备已经与Windows配对,我可以在设备管理器中看到它。但我不知道如何连接它。
使用SetupDiEnumDeviceInfo和SetupDiGetDeviceProperty,我可以获取有关BLE设备的一些信息,但要执行例如BluetoothGATTGetServices 需要句柄设备。我不知道从哪里获取它。也许我可以使用CreateFile,但不清楚将什么替代作为第一个参数lpFileName。
这是一段代码,我正在寻找我的设备。
使用SetupDiEnumDeviceInfo和SetupDiGetDeviceProperty,我可以获取有关BLE设备的一些信息,但要执行例如BluetoothGATTGetServices 需要句柄设备。我不知道从哪里获取它。也许我可以使用CreateFile,但不清楚将什么替代作为第一个参数lpFileName。
这是一段代码,我正在寻找我的设备。
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(
&BluetoothClassGUID, /* GUID_DEVCLASS_BLUETOOTH */
0, 0, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return ;//1;
}
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_FRIENDLYNAME,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER){
// Change the buffer size.
if (buffer) delete(buffer);
// Double the size to avoid problems on
// W2k MBCS systems per KB 888609.
buffer = new wchar_t[buffersize * 2];
}else{
// Insert error handling here.
break;
}
}
/* Here i just compare by name is this my device or not */
...
/* Here i just compare by name is this my device or not */
if (buffer) delete(buffer);
}
if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )
{
// Insert error handling here.
return; //1;
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return;// 0;
我已经移动了一点,但仍然无法从设备获取数据。
为了获取“设备接口路径”,必须使用其他函数:SetupDiGetClassDevs、SetupDiEnumDeviceInterfaces和SetupDiGetDeviceInterfaceDetail。
接下来,使用CreateFile,我获得了BLE设备的HANDLE。
hComm = CreateFile(pInterfaceDetailData->DevicePath, GENERIC_WRITE | GENERIC_READ,NULL,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
接下来,使用WinAPI的BluetoothGATTGetServices和BluetoothGATTGetCharacteristics,我获得了适当的结构。
但是,当试图使用BluetoothGATTGetCharacteristicsValue获取属性值时,我得到了ERROR_ACCESS_DENIED。
然后我不知道该怎么办。可能出了什么问题?