Skip to content

Enumerating Connected Devices

Christian Findlay edited this page Dec 30, 2018 · 27 revisions

To enumerate connected devices, you must register the factories for the device types. If you're not sure which device type you want to connect to, you should try both USB, and Hid. In this case, you will need to add both NuGet packages.

Here is an example for registering both factories on Windows:

    WindowsUsbDeviceFactory.Register();
    WindowsHidDeviceFactory.Register();

Code Reference

Then, you can use the Device manager to get a list of connected devices.

    var devices = await DeviceManager.Current.GetConnectedDeviceDefinitions(0x1209, 0x53C1);
    foreach(var device in devices)`
    {
        Debug.WriteLine(device.DeviceId);
    }

Output:

\?\usb#vid_1209&pid_53c1&mi_00#6&3344a6c7&1&0000#{dee824ef-729b-4a0e-9c14-b7117d33a817}

\?\hid#vid_1209&pid_53c1&mi_01#7&317d5c08&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

As you can see above, these are unique Ids that will allow you to construct IDevice objects that can communicate with the Hid or USB device. In this case, the device with a Vid of 0x1209 and Pid of 0x53C1 has noth a USB and a Hid interface. You can construct a device and initialize simply like this:

    var windowsUsbDevice = new WindowsUsbDevice(devices.First().DeviceId);
    await windowsUsbDevice.InitializeAsync();

Alternatively, you can allow the factory class to create your devices like this. This is generally the easier option.

    //Define the types of devices to search for.
    var deviceDefinitions = new List<DeviceDefinition>
    {
        new DeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C1, ReadBufferSize=64, WriteBufferSize=64, Label="Trezor One Firmware 1.7.x" }
    };

    //Get the first available device and connect to it
    var devices = await DeviceManager.Current.GetDevices(deviceDefinitions);
    using (var trezorDevice = devices.FirstOrDefault())
    {
        await trezorDevice.InitializeAsync();
    }

Code Reference

Clone this wiki locally