Chrome.hid.send doesn't work on second use
Something about my use chrome.hid.send
seems to be leaving the bus in poor condition. I constantly can't get the second use of the API call to work. Sometimes it also fails on first use. WITH EXACT DIFFERENT CODE, I can come back and try later (maybe 10 minutes) and the first submission will work.
The device I am working with does not return a response to all messages sent to it. For example, a test message is just a dummy message that is ignored by the device. I have tested this on both Mac and PC. The stack depth of my call is 2 at this point in my application (literally the first one starts with a button click and then setTimeout
calls the same method after 5 seconds).
I have tested sending buffers of 64 bits as well as 58 bits. Properties of the HidDeviceInfo object read "maxInputReportSize": 64, "maxOutputReportSize": 64
Params on first use:
Params on second use:
I really can't figure out how I'm using the API wrong. When messages succeed, I see them on the device side.
// 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();
source to share
The problem is that Windows requires the buffers to be the maximum report size expected by the device. I've posted a bug against Chromium to track down adding a workaround, or at least a better bug report to pinpoint the problem.
In general, you can get more verbose error messages from the chrome.hid API by enabling verbose logging using command line parameters --enable-logging --v=1
. Full documentation on Chrome registration is here .
source to share