Enumerating Point of Service devices

This topic demonstrates how to define a device selector that is used to query devices available to the system and use this selector to enumerate Point of Service devices using one of the following methods:

Method 1: Use a device picker

Display a device picker UI and have the user choose a connected device. This method handles updating the list when devices are attached and removed, and is simpler and safer than other methods.

Method 2: Get first available device

Use GetDefaultAsync to access the first available device in a specific Point of Service device class.

Method 3: Snapshot of devices

Enumerate a snapshot of Point of Service devices that are present on the system at a given point in time. This is useful when you want to build your own UI or need to enumerate devices without displaying a UI to the user. FindAllAsync will hold back results until the entire enumeration is completed.

Method 4: Enumerate and watch

DeviceWatcher is a more powerful and flexible enumeration model that allows you to enumerate devices that are currently present, and also receive notifications when devices are added or removed from the system. This is useful when you want to maintain a current list of devices in the background for displaying in your UI rather than waiting for a snapshot to occur.

Define a device selector

A device selector enables you to limit the devices you are searching through when enumerating devices. This lets you get only relevant results and reduce the time it takes to enumerate the desired devices.

You can use the GetDeviceSelector method for the type of device that you're looking for and get the device selector for that type. For example, using PosPrinter.GetDeviceSelector will provide you with a selector to enumerate all PosPrinters attached to the system, including USB, network and Bluetooth POS printers.

using Windows.Devices.PointOfService;

string selector = POSPrinter.GetDeviceSelector();

The GetDeviceSelector methods for the different device types are:

Using a GetDeviceSelector method that takes a PosConnectionTypes value as a parameter, you can restrict your selector to enumerate local, network, or Bluetooth-attached POS devices, reducing the time it takes for the query to complete. The sample below shows a use of this method to define a selector that supports only locally attached POS printers.

using Windows.Devices.PointOfService;

string selector = POSPrinter.GetDeviceSelector(PosConnectionTypes.Local);

Tip

See Build a device selector for building more advanced selector strings.

Method 1: Use a device picker

The DevicePicker class allows you to display a picker flyout that contains a list of devices for the user to choose from. You can use the Filter property to choose which types of devices to show in the picker. This property is of type DevicePickerFilter. You can add device types to the filter using the SupportedDeviceClasses or SupportedDeviceSelectors property.

When you are ready to show the device picker, you can call the PickSingleDeviceAsync method, which will show the picker UI and return the selected device. You'll need to specify a Rect that will determine where the flyout appears. This method will return a DeviceInformation object, so to use it with the Point of Service APIs, you'll need to use the FromIdAsync method for the particular device class that you want. You pass the DeviceInformation.Id property as the method's deviceId parameter, and get an instance of the device class as the return value.

The following code snippet creates a DevicePicker, adds a barcode scanner filter to it, has the user pick a device, and then creates a BarcodeScanner object based on the device ID:

private async Task<BarcodeScanner> GetBarcodeScanner()
{
    DevicePicker devicePicker = new DevicePicker();
    devicePicker.Filter.SupportedDeviceSelectors.Add(BarcodeScanner.GetDeviceSelector());
    Rect rect = new Rect();
    DeviceInformation deviceInformation = await devicePicker.PickSingleDeviceAsync(rect);
    BarcodeScanner barcodeScanner = await BarcodeScanner.FromIdAsync(deviceInformation.Id);
    return barcodeScanner;
}

Method 2: Get first available device

The simplest way to get a Point of Service device is to use GetDefaultAsync to get the first available device within a Point of Service device class.

The sample below illustrates the use of GetDefaultAsync for BarcodeScanner. The coding pattern is similar for all Point of Service device classes.

using Windows.Devices.PointOfService;

BarcodeScanner barcodeScanner = await BarcodeScanner.GetDefaultAsync();

Caution

GetDefaultAsync must be used with care as it may return a different device from one session to the next. Many events can influence this enumeration resulting in a different first available device, including:

  • Change in cameras attached to your computer
  • Change in Point of Service devices attached to your computer
  • Change in network-attached Point of Service devices available on your network
  • Change in Bluetooth Point of Service devices within range of your computer
  • Changes to the Point of Service configuration
  • Installation of drivers or OPOS service objects
  • Installation of Point of Service extensions
  • Update to Windows operating system

Method 3: Snapshot of devices

In some scenarios you may want to build your own UI or need to enumerate devices without displaying a UI to the user. In these situations, you could enumerate a snapshot of devices that are currently connected or paired with the system using DeviceInformation.FindAllAsync. This method will hold back any results until the entire enumeration is completed.

Tip

It is recommended to use the GetDeviceSelector method with the PosConnectionTypes parameter when using FindAllAsync to limit your query to the connection type desired. Network and Bluetooth connections can delay the results as their enumerations must complete before FindAllAsync results are returned.

Caution

FindAllAsync returns an array of devices. The order of this array can change from session to session, therefore it is not recommended to rely on a specific order by using a hardcoded index into the array. Use DeviceInformation properties to filter your results or provide a UI for the user to choose from.

This sample uses the selector defined above to take a snapshot of devices using FindAllAsync then enumerates through each of the items returned by the collection and writes the device name and ID to the debug output.

using Windows.Devices.Enumeration;

DeviceInformationCollection deviceCollection = await DeviceInformation.FindAllAsync(selector);

foreach (DeviceInformation devInfo in deviceCollection)
{
    Debug.WriteLine("{0} {1}", devInfo.Name, devInfo.Id);
}

Tip

When working with the Windows.Devices.Enumeration APIs, you will frequently need to use DeviceInformation objects to obtain information about a specific device. For example, the DeviceInformation.ID property can be used to recover and reuse the same device if it is available in a future session and the DeviceInformation.Name property can be used for display purposes in your app. See the DeviceInformation reference page for information about additional properties available.

Method 4: Enumerate and watch

A more powerful and flexible method of enumerating devices is creating a DeviceWatcher. A device watcher enumerates devices dynamically, so that the application receives notifications if devices are added, removed, or changed after the initial enumeration is complete. A DeviceWatcher will allow you to detect when a network-connected device comes online, a Bluetooth device is in range, as well as if a locally connected device is unplugged so that you can take the appropriate action within your application.

This sample uses the selector defined above to create a DeviceWatcher as well as defines event handlers for the Added, Removed, and Updated notifications. You will need to fill in the details of the actions that you wish to take upon each notification.

using Windows.Devices.Enumeration;

DeviceWatcher deviceWatcher = DeviceInformation.CreateWatcher(selector);
deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Removed += DeviceWatcher_Removed;
deviceWatcher.Updated += DeviceWatcher_Updated;

void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
{
    // TODO: Add the DeviceInformation object to your collection
}

void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
{
    // TODO: Remove the item in your collection associated with DeviceInformationUpdate
}

void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
    // TODO: Update your collection with information from DeviceInformationUpdate
}

Tip

See Enumerate and watch devices for more details on the use of a DeviceWatcher.

See also

Support and feedback

Find answers to your questions

Have questions? Ask us on either our Docs Q&A forum with the UWP tag or on Stack Overflow with the pointofservice tag.

Help us locate your questions:

  • Add the pointofservice tag to your question on Stack Overflow.
  • Include the "UWP" term in your post on the Q&A forum