Failed to connect to USB on chromebook: Permission

2019-08-12 15:43发布

问题:

I'm writing app for chromebook that has to communicate with specific USB device. The device is present in the list of found devices, that comes from callback:

   var VENDOR_ID = 5824, PRODUCT_ID = 1159;

   document.getElementById("request-permission").addEventListener('click', function() {
    chrome.permissions.request({
            permissions: [{
                'usbDevices': [{
                    'vendorId': VENDOR_ID,
                    "productId": PRODUCT_ID
                }]
            }]
        },
        function(result) {
            if (result) {
                console.log('App was granted the "usbDevices" permission.');
                getDevices();
            }
        }
    });
});


function getDevices() {
    chrome.usb.getDevices({
        vendorId: VENDOR_ID,
        productId: PRODUCT_ID
    }, function(devices) {
        if (chrome.runtime.lastError !== undefined) {
            console.warn('chrome.usb.getDevices error: ' + chrome.runtime.lastError.message);
            return;
        }
        if (devices) {
            if (devices.length > 0) {
                for (var device of devices)
                    openUsbDevice(device);
            }
        }
    }); 
}

So I can see my device successfully, but when I'm trying to open it , it fails:

chrome.usb.openDevice(device, function(handle) {
        if (chrome.runtime.lastError != undefined) {
          console.log('Failed to open device: '+chrome.runtime.lastError.message);
        } else {
          populateDeviceInfo(handle, function () {
              chrome.usb.closeDevice(handle);
            });
        }
      });

And I'm receiving error in console: Failed to open device: Permission to access device was denied

I have all required usb permissions declared in manifest.js:

  "permissions" : [
    "usb"
  ],
  "optional_permissions" : [{
    "usbDevices" : [
        {
          "productId" : 1159,
          "vendorId" : 5824
        }
      ]
  }],

I've also tried another api functions, like chrome.usb.findDevices and chrome.usb.requestAccess , but result is the same. At same time my nexus 7 device e.g. is recognised successfully via usb. Any ideas why that can be or guesses how to make my usb device become accessible? I've faced with this only on Acer Chromebook c7 (Chrome OS v.44) and on Mac I hadn't such issue.

回答1:

Open chrome://system and expand the "syslog" section. You will find a series of messages from "permission_broker" for each device you try to open and the rule that caused it to deny access.

The most common reason is "DenyClaimedUsbDeviceRule" which denies access to devices that are claimed by another application or a kernel driver. Access is denied in this case for security reasons. Chrome OS does not want a Chrome App to be able to override any policies normally enforced by the kernel for devices that it has support for. You should use a higher level API (such as chrome.fileSystem for storage devices or navigator.getUserMedia for webcams and audio adapters).