jeudi 23 juin 2016

Getting Connection-State from Bluetooth Low Energy Device from Device Manager


i'm developing on a Bluetooth Low Energy Device and i need to see in code if the device is connected or not. First thing i noticed was that there is in the Devicemanager a Attribute "Verbunden"-> English: Connected and it says true or false if my device is connected or not. So i need to read that Attribute in my program. What i have tried till now: Getting all Devices with SetupDiGetClassDevs Getting the FriendlyName with SetupDiGetDeviceRegistryProperty Searching for my Device with the name. That works. Now i wanted to get that Connected-Attribute but i didn't find out what i have to use at SetupDiGetDeviceRegistryProperty. SetupDiGetDeviceRegistryProperty is described here https://msdn.microsoft.com/en-us/library/windows/hardware/ff551967(v=vs.85).aspx Maybe someone knows what is the right value for Property. My Code: int get_device_info( void ) { HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i; FILE * devices = fopen("devices.txt", "a+"); GUID AGuid; //GUID can be constructed from "{xxx....}" string using CLSID CLSIDFromString(TEXT(TO_SEARCH_DEVICE_UUID), &AGuid); GUID BluetoothInterfaceGUID = AGuid; // Create a HDEVINFO with all present devices. hDevInfo = SetupDiGetClassDevs(&BluetoothInterfaceGUID, 0, // Enumerator 0, DIGCF_ALLCLASSES | 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; // // Call function with null to begin with, // then use the returned buffer size (doubled) // to Alloc the buffer. Keep calling until // success or an unknown failure. // // Double the returned buffersize to correct // for underlying legacy CM functions that // return an incorrect buffersize value on // DBCS/MBCS systems. // while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, //SPDRP_DEVICEDESC, //SPDRP_CAPABILITIES, &DataT, (PBYTE)buffer, buffersize, &buffersize)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. if (buffer) LocalFree(buffer); // Double the size to avoid problems on // W2k MBCS systems per KB 888609. buffer = (wchar_t *)LocalAlloc(LPTR,buffersize * 2); } else { // Insert error handling here. break; } } if(buffer) { if( strcmp("Name of Device",AnsiString(buffer).c_str())==0) { fprintf(devices,"Result:[%s]",AnsiString(buffer).c_str()); if (buffer) LocalFree(buffer); } } } if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS ) { // Insert error handling here. return 1; } // Cleanup SetupDiDestroyDeviceInfoList(hDevInfo); fclose(devices); return 0; }

Aucun commentaire:

Enregistrer un commentaire