首页 > 解决方案 > Android Chrome 上的 WebUSB

问题描述

我有一个使用 WebUSB 连接到标签打印机并打印标签的网站。一切都在计算机上运行良好,但不幸的是它不适用于 Android。Android 找到我的打印机“LP 433”,但什么也没发生,它应该打印。我使用 OTG 电缆连接到打印机。谷歌浏览器版本:83。在其他浏览器上WebUSB没有任何反应,除了firefox,直接说不支持WebUSB。我的网站在 https 上工作。

  async findDevices() {
try {
  const navi: any = navigator;
  const devices = await navi.usb.getDevices();
  console.log(devices);
  devices.forEach(dev => {
    console.log(dev);
  });
  const device = await navi.usb.requestDevice({filters: [{vendorId: 15923}]});
  await device.open(); // Begin a session.
  await device.selectConfiguration(1); // Select configuration #1 for the device.
  await device.claimInterface(0); // Request exclusive control over interface #2.
  await device.controlTransferOut({
    requestType: 'vendor',
    recipient: 'interface',
    request: 0x01,  // vendor-specific request: enable channels
    value: 0x0013,  // 0b00010011 (channels 1, 2 and 5)
    index: 0x0001   // Interface 1 is the recipient
  })
    .then(() => device.transferIn(1, 64)) // Waiting for 64 bytes of data from endpoint #5.
    .then(result => {
      const decoder = new TextDecoder();
      console.log('Received: ' + decoder.decode(result.data));
      // document.getElementById('target').innerHTML = 'Received: ' + decoder.decode(result.data);
    })
    .catch(error => {
      console.log(error);
    });

  const imgData: any = await this.toPrintFormat();
  for (let p = 0; p < this.pcs; p++) {
    for (let i = 0; i < imgData.length; i++) {
      //   setTimeout(() => {
      const string2 =
        'sLABEL,' + imgData[i].width + ',' + imgData[i].height + '\n' +
        'sGAP,0,0\n' +
        'sTPHY,0\n' +
        'sDENSITY,7\n' +
        'sSPEED,' + this.speedp + '\n' +
        'sTHERMAL,' + this.thermop + '\n' +
        'sDIRECTION,1\n' +
        'sORIGIN,0,0\n' +
        'wSAVEB64,' + imgData[i].src.length + ',\"WLP000\",' + imgData[i].src + '\n' +
        'wLOADIMG,0,' + 0 + ',1,\"WLP000\"\n' +
        'wPRINT,1\n';
      console.log(string2);
      const encoder = new TextEncoder();
      const data = encoder.encode(string2);
      device.transferOut(2, data)
        .catch(error => {
          console.log(error);
        });
    }
  }
  // document.getElementById('target').innerHTML = 'Received: ' + decoder.decode(result.data);
} catch (error) {
  // document.getElementById('target').innerHTML = error;
}
}

标签: webusb

解决方案


推荐阅读