Chrome USB API - How do I get the ConnectionHandle

755 views
Skip to first unread message

Paolo

unread,
Jan 31, 2015, 6:44:25 AM1/31/15
to chromi...@chromium.org

I've been looking at USB Api to connect to a USB relay.

Using chrome.usb.findDevices I can open the device but then I need to send some data to it. I'm thinking chrome.usb.controlTransfer is the way to do that. I have no idea how to get the ConnectionHandle which I need for the next step.

My findDevices code is below.

Any help would be gratefully received. I'm properly confused.

var usbConnection;
var onOpenCallback = function(connection) {
if (connection) {
usbConnection = connection;
document.getElementById("found").innerHTML ="Device opened.";
} else {
document.getElementById("found").innerHTML ="Device failed to open.";
}
}; chrome
chrome.usb.findDevices(DEVICE_INFO, onOpenCallback);

Ken Rockot

unread,
Jan 31, 2015, 12:46:32 PM1/31/15
to Paolo, Chromium Apps
The callback (onOpenCallback) receives an array of ConnectionHandles as described here. If the array is empty, no devices were opened.

--
You received this message because you are subscribed to the Google Groups "Chromium Apps" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-app...@chromium.org.
To post to this group, send email to chromi...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-apps/.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Paolo

unread,
Jan 31, 2015, 12:55:54 PM1/31/15
to chromi...@chromium.org, exhibitinte...@gmail.com
Thanks Ken

I'm slowly getting my head around this...

So even though my if (connection) is saying "device open" it may actually not be?

Would I be better to try to get my head around "chrome.usb.getDevices" and "chrome.usb.openDevice" rather than find?

I'm really struggling here... any help is appreciated.

λ Ken Rockot

unread,
Jan 31, 2015, 1:06:41 PM1/31/15
to Paolo, Chromium Apps
I recommend getDevices and openDevice instead of findDevices, because findDevices may unnecessarily open devices you don't want to use.

In any case, your function will always result in "Device opened." because the connection argument is an Array, which will always evaluate to true even if it's empty.

Paolo

unread,
Jan 31, 2015, 1:33:45 PM1/31/15
to chromi...@chromium.org, exhibitinte...@gmail.com

Brilliant...  Thank you Ken

I'll go away and see if I can use getDevices.

Does it have any effect if the device drivers are loaded? Or not.  Just something I read.

Ken Rockot

unread,
Jan 31, 2015, 2:50:03 PM1/31/15
to Paolo, Chromium Apps
It can matter. Chrome would be unable to open a device that's been claimed by something else.

Paolo

unread,
Feb 1, 2015, 6:34:05 AM2/1/15
to chromi...@chromium.org, exhibitinte...@gmail.com
I'm 'slowly' getting somewhere...

chrome.use.getDevices gets me what is below in the console :

devices.length = 1
devices[0].device = 13

What I can't seem to do is define the device for chrome.usb.openDevice.

I get this error : Uncaught ReferenceError: device is not defined.

Sorry if these seems frustratingly easy... I'm on a learning curve.

var onDeviceFound = function(devices) {
 
this.devices=devices;
 
if (devices) {
   
if (devices.length > 0) {
    console
.log("Device(s) found: "+ devices.length );

    console
.log("Device: "+ devices[0].device);

 
}
 
} else {
    console
.log("Device could not be found.");
 
}
};

chrome
.usb.getDevices(DEVICE_INFO , onDeviceFound);


var usbConnection = null;


var onOpenCallback = function(connection) {
 
if (connection) {
    usbConnection
= connection;

    console
.log("Device opened.");
 
} else {
    console
.log("Device failed to open.");
 
}
};

chrome
.usb.openDevice(device, onOpenCallback);

Paolo

unread,
Feb 1, 2015, 1:05:50 PM2/1/15
to chromi...@chromium.org, exhibitinte...@gmail.com
Looking at things another way using findDevices I get handle undefined.

Am I getting the handle the correct way? There's very little in the way of examples or tutorial on the web.

How can I even tell if my device can be controlled this way?

I have also uninstalled the drivers then the device doesn't show at all.


chrome.usb.findDevices(DEVICE_INFO, function(ConnectionHandle) {

 
if (ConnectionHandle) {
        console
.log("Handle " + ConnectionHandle.handle);

}
 
 
else {
    console
.log("Device could not be found.");
 
}
});

Reilly Grant

unread,
Feb 1, 2015, 5:54:32 PM2/1/15
to Paolo, chromi...@chromium.org
"Uncaught ReferenceError: device is not defined." is a JavaScript error cause by the variable "device," used when you call openDevice, is not defined. You should be calling openDevice from inside the callback passed to getDevices once you have decided which device in the array provided by getDevices you want to open.

I suggest turning to StackOverflow for general JavaScript help. I also monitor the "google-chrome-app" and "usb" keywords over there.

Paolo

unread,
Feb 2, 2015, 5:56:22 AM2/2/15
to chromi...@chromium.org, exhibitinte...@gmail.com
Thank you Grant...

I have that part working now... on to the next part getting the handle !!!

Paolo

unread,
Feb 2, 2015, 9:05:36 AM2/2/15
to chromi...@chromium.org, exhibitinte...@gmail.com
I have now connect to me device using the serial API on Com 4

Now I have to sent my device some data... in the device data to switch on the relay I have to send 100 - decimal or 65 hex.

I believe I have to create and arraybuffer of this data... Does anyone know how I'd go about this? I can't seem to find a clear tutorial or example.

Thanks

Paul

Reilly Grant

unread,
Feb 2, 2015, 11:34:54 AM2/2/15
to Paolo, chromi...@chromium.org
You will get a faster response for general Javascript questions on StackOverflow. I suggest looking at this Chrome app example where I construct a number of small ArrayBuffers for use with the chrome.hid API. The USB and serial APIs are similar:

Paolo

unread,
Feb 2, 2015, 11:41:56 AM2/2/15
to chromi...@chromium.org, exhibitinte...@gmail.com
Thank you... I'll take a look through this... clear as mud on first glance ;-)

Paolo

unread,
Feb 2, 2015, 12:07:33 PM2/2/15
to chromi...@chromium.org, exhibitinte...@gmail.com
Took me 10 mins now I'm there, I understand the arraybuffer... thank you for all of your help. I have my relay clicking away and lighting up leds.
Reply all
Reply to author
Forward
0 new messages