【问题标题】:chrome.hid.send fails on second usechrome.hid.send 在第二次使用时失败
【发布时间】:2015-02-01 17:04:43
【问题描述】:

我对@9​​87654323@ 的使用似乎使公共汽车处于不良状态。我一直无法让 API 调用的第二次使用正常工作。有时,它也会在第一次使用时失败。使用完全相同的代码,我可以稍后(可能 10 分钟)回来尝试,第一次发送就可以了。

我正在使用的设备不会对发送给它的所有消息返回响应。例如,测试消息只是被设备忽略的虚拟消息。我已经在 Mac 和 PC 上对此进行了测试。在我的应用程序中,此时我的调用堆栈深度为 2(实际上第一个是通过单击按钮启动的,然后 setTimeout 在 5 秒后调用相同的方法)。

我正在测试长度为 64 字节和 58 字节的发送缓冲区。 HidDeviceInfo 对象的属性为 "maxInputReportSize":64,"maxOutputReportSize":64

首次使用时的参数:

第二次使用的参数:

我真的无法确定我是如何错误地使用 API。当消息成功时,我可以在设备端看到它们。

// Transmits the given data
//
// @param[in] outData,       The data to send as an ArrayBuffer
// @param[in] onTxCompleted, The method called on completion of the outgoing transfer.  The return
//                           code is passed as a string.
// @param[in] onRxCompleted, The method called on completion of the incoming transfer.  The return
//                           code is passed as a string along with the response as an ArrayBuffer.
send: function(outData, onTxCompleted, onRxCompleted) {
  if (-1 === connection_) {
    console.log("Attempted to send data with no device connected.");
    return;
  }

  if (0 == outData.byteLength) {
    console.log("Attempted to send nothing.");
    return;
  }

  if (COMMS.receiving) {
    console.log("Waiting for a response to a previous message.  Aborting.");
    return;
  }

  if (COMMS.transmitting) {
    console.log("Waiting for a previous message to finish sending.  Aborting.");
    return;
  }

  COMMS.transmitting = true;
  var dummyUint8Array = new Uint8Array(outData);
  chrome.hid.send(connection_, REPORT_ID, outData, function() {
    COMMS.transmitting = false;

    if (onTxCompleted) {
      onTxCompleted(chrome.runtime.lastError ? chrome.runtime.lastError.message : '');
    }

    if (chrome.runtime.lastError) {
      console.log('Error in COMMS.send: ' + chrome.runtime.lastError.message);
      return;
    }

    // Register a response handler if one is expected
    if (onRxCompleted) {
      COMMS.receiving = true;
      chrome.hid.receive(connection_, function(reportId, inData) {
        COMMS.receiving = false;
        onRxCompleted(chrome.runtime.lastError ? chrome.runtime.lastError.message : '', inData);
      });
    }
  });
}


// Example usage
var testMessage = new Uint8Array(58);
var testTransmission = function() {
  message[0] = 123;
  COMMS.send(message.buffer, null, null);
  setTimeout(testTransmission, 5000);
};
testTranmission();

【问题讨论】:

  • 几个问题。您运行的是哪个版本的 Chrome?第二次使用的结果是什么?回调是否从未执行? chrome.runtime.lastError 是否设置为错误?
  • 版本 41.0.2240.0 金丝雀(64 位)。当它失败并且我为 onTxCompleted 指定一个方法时,我传递的回调执行得很好。
  • 我在最新的稳定(常规)chrome 中也遇到了问题。
  • 所以失败是没有数据发送到设备或者 chrome.runtime.lastError.message 设置为“传输失败”。在回调中?
  • chrome.runtime.lastError 设置为“传输失败”并且没有数据发送到设备。总是发生在 Windows 7 机器上(测试了三台),大多数时间发生在 Mac 上。我一直让它在 mac 上发生,每秒发送一次。现在它几乎每次都发生在 mac 上。

标签: javascript google-chrome-app hid


【解决方案1】:

问题在于 Windows 要求缓冲区是设备预期的完整报告大小。我已针对 Chromium 提交了a bug 以跟踪添加解决方法或至少提供更好的错误消息来查明问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 2010-12-21
    • 2016-08-25
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 2020-10-02
    相关资源
    最近更新 更多