Web USB:更改接口状态的操作正在进行错误

问题描述

突然,我在与我的 Angular 应用程序连接的网络 USB 设备上收到错误消息。 错误内容为:An operation that changes interface state is in progress

编辑:更多代码

选择设备并打开连接:

getDeviceSelector() {
  return navigator.usb
    .requestDevice(this.options)
    .then((selectedDevice) => {
        this.device = selectedDevice;
        return this.device.open(); // Begin a session.
    });
}

与设备(RaspBerry Pi)通信

开始与 Pi 上的 web-usb 通信:

connectedDevice
.then(() => this.device.selectConfiguration(1)) // Select configuration #1 for the device.
.then(() => this.device.claimInterface(0)) // Request exclusive control over interface #0.
.then(() => {
    // Read data every 40 ms
    this.interval = interval(40).subscribe(async () => {
        await this.read();
    });
})

处理正在发送的所有数据的读取:

async read() {
  const result = await this.readOneLine();
  this.readCallbacks.forEach((callback) => {
    callback(result);
  });
}

readOneLine() {
  return this.device.transferIn(1,8 * 1024).then(
    (result) => {
      return new Uint8Array(result.data.buffer);
    },(error) => {
        console.error(error);
    }
  );
}

从那时起,我们使用 readCallbacks 函数将我们从设备获取的数据传递给触发的自定义事件。

错误可能与新的 Chrome 更新有关,但我找不到 navigator.usb 或任何其他 USB 相关机制的更改。

有新信息会尽快添加

解决方法

就我而言,该问题仅出现在某些 Windows 笔记本电脑上。我必须#1 安全移除 USB 设备,然后#2 将其插入另一个端口。之后,连接和以前一样正常(也在原来的USB端口上)。

它突然在不同的计算机上同时发生,但我们无法查看导致此问题的确切环境。