如何枚举连接的USB和Wi-Fi设备在chrome应用程序开发

how to enumerate connected USB and Wi-Fi devices in chrome app development

本文关键字:Wi-Fi 应用程序开发 chrome USB 何枚举 枚举 连接      更新时间:2023-09-26

附示例代码

manifest.json

 {
  "manifest_version": 2,
  "name": "Devices",
  "version": "0.1",
  "minimum_chrome_version": "26",
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
 "permissions": [
 "usb",
    {"usbDevices": [
      {
        "vendorId": 1234,
        "productId": 145
      }
    ]}
  ]
}

background.js

chrome.app.runtime.onLaunched.addListener(function(launchData) {
  chrome.app.window.create('index.html', {id:"fileWin", bounds: {width: 800, height: 500}}, function(win) {
    win.contentWindow.launchData = launchData;
  });
});

index . html

<html>
    <head>
        <title> Devices</title>
        <style>
            button {
                    display: inline-block;
                    background: -webkit-linear-gradient(#F9F9F9 40%, #E3E3E3 70%);
                    background: linear-gradient(#F9F9F9 40%, #E3E3E3 70%);
                    border: 1px solid #999;
                    -webkit-border-radius: 3px;
                    border-radius: 3px;
                    padding: 5px 8px;
                    outline: none;
                    white-space: nowrap;
                    -webkit-user-select: none;
                    user-select: none;
                    cursor: pointer;
                    text-shadow: 1px 1px #fff;
                    font-weight: 700;
                    font-size: 10pt;
                }
            button:not(:disabled):hover {
                    border-color: black;
                }
            button:not(:disabled):active {
                    background: -webkit-linear-gradient(#E3E3E3 40%, #F9F9F9 70%);
                    background: linear-gradient(#E3E3E3 40%, #F9F9F9 70%);
                }
        </style>    
    </head>
    <body>
        <p>Devices<br><br>
            <nav>
                    <button id="usb_dev" onclick="GetUsbDevices()">USB</button>
                    <button id="wifi_dev" ">Wi-Fi</button>
            </nav>
        </p
        <script src="script.js"></script>
    </body>
</html>

script.js

function GetUsbDevices() 
{
    //alert('Clicked USB Device Button');
   console.log("Device could not be found");
}

我正在尝试将连接的设备显示到应用程序窗口。因为我是新手,没有成功。

请帮忙提前感谢,Sarath s

我相信你的两个任务都是不可能实现的(无论如何,没有本机)。

  1. USB枚举是特别不允许的安全目的,我在这里概述:

    这样做的原因是为了防止应用程序通过枚举所有连接的USB设备来识别硬件,这很有可能是一个唯一的集合。

  2. 我不清楚你所说的"WiFi设备"是什么意思。您可以列举具有chrome.system.network API的网络接口,但网络上的任何类型的设备发现将高度依赖于您的意思。

    你不能枚举WiFi网络,你必须实现你自己的,特定的服务发现(只要可能使用chrome.socket.tcp/chrome.socket.udp)。