Skip to content

LibUsb.open returns Access denied (insufficient permissions) when accessed too slow #81

Open

Description

@tzah4748

Environment:

  • OS: Windows 10
  • Java version:
java --version
openjdk 13.0.2 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
  • Android device: xiaomi-pocophone_f1
  • usb4java version 1.3.0

Bug description
I want to detect my Android device's S/N when Debugging is Enabled on the device settings.
When my program manages to access the LibUsb.open()/device fast enough, i am able to get the S/N.
But if i give a 1 second (or even a bit less) before trying to get the S/N i will get an exception of -
USB error 3: Can't open device Bus 001 Device 060: ID 18d1:4ee7: Access denied (insufficient permissions)

Its worth mentioning that i succeed in getting the S/N only when i replug the device and not when its already plugged in.

Reproduction
DeviceUtils.zip
Added DeviceUtils class as an attachment

public static void main(String[] args) throws UsbException {
        // Get the USB services and dump information about them
        final UsbServices services = UsbHostManager.getUsbServices();
        services.addUsbServicesListener(serviceListener());
        while(true){}
}

    public static UsbServicesAdapter serviceListener() {
        return new UsbServicesAdapter() {

            @Override
            public void usbDeviceAttached(UsbServicesEvent event) {
                // Read the string descriptor indices from the device descriptor.
                // If they are missing then ignore the device.
                handleDevice(event.getUsbDevice());
            }

            private void handleDevice(UsbDevice device) {
                if (device.isUsbHub()) {
                    final UsbHub hub = (UsbHub) device;
                    for (Object child : hub.getAttachedUsbDevices()) {
                        handleDevice((UsbDevice) child);
                    }
                } else {
                    DeviceUtils.Platform platform = DeviceUtils.getPlatform(device.getUsbDeviceDescriptor().idVendor());
                    if (platform == DeviceUtils.Platform.Android) {
                        System.out.println("Device platform - " + platform);
                        boolean isDebuggingEnabled = DeviceUtils.isDebuggingEnabled(device);
                        System.out.println("Is Debugging Enabled? " + isDebuggingEnabled);
                        if (isDebuggingEnabled) {
                            try {
                                TimeUnit.SECONDS.sleep(0);
                                // TimeUnit.SECONDS.sleep(1);
                                System.out.println("S/N: " +device.getSerialNumberString());
                            } catch (UsbException | InterruptedException | UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        };
    }

Expected behavior
A consistent behavior at least, if the problem is really permissions so why can i still get the S/N when i replug the device?
If the problem is not really permissions i would expect to get the S/N and not get this exception!

Many thanks for everyone who can help

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions