關於我使用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