使用chromeapi无法从hid设备获取任何输入数据

cant get any input data from hid device with chrome api

本文关键字:获取 任何 输入 数据 hid chromeapi 使用      更新时间:2023-09-26

我正在制作一个小型应用程序,它将使用chrome封装的应用程序从usb设备中获取输入数据。想法是,当我按下usb设备上的按钮时,它会接收传入的流量,分析它,并根据输入做出反应。我尝试了一些在线教程/代码中的设备和技术,遇到了很多问题,但在解决了这些问题后,我终于开始使用索尼playstation 3平板电脑。设备是通过hid连接的,但这就是我所能完成的。按下任何按钮都不会产生任何输入,到目前为止,我不知道这是什么原因。Stackoverflow、谷歌手册和互联网似乎对此没有任何答案。这是我的代码:

manifest.json:

{
      "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"
  });
});

mychromeap.html

<!DOCTYPE html>
<html>
  <head>
    <title>HID Input Analyzer</title>
  </head>
  <body>
        <input type="text" id="mytext" />
    <script src="mychromeapp.js"></script>
  </body>
</html>

mychromeap.js

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";

控制台日志如下:

正在尝试连接到HID USB。。。

找到设备ID为23 的设备

连接到HID设备,连接ID:31

在分析了我的代码(尤其是最后一个文件)后,我想chrome.hid.rece函数没有从设备中获取任何数据,但我不知道为什么。不幸的是,谷歌的手册制作得很差,缺乏好的例子使它很难编码。我希望有人能帮我解决这个问题——我已经坐了3天了:(

Kalreg。

receive中的回调函数缺少一个参数。在chrome.hid页面中查看它,您会注意到它需要一个reportId参数和数据。修复后,它对我起了作用。然而,我的问题是,每次新的民意调查都会得到新的数据。只有当我再次连接它时才会发生这种情况,即使它已经连接好了。这个问题只适用于Mac和Ubuntu。在窗户上,我的效果很好。

尝试在清单文件中写入mychromeap.js中vendorID和productID的十进制等价物