chrome.hid.send在第二次使用时失败
问题描述:
关于我使用chrome.hid.send
的一些事情似乎正在使公共汽车处于不良状态。我始终无法让我的第二次使用API调用工作。有时,它也会在第一次使用时失败。使用完全相同的代码,我可以回来尝试一段时间(也许10分钟),第一次发送将工作。chrome.hid.send在第二次使用时失败
我正在使用的设备不会返回发送给它的所有消息的响应。例如,测试消息只是设备忽略的虚拟消息。我已经在Mac和PC上测试过了。在我的应用程序中,此时我的调用堆栈深度为2(实际上,第一个调用堆栈的深度由单击按钮启动,然后在5秒后调用相同的方法)。
我测试了发送长度为64Bytes以及58Bytes的缓冲区。从HidDeviceInfo对象的属性读 “maxInputReportSize”:64, “maxOutputReportSize”:在第一次使用64个
PARAMS:上第二使用
PARAMS:
我真的无法确定我是如何错误地使用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是否设置为错误? – 2014-12-05 18:27:32
版本41.0.2240.0 canary(64位)。当它失败,我指定一个onTxCompleted的方法,我传递的回调就会执行得很好。 – tarabyte 2014-12-05 18:34:14
虽然我在最新的stable(常规)chrome中也遇到了问题。 – tarabyte 2014-12-05 18:35:57