UWP USB DeviceWatcher Doesn't Work

1.1k Views Asked by At

Firstly, I should point out that I have added the device that I need to talk to in to my app's manifest, and I am able to successfully talk to the device. I am able to get this device information for the device with this line:

var trezorDeviceInformation = await UWPHidDevice.GetDeviceByIdSlowAsync(TrezorManager.HidId);

But, for some reason, if I try to watch for the device with product id, and vendor, Id, the watcher's events never fire. I have tried this:

    public UWPHidDevice(uint vendorId, uint productId)
    {
        var deviceWatcher = DeviceInformation.CreateWatcher($"System.DeviceInterface.WinUsb.UsbVendorId:={vendorId} AND System.DeviceInterface.WinUsb.UsbProductId:={productId}");
        deviceWatcher.Added += DeviceWatcher_Added;
        deviceWatcher.Removed += DeviceWatcher_Removed;
        deviceWatcher.Start();
    }

and

    public UWPHidDevice(uint vendorId, uint productId)
    {
        string aqs = UsbDevice.GetDeviceSelector(vendorId, productId);
        var deviceWatcher = DeviceInformation.CreateWatcher(aqs);
        deviceWatcher.Added += DeviceWatcher_Added;
        deviceWatcher.Removed += DeviceWatcher_Removed;
        deviceWatcher.Start();
    }

The added event never fires when I plug the device in, or start the app up with the device. I must stress that I can use this device in my app, and I have triple checked the VendorId and ProductId. It's just the watcher that doesn't work. I'm stuck.

Does anyone have tips or a sample app?

Edit: I have tried the official UWP sample app. I replaced the VendorId and ProductId with the device values that I need to use in both the code, and the app manifest. The sample has the same issue.

1

There are 1 best solutions below

2
On

Is you device showing up in device manager using the WinUSB driver? The code you have works for me with my vendor id and pid only when using the WinUSB driver.

I use this code:

// Create a device watcher to look for instances of the Xerox device
watcher = DeviceInformation.CreateWatcher(UsbDevice.GetDeviceSelector(vendorId, productId));

watcher.Added += new TypedEventHandler<DeviceWatcher, DeviceInformation>(this.OnDeviceAdded);
watcher.Removed += new TypedEventHandler<DeviceWatcher, DeviceInformationUpdate>(this.OnDeviceRemoved);
watcher.EnumerationCompleted += new TypedEventHandler<DeviceWatcher, Object>(this.OnDeviceEnumerationComplete);
watcher.Start();