chrome.hid.send在PC上无法使用,但在Mac上可以使用

塔拉字节

我在Mac和PC上都运行了相同的基本测试,但是当我尝试在PC上运行时似乎失败了。这令人难以置信,因为我在chrome之上构建的全部原因是因为我假设是跨平台的HID API该API的Windows端口是否存在错误?

  COMMS.transmitting = true;
  console.log("Sending...");
  chrome.hid.send(connection_, REPORT_ID, arrayBuffer, function() {
    if (chrome.runtime.lastError) {
      throw chrome.runtime.lastError.message;
      COMMS.transmitting = false;
      return;
    }

    console.log("done sending.");
    COMMS.transmitting = false;
    console.log("Receiving...");
    COMMS.receiving = true;
    chrome.hid.receive(connection_, function(reportId, data) {
      if (chrome.runtime.lastError) {
        throw chrome.runtime.lastError.message;
        COMMS.receiving = false;
        return;
      }

      console.log("done receiving.");
      COMMS.receiving = false;
      responseHandler(data);
    });
  });

完整的comms.js文件:

//
// Description: Communication abstractions.  This is currently built on the 
// chrome.hid API.
//
var COMMS = (function() {
  // -------------------------------------------------------------------------------------------------------------------
  // Private Constants
  // -------------------------------------------------------------------------------------------------------------------
  var REPORT_ID = 0;
  var MAX_NUM_RX_BYTES = 64;
  var MAX_NUM_TX_BYTES = 64;

  // -------------------------------------------------------------------------------------------------------------------
  // Private Properties
  // -------------------------------------------------------------------------------------------------------------------
  var connection_;
  var onDeviceConnected_;

  // -------------------------------------------------------------------------------------------------------------------
  // Private Methods
  // -------------------------------------------------------------------------------------------------------------------
  var enumerateDevices = function() {
    var deviceIds = [];
    var permissions = chrome.runtime.getManifest().permissions;
    for (var i = 0; i < permissions.length; i++) {
      var p = permissions[i];
      if (p.hasOwnProperty('usbDevices')) {
        deviceIds = deviceIds.concat(p.usbDevices);
      }
    }

    // REMOVE AFTER TESING
    deviceFilter = {
      vendorId: 1234, // replace with your own VID/PID combo, should match permissions values
      productId: 5678
    };
    // Only search for the first VID/PID device specified in the permissions
    //chrome.hid.getDevices(deviceIds[0], onDevicesEnumerated);
    chrome.hid.getDevices(deviceFilter, onDevicesEnumerated);
  };

  var onDevicesEnumerated = function(devices) {
    if (chrome.runtime.lastError) {
      console.log(chrome.runtime.lastError.message);
      return;
    }

    if (1 !== devices.length) {
      console.log("Ensure one (and only one) device is inserted.");
      // Schedule the next attempt
      window.setTimeout(enumerateDevices, 2000);
    } else {
      console.log("Found a device");
      connectDevice(devices[0].deviceId);
    }
  };

  var connectDevice = function(deviceId) {
    chrome.hid.connect(deviceId, function(connectInfo) {
      if (chrome.runtime.lastError) {
        console.log(chrome.runtime.lastError.message);
        return;
      }

      if (!connectInfo) {
        console.log("Failed to connect to device.");
        this.transmitting = false;
        this.receiving = false;
        return;
      }

      connection_ = connectInfo.connectionId;
      console.log("connection: " + connection_);
      onDeviceConnected_();
    });
  };

  var byteToHex = function(value) {
    if (value < 16) {
      return '0' + value.toString(16);
    }

    return value.toString(16);
  };

  return {
    // -----------------------------------------------------------------------------------------------------------------
    // Public Properties
    // -----------------------------------------------------------------------------------------------------------------
    transmitting: false,
    receiving: false,

    // -----------------------------------------------------------------------------------------------------------------
    // Public Methods
    // -----------------------------------------------------------------------------------------------------------------
    init: function(onConnectedHandler) {
      // Initialize module memory
      connection_ = -1;
      onDeviceConnected_ = onConnectedHandler;
      // Begin the search for a single, predetermined device
      enumerateDevices();
    },

    isConnected: function() {
      return (-1 !== connection_);
    },

    send: function(arrayBuffer, responseHandler) {      
      if (-1 === connection_) {
        throw "Attempted to send data with no device connected.";
        return;
      }
      if (true === COMMS.receiving) {
        throw "Waiting for a response to a previous message.  Aborting.";
        return;
      }
      if (true === COMMS.transmitting) {
        throw "Still waiting to finish sending a previous message.";
        return;
      }
      if (-1 === connection_) {
        throw "Attempted to send without a device connected.";
        return;
      }
      if (MAX_NUM_TX_BYTES < arrayBuffer.byteLength) {
        throw "Given data is too long to send as a single message.";
        return;
      }
      if (0 === arrayBuffer.byteLength) {
        throw "Given data to transmit is empty";
        return;
      }

      COMMS.transmitting = true;
      console.log("Sending...");

      chrome.hid.send(connection_, REPORT_ID, arrayBuffer, function() {
        if (chrome.runtime.lastError) {
          throw chrome.runtime.lastError.message;
          COMMS.transmitting = false;
          return;
        }

        console.log("done sending.");
        COMMS.transmitting = false;
        console.log("Receiving...");
        COMMS.receiving = true;
        chrome.hid.receive(connection_, function(reportId, data) {
          if (chrome.runtime.lastError) {
            throw chrome.runtime.lastError.message;
            COMMS.receiving = false;
            return;
          }

          console.log("done receiving.");
          COMMS.receiving = false;
          responseHandler(data);
        });
      });
    },

    disconnectDevice: function() {
      if (-1 === connection_) {
        console.log("Attempted to disconnect device with no device connected");
        COMMS.transmitting = false;
        COMMS.receiving = false;
        return;
      }

      chrome.hid.disconnect(connection_, function() {
        if (chrome.runtime.lastError) {
          console.log(chrome.runtime.lastError.message);
          return;
        }

        // TODO: how to check that successfully disconnected?
        console.log("Finished disconnecting device.  Success?");
        COMMS.transmitting = false;
        COMMS.receiving = false;
      });
    },

    hexStringToArrayBuffer: function(s) {
      var bytes = new Uint8Array(s.length);
      for (var i = 0; i < s.length; i += 3) {
        if (' ' === s[i]) {
          console.log('Error parsing hex string');
          return;
        }

        bytes[i] = parseInt(s.substring(i, (i + 2)), 16);
      }

      return bytes.buffer;
    },

    byteArrayToFormattedString: function(byteArray) {
      var s = '';
      for (var i = 0; i < byteArray.length; i += 16) {
        var sliceLength = Math.min(byteArray.length - i, 16);
        var lineBytes = new Uint8Array(byteArray.buffer, i, sliceLength);
        for (var j = 0; j < lineBytes.length; ++j) {
          s += byteToHex(lineBytes[j]) + ' ';
        }
        for (var j = 0; j < lineBytes.length; ++j) {
          var ch = String.fromCharCode(lineBytes[j]);
          if (lineBytes[j] < 32 || lineBytes[j] > 126)
            ch = '.';
          s += ch;
        }
        s += '\n';
      }
      return s;
    }
  }
})();
赖利·格兰特

OP在这里再次问了这个问题抱歉,我没有注意到对最初问题的回答,请在此处进行后续操作。问题是Windows要求缓冲区必须是设备期望的完整报告大小。针对Chromium提交了一个错误,以跟踪添加变通办法或至少找到更好的错误消息来查明问题。

通常,通过使用--enable-logging --v=1命令行选项启用详细日志记录,您可以从chrome.hid API中获取更详细的错误消息有关Chrome日志记录的完整文档,请参见此处

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

chrome.hid.send的ArrayBuffer大小是否有限制?

来自分类Dev

chrome.hid.send失败,因为设备丢失了?

来自分类Dev

我可以通过Chrome应用访问USB HID设备吗?

来自分类Dev

无法在IOS Safari上访问对象,但在桌面版Chrome上可以使用

来自分类Dev

我可以在没有应用程序的情况下在Chrome中访问USB HID吗?

来自分类Dev

如何通过chrome.hid API检测丢失的连接?

来自分类Dev

在Mac Chrome上使用Selenium

来自分类Dev

HID设备可以接收哪些数据?

来自分类Dev

HID设备无法获取实际值

来自分类Dev

JQuery在Firefox中无法使用,但在Chrome中可以使用

来自分类Dev

Youtube Flash API(AS3)在Firefox中无法使用,但在Chrome中可以使用

来自分类Dev

无法从IE访问https:// localhost:7002,但在Firefox和Chrome中也可以使用

来自分类Dev

onmouseover无法在Chrome上使用?

来自分类Dev

尝试从Mac上的HID管理器获取HID设备引用时出现不兼容的指针错误

来自分类Dev

尝试从Mac上的HID管理器获取HID设备引用时出现不兼容的指针错误

来自分类Dev

Arduino Pro Micro作为PC上的“键盘”-在PC上打印“ =”,而不是“-” HID库

来自分类Dev

为什么Selenium可以使用Chrome和Firefox单击单选按钮,却无法在IE 9上运行?

来自分类Dev

在一个特定的HID上停用中间按钮粘贴

来自分类Dev

在Windows的HID设备上执行ReadFile()会发生什么?

来自分类Dev

在一个特定的HID上停用中间按钮粘贴

来自分类Dev

在Windows的HID设备上执行ReadFile()会发生什么?

来自分类Dev

消息CRC校验协议在USB HID上运行

来自分类Dev

聚合物无法在IE或Edge中使用,但在chrome中可以使用,但是对于Doctype,它在chrome中也无法使用

来自分类Dev

使用针对HID的micrsoft代码使用Autohotkey调整亮度

来自分类Dev

使用Twisted Python在Linux上将HID访问与evdev集成

来自分类Dev

如何使用旋转编码器控制HID设备?

来自分类Dev

使用HidLibrary从C#中的任何USB HID检测输入

来自分类Dev

为什么在使用Chrome但在IE中无法在SSRS上出现rsAccessDenied错误

来自分类Dev

获取HID设备的MAC地址时出现问题

Related 相关文章

  1. 1

    chrome.hid.send的ArrayBuffer大小是否有限制?

  2. 2

    chrome.hid.send失败,因为设备丢失了?

  3. 3

    我可以通过Chrome应用访问USB HID设备吗?

  4. 4

    无法在IOS Safari上访问对象,但在桌面版Chrome上可以使用

  5. 5

    我可以在没有应用程序的情况下在Chrome中访问USB HID吗?

  6. 6

    如何通过chrome.hid API检测丢失的连接?

  7. 7

    在Mac Chrome上使用Selenium

  8. 8

    HID设备可以接收哪些数据?

  9. 9

    HID设备无法获取实际值

  10. 10

    JQuery在Firefox中无法使用,但在Chrome中可以使用

  11. 11

    Youtube Flash API(AS3)在Firefox中无法使用,但在Chrome中可以使用

  12. 12

    无法从IE访问https:// localhost:7002,但在Firefox和Chrome中也可以使用

  13. 13

    onmouseover无法在Chrome上使用?

  14. 14

    尝试从Mac上的HID管理器获取HID设备引用时出现不兼容的指针错误

  15. 15

    尝试从Mac上的HID管理器获取HID设备引用时出现不兼容的指针错误

  16. 16

    Arduino Pro Micro作为PC上的“键盘”-在PC上打印“ =”,而不是“-” HID库

  17. 17

    为什么Selenium可以使用Chrome和Firefox单击单选按钮,却无法在IE 9上运行?

  18. 18

    在一个特定的HID上停用中间按钮粘贴

  19. 19

    在Windows的HID设备上执行ReadFile()会发生什么?

  20. 20

    在一个特定的HID上停用中间按钮粘贴

  21. 21

    在Windows的HID设备上执行ReadFile()会发生什么?

  22. 22

    消息CRC校验协议在USB HID上运行

  23. 23

    聚合物无法在IE或Edge中使用,但在chrome中可以使用,但是对于Doctype,它在chrome中也无法使用

  24. 24

    使用针对HID的micrsoft代码使用Autohotkey调整亮度

  25. 25

    使用Twisted Python在Linux上将HID访问与evdev集成

  26. 26

    如何使用旋转编码器控制HID设备?

  27. 27

    使用HidLibrary从C#中的任何USB HID检测输入

  28. 28

    为什么在使用Chrome但在IE中无法在SSRS上出现rsAccessDenied错误

  29. 29

    获取HID设备的MAC地址时出现问题

热门标签

归档