Javascript chrome.hid.send在第二次使用时失败

Javascript chrome.hid.send在第二次使用时失败,javascript,google-chrome-app,hid,Javascript,Google Chrome App,Hid,关于我使用chrome.hid.send的某些内容似乎使总线处于不好的状态。我始终无法使第二次使用API调用正常工作。有时,它在第一次使用时也会失败。使用完全相同的代码,我可以稍后回来尝试(可能10分钟),第一次发送就可以了 我正在使用的设备不会返回对发送到它的所有消息的响应。例如,测试消息只是一条被设备忽略的伪消息。我已经在mac和PC上对此进行了测试。在我的应用程序中,此时我的调用堆栈深度为2(确切地说,第一个调用堆栈是通过单击按钮启动的,然后一个setTimeout在5s后调用相同的方法)

关于我使用chrome.hid.send
的某些内容似乎使总线处于不好的状态。我始终无法使第二次使用API调用正常工作。有时,它在第一次使用时也会失败。使用完全相同的代码,我可以稍后回来尝试(可能10分钟),第一次发送就可以了

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

我已经测试了长度为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();

问题是Windows要求缓冲区为设备预期的完整报告大小。我已经对Chromium提出诉讼,以跟踪添加一个解决方法或至少一个更好的错误消息来查明问题


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

有几个问题。你正在运行什么版本的Chrome?第二次使用的结果是什么?回调是否从未执行过?chrome.runtime.lastError是否设置为错误?版本41.0.2240.0 canary(64位)。当它失败时,我为onTxCompleted指定了一个方法,我传递的回调执行得很好。不过,我在最新稳定(常规)的chrome中也遇到了问题。因此,失败的原因是没有向设备发送数据,或者chrome.runtime.lastError.message设置为“传输失败”在回调中?chrome.runtime.lastError设置为“传输失败”,并且没有数据发送到设备。通常发生在Windows7机器上(测试了三台),大多数情况下发生在Mac电脑上。我总是让它在mac上每秒发送一次。现在它几乎每次都在mac上发生。