-
-
Notifications
You must be signed in to change notification settings - Fork 121
Enumerating Connected Devices
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. Using the DeviceListener is probably a better option than simply enumerating devices because it will handle the connecting to (initializing) and from the device for you.
Note: if you have not already been through the process you will need to configure device permissions on Android, or UWP.
Here is an example for registering both factories on Windows:
WindowsUsbDeviceFactory.Register();
WindowsHidDeviceFactory.Register();
Then, you can use the Device manager to get a list of connected devices.
var devices = await DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(null);
Console.WriteLine("Currently connected devices: ");
foreach (var device in devices)
{
Console.WriteLine(device.DeviceId);
}
Console.WriteLine();
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 both 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<FilterDeviceDefinition>
{
new FilterDeviceDefinition{ DeviceType= DeviceType.Usb, VendorId= 0x1209, ProductId=0x53C1, 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();
}