WebUSB .claimInterface 从不 returns Windows

WebUSB .claimInterface never returns on Windows

我一直在开发一个使用 WebUSB 的网站。 WebUSB 库在 Linux 和 Android 上 100% 工作。不幸的是,在 Windows 上,.claimInterface 返回的承诺从来没有 returns 任何东西。当我出于兴趣,运行 claimInterface 再次返回承诺 returns 错误“更改状态的操作正在进行中”,确认第一个 claimInterface 函数仍在忙。

只有认领接口1时出现问题,认领接口0可以,但是这个接口对我没用。 USB 设备不是标准的(下面播放的描述符)所以它可能与设备配置有关,但由于缺少日志,很难判断。

我确实尝试查看 chrome://device-log?refresh=1 以获取更多日志,但仅显示了插头事件。我还在 chrome://flags/#new-usb-backend 启用了新的 USB 后端,但无济于事。我还尝试了 运行ning chrome 和 --enable-loging --v1,但唯一似乎相关的错误是:ERROR:device_event_log_impl.cc(211)] [01:32:33.170] USB: usb_device_handle_win.cc:1020 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F) .

但是这个错误是在我连接设备之前记录的,也讨论过,所以我什至不知道它是否相关。

Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            2 Communications
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0xXXXX XXX
  idProduct          0xXXXX 
  bcdDevice            1.01
  iManufacturer           1 XXX
  iProduct                2 XXX
  iSerial                 3 XXX
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x0030
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0x40
      (Missing must-be-set bit!)
      Self Powered
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass        10 CDC Data
      bInterfaceSubClass      0 
      bInterfaceProtocol      1 
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval              32
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass        10 CDC Data
      bInterfaceSubClass      0 
      bInterfaceProtocol      1 
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval              32
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval              32
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
Device Status:     0x0001
  Self Powered

驱动程序安装几乎与描述的 WinUSB 完全相同 method

[Version]
DriverVer=XX/XX/XXXX,X.X.X.X
Signature = "$Windows NT$"
Class = USBDevice
ClassGuid = {88BAE032-5A81-49f0-BC3D-A4FF138216D6}
Provider = %ManufacturerName%
CatalogFile = XXX.cat

[ClassInstall32]
AddReg = ClassInstall_AddReg

[ClassInstall_AddReg]
HKR,,,,%ClassName%
HKR,,NoInstallClass,,1
HKR,,IconPath,%REG_MULTI_SZ%,"%systemroot%\system32\setupapi.dll,-20"
HKR,,LowerLogoVersion,,5.2

[WinUSBDeviceClassReg]
HKR,,,0,%ClassName%
HKR,,Icon,,-20

[Manufacturer]
%ManufacturerName% = Standard,NTx86,NTamd64

[Standard.NTx86]
%DeviceName% = USB_Install, USB\%VendorID%&%ProductID%

[Standard.NTamd64]
%DeviceName% = USB_Install, USB\%VendorID%&%ProductID%

[USB_Install]
Include=winusb.inf
Needs=WINUSB.NT

[USB_Install.Services]
Include=winusb.inf
Needs=WINUSB.NT.Services

[USB_Install.HW]
AddReg=Dev_AddReg

[Dev_AddReg]
HKR,,DeviceInterfaceGUIDs,0x10000,{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}

[Strings]
ManufacturerName = "XXX"
ClassName="Universal Serial Bus devices"
DeviceName = "XXX"
VendorID = "VID_XXX"
ProductID = "PID_XXX"

以及我用来测试库的 React 应用程序。

import { useEffect, useState } from 'react';

export default function App() {
  const [devices, setDevices] = useState([]);

  useEffect(function () {
    update()
    navigator.usb.addEventListener("connect", update);
    navigator.usb.addEventListener("disconnect", update);
  }, [])

  return (
    <div className="App">
      <button onClick={refresh}>
        <div>Refresh</div>
      </button>
      <button onClick={connect}>
        <div>Connect</div>
      </button>
    </div>
  );


  function connect() {
    let _device = devices[0]
    if (_device) {
      _device
      .open()
      .then(function () {
        _device
          .selectConfiguration(1)
          .then(function () {
            console.log("Connecting to interface")
            _device
              .claimInterface(1)
              .then(function () {
                console.log("Success");
              })
              .catch(console.error);
          })
          .catch(console.error);
      })
      .catch(console.error);
    } 
  }

  function update() {
    navigator.usb.getDevices().then(function (_devices) {
      setDevices(_devices)
    });
  }
  
  function refresh() {
    navigator.usb
      ?.requestDevice({
        filters: [{ vendorId: 0xXXXX, productId: 0xXXXX }],
      })
      .then(update)
      .catch(console.error);
  }
}

我下载了goole-chrome canary 89.0.4387.0,问题解决了。所以我想这只是 google,在我提出问题之前就解决了问题。