Find then open the Azure Kinect device
This article describes how you can find, then open your Azure Kinect DK. The article explains how to handle the case where there are multiple devices connected to your machine.
You can also refer to the SDK Enumerate Example that demonstrates how to use the functions in this article.
The following functions are covered:
Discover the number of connected devices
First get the count of currently connected Azure Kinect devices using k4a_device_get_installed_count()
.
uint32_t device_count = k4a_device_get_installed_count();
printf("Found %d connected devices:\n", device_count);
Open a device
To get information about a device, or to read data from it, you need to first open a handle to the device using k4a_device_open()
.
k4a_device_t device = NULL;
for (uint8_t deviceIndex = 0; deviceIndex < device_count; deviceIndex++)
{
if (K4A_RESULT_SUCCEEDED != k4a_device_open(deviceIndex, &device))
{
printf("%d: Failed to open device\n", deviceIndex);
continue;
}
...
k4a_device_close(device);
}
The index
parameter of k4a_device_open()
indicates which device to open if there are more than one connected. If you only expect a single device to be connected, you can pass an argument of K4A_DEVICE_DEFAULT
or 0 to indicate the first device.
Anytime you open a device you need to call k4a_device_close()
when you're done using the handle. No other handles can be opened to the same device until you've closed the handle.
Identify a specific device
The order devices enumerate by index won't change until devices are attached or detached. To identify a physical device, you should use the device's serial number.
To read the serial number from the device, use the k4a_device_get_serialnum()
function after you've opened a handle.
This example demonstrates how to allocate the right amount of memory to store the serial number.
char *serial_number = NULL;
size_t serial_number_length = 0;
if (K4A_BUFFER_RESULT_TOO_SMALL != k4a_device_get_serialnum(device, NULL, &serial_number_length))
{
printf("%d: Failed to get serial number length\n", deviceIndex);
k4a_device_close(device);
device = NULL;
continue;
}
serial_number = malloc(serial_number_length);
if (serial_number == NULL)
{
printf("%d: Failed to allocate memory for serial number (%zu bytes)\n", deviceIndex, serial_number_length);
k4a_device_close(device);
device = NULL;
continue;
}
if (K4A_BUFFER_RESULT_SUCCEEDED != k4a_device_get_serialnum(device, serial_number, &serial_number_length))
{
printf("%d: Failed to get serial number\n", deviceIndex);
free(serial_number);
serial_number = NULL;
k4a_device_close(device);
device = NULL;
continue;
}
printf("%d: Device \"%s\"\n", deviceIndex, serial_number);
Open the default device
In most applications, there will only be a single Azure Kinect DK attached to the same computer. If you only need to connect to the single expected device, you can call k4a_device_open()
with index
of K4A_DEVICE_DEFAULT
to open the first device.
k4a_device_t device = NULL;
uint32_t device_count = k4a_device_get_installed_count();
if (device_count != 1)
{
printf("Unexpected number of devices found (%d)\n", device_count);
goto Exit;
}
if (K4A_RESULT_SUCCEEDED != k4a_device_open(K4A_DEVICE_DEFAULT, &device))
{
printf("Failed to open device\n");
goto Exit;
}
Next steps
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for