如何连接到蓝牙低功耗设备
How to connect to the bluetooth low energy device
我正在为 Win 8 平板电脑编写程序。我需要连接外部 BLE 设备。该设备已经与Windows配对,我可以在设备管理器中看到它。但是我不知道如何连接它。
使用SetupDiEnumDeviceInfo和SetupDiGetDeviceProperty,我可以获得有关BLE设备的一些信息,但要执行,例如蓝牙GATTGetServices手柄设备要求。我不知道把它带到哪里。也许我可以使用 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 andSetupDiGetDeviceInterfaceDetail.
接下来,使用CreateFile,我得到了HANDLE BLE设备。
hComm = CreateFile(pInterfaceDetailData->DevicePath, GENERIC_WRITE |GENERIC_READ,空,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,空);
接下来使用WinAPI蓝牙GATTGetServices和BluetoothGATTGetFeatures我得到了适当的结构。
但是当尝试使用蓝牙GATTGetFeaturesValue获取属性值时,我得到了ERROR_ACCESS_DENIED。
然后我不知道该怎么办。可能出了什么问题?
Andrey,我认为问题是您的设备未连接,蓝牙GATTGetFeaturesValue未触发连接。
尝试使用 Windows 工具手动连接设备。我有以程可以帮助我:取消配对设备,配对设备 ->它应该显示为已连接(它在我的情况下有效;) )
无论如何,如果这没有帮助,请尝试运行"以管理员身份",这在某些情况下会有所帮助。
祝你好运!!!
注意:非常想知道如何检索BTLE设备的设备路径以调用蓝牙GATTGetServices?
gattServiceGUID 是您的设备支持的任何长格式 BLE UUID。
"{00001803-0000-1000-8000-00805F9B34FB"} can be used to open a handle to the Link Loss service if supported by the device you are trying to open
HDEVINFO hDevInfo = SetupDiGetClassDevs(&gattServiceGUID, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo != INVALID_HANDLE_VALUE)
{
SP_DEVICE_INTERFACE_DATA interfaceData;
ZeroMemory(&interfaceData,sizeof(SP_DEVICE_INTERFACE_DATA));
interfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for (DWORD dwDeviceIndex = 0; SetupDiEnumDeviceInterfaces(hDevInfo, NULL, &gattServiceGUID, dwDeviceIndex, &interfaceData); dwDeviceIndex++)
{
dwDeviceCount++;
SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, NULL, 0, &dwBytesNeeded, NULL);
if (::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
pInterfaceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) new byte[dwBytesNeeded];
SP_DEVINFO_DATA spDeviceInfoData = { sizeof(SP_DEVINFO_DATA) };
ZeroMemory(pInterfaceDetail, sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA));
pInterfaceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
// grab the interface detail
if (SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, pInterfaceDetail, dwBytesNeeded, NULL, &spDeviceInfoData) == TRUE)
{
// request a handle to the GATT service path
m_hGattServiceHandle = CreateFile(pInterfaceDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (m_hGattServiceHandle != INVALID_HANDLE_VALUE)
{
now you can drill down the characteristics and descriptors with the m_hGattServiceHandle
}
}
}
}
}
- 当套接字连接断开时检测C/C++Unix
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 被解释为低级别const的const对象的地址
- Python中的for循环与C++有何不同
- 无法在windows上使用mingw将sqlite3与c连接
- 为什么当我解模块化时,这个C++代代码"效率较低"?
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 低功耗 ESP32 + 具有深度睡眠模式的 LoRa (TTGO LoRa32 OLED 板 V1.0).
- QT中的TCP连接性能低问题
- 你能帮我修复有关蓝牙低功耗(C++)的错误吗?
- 在低功耗多核微控制器中使用全局变量不好吗?(C++)
- 如何连接到蓝牙低功耗设备
- 蓝牙低功耗低速率在Windows上
- 蓝牙低功耗Java Api或C/ c++库