2017-09-08 15 views
2

新しいChrome WebUSB APIを試していますが、接続されているデバイスは表示されません。Chrome WebUSB APIはnavigator.usb.getDevices()を使用しているときにデバイスを返しません。

<html> 
    <body> 
     <button onclick="myFunction()">Click me</button> 

     <script> 
      function myFunction() { 
       console.log('Clicked'); 
       navigator.usb.getDevices() 
        .then(devices => { 
        devices.map(device => { 
         console.log('Device:'); 
         console.log(device.productName); 
         console.log(device.manufacturerName); 
        }); 
        }); 
      } 
     </script> 
    </body> 
</html> 

しかし、誰デバイスを持っていない:

は私のWindows 7 PCに接続されている別のUSBデバイスと、例えばこれを試してみました。

私は間違っていますか? どのデバイスでも使用できますか?

ありがとうございました。

答えて

5

あなたのページがデバイスへのアクセス権を要求するまで、navigator.usb.getDevices()は空のリストを返します。 onclickハンドラー内で、代わりにnavigator.usb.requestDevice()を呼び出し、サポートするデバイスのベンダーIDとプロダクトIDを選択します。例を参照してくださいfrom the specification

let button = document.getElementById('request-device'); 
button.addEventListener('click', async() => { 
    let device; 
    try { 
    device = await navigator.usb.requestDevice({ filters: [{ 
     vendorId: 0xABCD, 
     classCode: 0xFF, // vendor-specific 
     protocolCode: 0x01 
    }]}); 
    } catch() { 
    // No device was selected. 
    } 

    if (device !== undefined) { 
    // Add |device| to the UI. 
    } 
}); 
関連する問題