Last active
January 27, 2020 08:43
-
-
Save davidkelley/9002267 to your computer and use it in GitHub Desktop.
Example showing how to connect with a USB device using Javascript. Note that this file uses the chrome.usb.* API's which are only available for "packaged app" Chrome extensions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var port = 0; | |
var endpoint = 0x01; | |
var device = { vendorId: 0x04b8, productId: 0x0202}; | |
var connect = function(callback) { | |
chrome.permissions.getAll(function(p) { | |
if (p.permissions.indexOf('usb') >= 0) { | |
//construct permission object for our device | |
var obj = { usbDevices: [device] }; | |
//now request the permissions | |
chrome.permissions.request({ permissions: [obj] }, function(granted) { | |
if (granted) { | |
chrome.usb.findDevices(device, function(devices) { | |
if (devices && devices.length > 0) { | |
//use the first found device | |
var foundDevice = devices[0]; | |
//now lets reset the device | |
chrome.usb.resetDevice(foundDevice, function() { | |
//perform some error checking to make sure we reset the device | |
if ( ! chrome.runtime.lastError) { | |
//now claim the interface using the port we specified | |
chrome.usb.claimInterface(foundDevice, port, function() { | |
if ( ! chrome.runtime.lastError) { | |
callback(foundDevice); | |
} else { | |
throw chrome.runtime.lastError.message; | |
} | |
}) | |
} else { | |
throw chrome.runtime.lastError.message; | |
} | |
}); | |
} else { | |
console.warn("Device not found!"); | |
} | |
}); | |
} else { | |
console.warn("USB Permission not granted.") | |
} | |
}); | |
} else { | |
console.warn("No USB permissions granted."); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@FrozenMaiden nop, didn't manage to, did you find a different solution?