我对 chrome.hid.send
的使用似乎让总线处于糟糕的状态。我一直无法让我第二次使用 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();
最佳答案
问题是 Windows 要求缓冲区为设备预期的完整报告大小。我已经提交了 a bug针对 Chromium 跟踪添加解决方法或至少添加更好的错误消息来查明问题。
通常,通过使用 --enable-logging --v=1
命令行选项启用详细日志记录,您可以从 chrome.hid API 获取更详细的错误消息。 Chrome 日志记录的完整文档是 here .
关于javascript - chrome.hid.send 第二次使用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27283505/