Usb 如何在Windows 10 universal app中通过键盘支持从磁条阅读器读取输入流

Usb 如何在Windows 10 universal app中通过键盘支持从磁条阅读器读取输入流,usb,windows-10,win-universal-app,Usb,Windows 10,Win Universal App,我们尝试了以下建议的方法: 我们可以使用下面的代码片段找到所有磁设备的列表 var magneticDevices=wait DeviceInformation.findalsync(aqsFilter); 但是我们无法从下面的代码中获取HidDevice对象。这是给空的 HidDevice device=wait HidDevice.FromIdAsync(磁设备[0]。Id 我们还在应用程序清单文件中设置了设备功能,如下所示 完整函数的代码 private异步任务HasCardR

我们尝试了以下建议的方法:

我们可以使用下面的代码片段找到所有磁设备的列表

var magneticDevices=wait DeviceInformation.findalsync(aqsFilter);
但是我们无法从下面的代码中获取HidDevice对象。这是给空的

HidDevice device=wait HidDevice.FromIdAsync(磁设备[0]。Id
我们还在应用程序清单文件中设置了设备功能,如下所示


完整函数的代码

private异步任务HasCardReader()
{
bool hasCardReader=false;
ushort usagePage=0x0001;
ushort usageId=0x0006;
ushort vendorId=0x0ACD;
ushort productId=0x0520;
var aqsFilter=HidDevice.GetDeviceSelector(usagePage、usageId、vendorId、productId);
var magneticDevices=等待设备信息。FindAllAsync(aqsFilter);
尝试
{
if(magneticDevices!=null&&magneticDevices.Count>0)
{
HIDEVICE device=等待HIDEVICE.FromIdAsync(磁设备[0].Id,Windows.Storage.FileAccessMode.Read);
inputReportEventHandler=新类型的DevenHandler(this.OnInputReportEvent);
device.InputReportReceived+=inputReportEventHandler;
var-watcher=DeviceInformation.CreateWatcher(aqsFilter);
watcher.Added+=WatcherAdded;
watcher.Removed+=WatcherRemoved;
watcher.Start();
hasCardReader=true;
}
其他的
{
}
}
捕获(例外情况除外)
{
LoggingSessionScenario.LogMessageAsync(例如Message,LoggingLevel.Error);
}
返回读卡器;
}

空返回值有几个原因,但我认为您的代码没有问题,因为您可以通过调用FindAllAsync找到设备。我建议您使用GitHub解决此问题

通过将vid&pid&usagepage&usageid更改为我的设备,我成功地使用该示例连接到我的外部hid设备

在EventHandlerForDevice.cs中,找到函数OpenDeviceAsync,当FromIdAsync返回null时,您将注意到以下可能的原因

else
{
    successfullyOpenedDevice = false;

    notificationStatus = NotifyType.ErrorMessage;

    var deviceAccessStatus = DeviceAccessInformation.CreateFromId(deviceInfo.Id).CurrentStatus;

    if (deviceAccessStatus == DeviceAccessStatus.DeniedByUser)
    {
        notificationMessage = "Access to the device was blocked by the user : " + deviceInfo.Id;
    }
    else if (deviceAccessStatus == DeviceAccessStatus.DeniedBySystem)
    {
        // This status is most likely caused by app permissions (did not declare the device in the app's package.appxmanifest)
        // This status does not cover the case where the device is already opened by another app.
        notificationMessage = "Access to the device was blocked by the system : " + deviceInfo.Id;
    }
    else
    {
        // Most likely the device is opened by another app, but cannot be sure
        notificationMessage = "Unknown error, possibly opened by another app : " + deviceInfo.Id;
    }
}
请尝试使用该示例(Scenario1),并更改appxmanifest和SampleConfiguration.cs(类设备)中的ID。如果在设备列表中看不到设备,则表示设备的配置不正确