While working on the Devices and Printing chapter of the second edition of Programming Windows Store Apps with HTML, CSS, and JavaScript, I’ve been bitten several times by a subtly confusing feature of the device enumeration API and it’s projection into JavaScript.

Device enumeration happens in a few different ways, but a common method is to ask a device object like HidDevice or Bluetooth.RfcommDeviceService (both under the Windows.Devices namespace) for a “selector” string through a static method called getDeviceSelector. You then pass this to the Windows.Devices.Enumeration.DeviceInformation.findAllAsync.

The gotcha is that findAllAsync has two overloads that take one argument: one that takes a selector string, and one that takes a DeviceClass object. Only one of these, however, is projected into JavaScript and it’s the DeviceClass variant. Unfortunately, if you happen to pass a selector string to this method, you’ll get no complaint anywhere along the way: the API will accept the string, see that it’s an invalid DeviceClass, and then ignore it.

As a result, this variant of findAllAsync just assumes then that you didn’t need the argument and does the equivalent of findAllAsync() with no arguments, which enumerates every device on your system (all 300+ of them, typically!). This is, like I said, very confusing if you were only expecting to see a single result.

When using a selector to enumerate devices in JavaScript, then, be sure to call the findAllAsync(<selector>, <properties>) variant, passing null for <properties>. This ensures that you use the findAllAsync that expects a selector string, and thus you’ll get the results you’re looking for.



One Comment

  1. Posted February 21, 2014 at 5:16 pm | Permalink

    Thank you a lot. Just encountered with the same problem.