Search code examples
c#uwpusbprinters

UWP app cannot find the USB device. Always getting null from UsbDevice.FromIdAsync()


I tried to connect USB devices in uwp according to the instructions in MSDN, but I couldn't connect to usb devices and always getting null from UsbDevice.FromIdAsync (). I had seen may questions and answers regarding this issue here here

But nothing solved my issue. Any help would be appreciated.

these are the code snippet am trying. the same code snippet is working with .Net standard applications. But not working with UWP.

UsbDevice device = null;
        string HPVID = "1008";
        string pid = "9812";
        try
        {

            Task.Run(async () =>
            {
                var aqsFilter = "System.Devices.InterfaceEnabled:= System.StructuredQueryType.Boolean#True " + "AND System.DeviceInterface.WinUsb.UsbVendorId:=" + HPVID +
                    "AND System.DeviceInterface.Winusb.UsbProductId:=" + pid;
                DeviceInformationCollection deviceInterface = await DeviceInformation.FindAllAsync(aqsFilter);

                if (deviceInterface.Count > 0)
                {
                    device = await UsbDevice.FromIdAsync(deviceInterface[0].Id);
                }

            }).Wait();
        }

Solution

  • UWP app cannot find the USB device. Always getting null from UsbDevice.FromIdAsync()

    Please check the package manifest file if add DeviceCapability capability, here is code sample that you could refer.

    Really I want to know what actually means

    We have document that explain the Function . And here is document how to specify device capabilities in a package manifest. Here is support list that you could refer.

    A string between 1 and 100 characters in length. Where appropriate, the string may begin with "classId:" | "winUsbId:" | "serviceId:" | "serviceId:" | "usage:" | "interfaceId:"