Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get bluetooth devices in range

I am developing for Windows Desktop. I want to get the devices in range and to show then as a list to the user.

This is the code I am using:

HBLUETOOTH_DEVICE_FIND founded_device;

BLUETOOTH_DEVICE_INFO device_info;
device_info.dwSize = sizeof(device_info);

BLUETOOTH_DEVICE_SEARCH_PARAMS search_criteria;
search_criteria.dwSize = sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS);
search_criteria.fReturnAuthenticated = FALSE;
search_criteria.fReturnRemembered = FALSE;
search_criteria.fReturnConnected = TRUE;
search_criteria.fReturnUnknown = FALSE;
search_criteria.fIssueInquiry = FALSE;
search_criteria.cTimeoutMultiplier = 0;

founded_device = BluetoothFindFirstDevice(&search_criteria, &device_info);

if (founded_device == NULL)
{
    _tprintf(TEXT("Error: \n%s\n"), getErrorMessage(WSAGetLastError(), error));
    return -1;
}

do
{
    _tprintf(TEXT("founded device: %s\n"), device_info.szName);

} while (BluetoothFindNextDevice(founded_device, &device_info));
return 0;

My problem is that in the list of devices in range I always get the remembered devices. even if fReturnRemembered is set to false.

I need to find a way to get only the devices in range without the remembered devices.

currently what I am doing is, I am trying to open a socket and try to communicate, but is there any other way?

like image 382
user844541 Avatar asked Nov 01 '22 14:11

user844541


1 Answers

If you want to start scan (Device Inquiry) you should change the search criteria:

search_criteria.fIssueInquiry = TRUE;

and maybe use some timeout bigger than 0.

By the way, if BluetoothFindFirstDevice(&search_criteria, &device_info); returns with NULL, it doesn't mean that it fails. It may end the search without results. You should get the error by WSAGetLastError() and check if it is ERROR_NO_MORE_ITEMS.

Alternative way is to use WinSock APIs.

like image 77
Pupsik Avatar answered Nov 15 '22 05:11

Pupsik