Adding HID Device to UWP Application

In this post I wanted to go through adding a HID device to a UWP Application. I will go into detail about determining the correct usage, setting up capabilities and connecting to the device.

To begin, we need to determine Usage ID and Usage Page of the HID device. We also need to determine the Vendor ID and Product ID for the device. Let’s start by opening the Device Manager, expanding Human Interface Devices and locating your HID device. Right click the device and select Properties.

Select Detail tab and under Property select Hardware Ids from the dropdown. From here we can get all the information we need to connect our device. Make note of the VID, PID, and HD_DEVICE_UP.

At this point we can determine if our device is supported by UWP. You can check MSDN documentation for HID limitations. Basically the device can be accessed if the Usage Page is vendor defined (0xFF00-0xFFFF). In our example, usage page is 0xFF00 and usage id is 0x0004.

Now we are ready to set up our capabilities for the UWP app. To define HID capabilities, we need to edit the package manifest XML. Select Package.appxmanifest file in Visual Studio and select View Code. Scroll down to Capabilities and add a new DeviceCapability.

<DeviceCapability Name="humaninterfacedevice">
   <Device Id="vidpid:[VID] [PID]">
      <Function Type="usage:[UsagePage] [UsageId]"/>

Here are the Capabilities from my UWP app.

   <Capability Name="internetClient" />
   <DeviceCapability Name="webcam" />
   <DeviceCapability Name="microphone" />
   <DeviceCapability Name="humaninterfacedevice">
      <Device Id="vidpid:046D C52B">
         <Function Type="usage:FF00 0001"/>

Replace the VID, PID, and Usage with the values for your device. The order for the usage is imporatant here. Usage page should be first, followed by usage id. In the example above, FF00 is the usage page and 0001 is the usage id.

Final step is to add the code to connect to the HID device. Once connected we can read input reports to get data sent by the HID device. We will use Windows.Devices.HumanInterfaceDevice namespace to interface with our HID device.

ushort vid = 1133;      // 0x046D
ushort pid = 50475;     // 0xC52B

ushort usageId = 1;         // 0x0001
ushort usagePage = 65280;   // 0xFF00

// get selector string for the device (connection string)
string selector = HidDevice.GetDeviceSelector(usagePage, usageId, vid, pid);
// find the device
var myDevices = await DeviceInformation.FindAllAsync(selector);

HidDevice myHidDevice = null;

if (myDevices.Any())
    myHidDevice = await HidDevice.FromIdAsync(myDevices[1].Id, FileAccessMode.Read);

if (myHidDevice != null)
    myHidDevice.InputReportReceived += (sender, args) =>
                    // read report