2015-12-06 70 views
1

我們已經想盡了辦法建議在:如何來讀取磁條讀寫器輸入流與Windows 10通用的應用程序的鍵盤支持

https://msdn.microsoft.com/en-us/library/windows/hardware/dn312121(v=vs.85).aspx https://msdn.microsoft.com/en-us/library/windows/hardware/dn303343(v=vs.85).aspx

我們能夠找到的所有列表magneticDevices using below code snippet

var magneticDevices = await DeviceInformation.FindAllAsync(aqsFilter); 

但是我們無法從下面的代碼中獲得HidDevice對象。它是給零。

HidDevice device = await HidDevice.FromIdAsync(magneticDevices[0].Id 

我們還在應用程序清單文件中設置了設備功能,如下所示。

<DeviceCapability Name="humaninterfacedevice"> 
    <Device Id="vidpid:0ACD 0520"> 
     <Function Type="usage:0001 0006"/> 
    </Device> 
</DeviceCapability> 

<DeviceCapability Name="usb"> 
    <Device Id="vidpid:0ACD 0520"> 
     <Function Type="winUsbId:4d1e55b2-f16f-11cf-88cb-001111000030"/> 
    </Device> 
</DeviceCapability> 

代碼的完整功能

private async Task<bool> 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 = await DeviceInformation.FindAllAsync(aqsFilter); 
      try 
      { 
       if (magneticDevices != null && magneticDevices.Count > 0) 
       { 
        HidDevice device = await HidDevice.FromIdAsync(magneticDevices[0].Id, Windows.Storage.FileAccessMode.Read); 
        inputReportEventHandler = new TypedEventHandler<HidDevice, HidInputReportReceivedEventArgs>(this.OnInputReportEvent); 
        device.InputReportReceived += inputReportEventHandler; 
        var watcher = DeviceInformation.CreateWatcher(aqsFilter); 
        watcher.Added += WatcherAdded; 
        watcher.Removed += WatcherRemoved; 
        watcher.Start(); 
        hasCardReader = true; 
      } 
       else 
       { 

       } 
      } 
      catch (Exception ex) 
      { 
       Logging.LoggingSessionScenario.LogMessageAsync(ex.Message, LoggingLevel.Error); 
      } 
      return hasCardReader; 
      } 

回答

0

有幾個原因空返回值,但我不覺得有什麼不對您的代碼,因爲你可以找到該設備通過調用FindAllAsync。我會建議你在GitHub上使用this official HIDDevice sample來解決這個問題。

我成功通過改變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)嘗試和改變的ID在這兩個appxmanifest和SampleConfiguration.cs(類設備)。如果您在設備列表中看不到您的設備,那意味着您的設備的配置不正確。

相關問題