Introduction
Windows 8.1 provides developers access to available devices on a Windows 8.1 computing device through first-class support for device enumeration APIs. By using these APIs, developers can programmatically query for device properties, such as ID, Friendly name, and other device properties.
Device APIs
The Device Enumeration APIs are homed in the Windows.Devices.Enumeration namespace. Devices can be filtered by querying for devices with a specific device interface. The most commonly used device interfaces are:
- Printers—interface value—”{0ECEF634-6EF0-472A-8085-5AD023ECBCCD}”
- Webcams—interface value—”{E5323777-F976-4F5B-9B55-B94699C46E44}”
- Portable devices—interface value—”{6AC27878-A6FA-4155-BA85-F98F491D4F33}”
To query for devices of a specific interface, we use the DeviceInformation call, call the FindAllAsync API, and pass in the filter information. We also can query for devices through the device container approach. In this article, we use the PnpObject class on the Windows.Devices.Enumeration.Pnp namespace to get the device list by calling the FindAllAsync API.
We will explore this more in our hands-on experiment.
Hands On
Create a new Visual Studio 2013 project of type Windows Store Application under the Visual C# project templates. Let’s name the project Win8DeviceEnumeration.
Figure 1: The new project
Open MainPage.xaml; add four radio buttons and one button, as shown in the following code. Also, add a ListView.
<Page x_Class="Win8DeviceEnumeration.MainPage" xmlns_x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns_local="using:Win8DeviceEnumeration" xmlns_d="http://schemas.microsoft.com/expression/blend/2008" xmlns_mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc_Ignorable="d"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <RadioButton x_Name="radioButtonPrinters" Content="Printers" HorizontalAlignment="Left" Margin="158,264,0,0" VerticalAlignment="Top"/> <RadioButton x_Name="radioButtonWebcams" Content="Webcams" HorizontalAlignment="Left" Margin="161,319,0,0" VerticalAlignment="Top"/> <RadioButton x_Name="radioButtonPortableDevices" Content="Portable Devices" HorizontalAlignment="Left" Margin="164,376,0,0" VerticalAlignment="Top"/> <RadioButton x_Name="radioButtonAll" Content="All" HorizontalAlignment="Left" Margin="167,427,0,0" VerticalAlignment="Top"/> <Button x_Name="buttonGetDevices" Content="Get Devices" HorizontalAlignment="Left" Margin="164,504,0,0" VerticalAlignment="Top"/> <ListView x_Name="listBoxDevices" HorizontalAlignment="Left" Height="579" Margin="576,96,0,0" VerticalAlignment="Top" Width="490"/> </Grid> </Page>
Next, we implement the code behind for MainPage.xaml. This is where we will query for the devices based on the selected radio button. First, we need to include the necessary namespaces.
//MainPage.xaml.cs using Windows.Devices.Enumeration; using Windows.Devices.Enumeration.Pnp;
Next, we will implement the click event handler for the button. In this method, we will look at which radio button was selected to determine whether we need to filter the devices or list all the devices.
async private void buttonGetDevices_Click(object sender, RoutedEventArgs e) { listBoxDevices.Items.Clear(); bool queryForAll = true; if ((bool)radioButtonPrinters.IsChecked || (bool)radioButtonWebcams.IsChecked || (bool)radioButtonPortableDevices.IsChecked) queryForAll = false; if (!queryForAll) { string deviceInterfaceId = ""; if ((bool)radioButtonPrinters.IsChecked) deviceInterfaceId = "{0ECEF634-6EF0-472A-8085-5AD023ECBCCD}"; else if ((bool)radioButtonWebcams.IsChecked) deviceInterfaceId = "{E5323777-F976-4F5B-9B55-B94699C46E44}"; else deviceInterfaceId = "{6AC27878-A6FA-4155-BA85-F98F491D4F33}"; var selector = "System.Devices.InterfaceClassGuid:=\"" + deviceInterfaceId + "\""; var interfaces = await DeviceInformation.FindAllAsync(selector, null); foreach (DeviceInformation deviceInterface in interfaces) { listBoxDevices.Items.Add((string)deviceInterface. Properties["System.ItemNameDisplay"]); } } else { string[] properties = { "System.ItemNameDisplay", "System.Devices.ModelName", "System.Devices.Connected" }; var containers = await PnpObject.FindAllAsync(PnpObjectType.DeviceContainer, properties); foreach (PnpObject container in containers) { listBoxDevices.Items.Add((string)container.Properties["System.ItemNameDisplay"]); } }
Our application is now ready. When we run the application, we can see that, depending on our selection, the list of devices that are listed varies.
Summary
In this article, we learned how to programmatically find available devices on Windows 8.1. If you want to download the sample code, the zip file is included in the article.
About the Author
Vipul Patel is a technology geek based in Seattle. He can be reached at vipul.patel@hotmail.com. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.