I'm trying to discover if a Bluetooth Low Energy (BLE) device, that has been paired previously, is currently enabled using setupAPI in a C program with minGW compiler. At the moment I'm trying to use setupAPI SetupDiGetDevicePropertyW() function for property DEVPKEY_Device_IsPresent but the result is always true even if the BLE device is currently switched off. The property DEVPKEY_DeviceContainer_IsConnected produces an error code 1168. Does anyone have done something similar already?
The relevant console output for my device:
SPDRP_FRIENDLYNAME: Testdevice
The device is present.
Failed to get connn property: 1168
Failed to get paired property: 1168
The output has been generated by the following test c program.
#include <Windows.h>
#include <Setupapi.h>
#include <initguid.h>
#include <devpkey.h>
#include <devguid.h>
#include <bthdef.h>
#include <stdio.h>
int main()
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
// Create a device information set of Bluetooth devices
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_BLUETOOTH, 0, 0, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
printf("Error getting device information set: %lu\n", GetLastError());
return 1;
}
devInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
// Enumerate through devices in the set
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &devInfoData); i++)
{
DEVPROPTYPE dataType;
DWORD requiredSize;
SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME, &dataType, NULL, 0, &requiredSize);
LPTSTR deviceDescription = (LPTSTR)malloc(requiredSize);
if (SetupDiGetDeviceRegistryProperty(hDevInfo, &devInfoData, SPDRP_FRIENDLYNAME, &dataType, (PBYTE)deviceDescription, requiredSize, NULL))
{
// Print device description
printf(("SPDRP_FRIENDLYNAME: %s\n"), deviceDescription);
}
else
{
printf(("Error SPDRP_FRIENDLYNAME information. Error %d\n"), GetLastError());
}
free(deviceDescription);
UCHAR present;
if(SetupDiGetDevicePropertyW(hDevInfo, &devInfoData, &DEVPKEY_Device_IsPresent, &dataType, (PBYTE)&present, sizeof(present), NULL, 0))
{
if (dataType == DEVPROP_TYPE_BOOLEAN && present) {wprintf(L"The device is present.\n");} else {wprintf(L"The device is not present.\n");}
}
else
{
printf("Failed to get present property: %lu\n", GetLastError());
}
UCHAR connn;
if(SetupDiGetDevicePropertyW(hDevInfo, &devInfoData, &DEVPKEY_DeviceContainer_IsConnected, &dataType, (PBYTE)&connn, sizeof(connn), NULL, 0))
{
if (dataType == DEVPROP_TYPE_BOOLEAN && connn) {wprintf(L"The device is connn.\n");} else {wprintf(L"The device is not connn.\n");}
}
else
{
printf("Failed to get connn property: %lu\n", GetLastError());
}
UCHAR paired;
if(SetupDiGetDevicePropertyW(hDevInfo, &devInfoData, &DEVPKEY_DeviceContainer_IsPaired, &dataType, (PBYTE)&paired, sizeof(paired), NULL, 0))
{
if (dataType == DEVPROP_TYPE_BOOLEAN && paired) {wprintf(L"The device is paired.\n");} else {wprintf(L"The device is not paired.\n");}
}
else
{
printf("Failed to get paired property: %lu\n", GetLastError());
}
}
// Clean up
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}