Javascript 无法使用chrome api从hid设备获取任何输入数据
我正在制作一个小应用程序,它将从带有chrome软件包的usb设备上获取输入数据。这个想法是,当我按下usb设备上的按钮时,它将接收传入的流量,对其进行分析,并根据输入做出反应。 我从在线教程/代码中尝试了几种设备和技术,遇到了许多问题,但解决了这些问题后,我终于开始使用索尼playstation 3 Pad。设备是通过hid连接的,但这就是我所能做到的。按下任何按钮都不会产生任何输入,到目前为止,我不知道这是什么原因。Stackoverflow、谷歌手册和互联网似乎对此没有任何答案。这是我的密码: manifest.json:Javascript 无法使用chrome api从hid设备获取任何输入数据,javascript,google-chrome,usb,google-chrome-app,hid,Javascript,Google Chrome,Usb,Google Chrome App,Hid,我正在制作一个小应用程序,它将从带有chrome软件包的usb设备上获取输入数据。这个想法是,当我按下usb设备上的按钮时,它将接收传入的流量,对其进行分析,并根据输入做出反应。 我从在线教程/代码中尝试了几种设备和技术,遇到了许多问题,但解决了这些问题后,我终于开始使用索尼playstation 3 Pad。设备是通过hid连接的,但这就是我所能做到的。按下任何按钮都不会产生任何输入,到目前为止,我不知道这是什么原因。Stackoverflow、谷歌手册和互联网似乎对此没有任何答案。这是我的密
{
"manifest_version": 2,
"name": "HID Input Analyzer",
"version": "1.0",
"app": {
"background": {
"scripts": [ "background.js" ],
"persistent": true
}
},
"permissions": ["hid", {
"usbDevices": [
{ "vendorId": 1356 , "productId": 616 }
]
}
]
}
background.js
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('mychromeapp.html', {
singleton: true,
id: "Input analyzer"
});
});
mychromeapp.html
<!DOCTYPE html>
<html>
<head>
<title>HID Input Analyzer</title>
</head>
<body>
<input type="text" id="mytext" />
<script src="mychromeapp.js"></script>
</body>
</html>
控制台日志如下所示:
正在尝试连接到HID USB
找到设备ID为23的设备
已连接到具有connectionId:31的HID设备
在分析了我的代码(尤其是最后一个文件)之后,我猜chrome.hid.receive函数并没有从设备中获取任何数据,但我不知道为什么。不幸的是,谷歌的手册做得很差,缺乏好的例子使得编写代码变得很困难。我希望有人能帮助我解决这个问题-我已经坐了3天了:(
Kalreg.尝试在清单文件中写入mychromeapp.js中vendorID和productID的十进制等价物您在receive中的回调函数缺少一个参数。请在chrome.hid页面中查看它,您会注意到有必要在数据中添加一个reportId参数。修复后,它对我有效。我的问题,however正在为每一次新的投票获取新数据。只有在我再次连接它时才会发生这种情况,即使它已经连接。这个问题只适用于Mac和Ubuntu。在windows中,我的工作非常完美。不太可能有帮助,因为该设备已找到并连接。
var MY_HID_VENDOR_ID = 0x09da; // 4660 in hexadecimal!
var MY_HID_PRODUCT_ID = 0x8090;
var DEVICE_INFO = {"vendorId": MY_HID_VENDOR_ID, "productId": MY_HID_PRODUCT_ID };
var connectionId = null;
function arrayBufferToString(array) {
return String.fromCharCode.apply(null, new Uint8Array(array));
}
var myDevicePoll = function() {
var size = 64;
var i = 0;
if (chrome.runtime.lastError) {console.log(chrome.runtime.lastError);}
chrome.hid.receive(connectionId, function(data) {
console.log("::" + connectionId);
if (data != null) {
// Convert Byte into Ascii to follow the format of our device
myText.value = arrayBufferToString(data);
console.log('Data: ' + myText.value);
}
setTimeout(myDevicePoll, 0);
});
}
function initializeHid(pollHid) {
// brackets are empty for purpose because permissions are given in manifest.json
chrome.hid.getDevices({}, function(devices) {
if (!devices || !devices.length) {
console.log('device not found');
if (chrome.runtime.lastError) {console.log(chrome.runtime.lastError);}
return;
}
console.log('Found device with deviceId: ' + devices[0].deviceId);
myHidDevice = devices[0].deviceId;
// Connect to the HID device
chrome.hid.connect(myHidDevice, function(connection) {
console.log('Connected to the HID device with connectionId: ' + connection.connectionId);
connectionId = connection.connectionId;
// Poll the USB HID Interrupt pipe
pollHid();
});
});
}
initializeHid(myDevicePoll);
console.log("Trying to connect to HID USB ...");
var myText = document.getElementById("mytext");
myText.value = "Ready";