none
detect if a BLE device is in range RRS feed

  • Question

  • Hi, im doing a heart rate app, first i get the BLE handle and then  get the characteristics, but my code detect pair devices and if is pair but is not in range, it appear in my list and i wanna detect when is in range to dont appear in my list of connected devices, the code to detect the handle is

    class HRD_EXPORT hrdiscoverer : public IDeviceDiscoverer

    {

    ....

    protected:

            HDEVINFO hDI;

    int n_disp;

    }


    HANDLE hrdiscoverer::GetBLEHandle(__in GUID AGuid)
    {
    	
    	SP_DEVICE_INTERFACE_DATA did;
    	SP_DEVINFO_DATA dd;
    	GUID BluetoothInterfaceGUID = AGuid;
        HANDLE hComm = NULL;
    
    
        n_disp = 0;
    	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;
    
    
            QTextStream(stdout)<< "Ha captado algo\n";
    		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);
    
    
                
                
                n_disp++;
    			GlobalFree(pInterfaceDetailData);
                
    		}
    	}
        //hComm.getName();
        //BluetoothGatt gatt = 0;
    	SetupDiDestroyDeviceInfoList(hDI);
    	return hComm;
    }
    
    


    • Edited by ikerena Wednesday, March 21, 2018 11:22 AM
    Wednesday, March 21, 2018 11:17 AM

Answers

  • Hi Ikerena,

     

    I understand your problem as:

     

    • Your code enumerates paired devices.
    • You’d like to know when a paired device is in range, so you do not connect to out of range devices.

     

    I believe what you’re asking for is to know if a device is ‘connectable.’  Typically Bluetooth LE Advertisements are used to discover this. I am going to infer, based on HeartRate service, you want a notification from the service.

     

    If you intend to always connect to a connectable Bluetooth LE device to get the notifications, we simplify this for developers you simply need to subscribe to a notifiable property on the device, and we’ll connect automatically and surface the notification to your application.

     

    This is done using the GattCharacteristic.ValueChanged event handler.

     

    If you have some other reason I’d be curious, regardless you’d use the BluetoothLEAdvertisementWatcher.

     

    You’ll notice the APIs I recommended is WinRT APIs; they are usable from a native desktop application via COM, C++/WRL, or C++/WinRT.  You’ll be able to write a native desktop application and use our APIs. There were issues before Windows 10 (1709) that had workarounds. However, we expect all issues are resolved in current versions.

     

    I also made no Win32 API recommendations because we’ve stopped investing in them, the features you're looking for are not present in Win32 APIs, they are in maintenance mode only.


    Wednesday, March 21, 2018 11:46 PM