2

Getting a NullPointerException on usbManager.requestPermission()

try
{
    UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    manager.requestPermission(device, PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT));
}
catch (Exception e)
{
    e.printStackTrace();
}

Error :

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
at android.os.Parcel.createException(Parcel.java:1956)
        at android.os.Parcel.readException(Parcel.java:1918)
at android.os.Parcel.readException(Parcel.java:1868)
at android.hardware.usb.IUsbManager$Stub$Proxy.requestDevicePermission(IUsbManager.java:682)
at android.hardware.usb.UsbManager.requestPermission(UsbManager.java:554)

Which string is Android trying to compare? How can I bypass this error?

Other info:

  • Android 9
  • Problem does not appears on every device
  • UsbDevice[mName=/dev/bus/usb/001/008,mVendorId=1947,mProductId=40,mClass=2,mSubclass=0,mProtocol=0,mManufacturerName=null,mProductName=null,mVersion=0.00,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=128,mMaxPower=71,mInterfaces=[ UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=2,mSubclass=2,mProtocol=1,mEndpoints=[ UsbEndpoint[mAddress=131,mAttributes=3,mMaxPacketSize=8,mInterval=4]] W/System.err: UsbInterface[mId=1,mAlternateSetting=0,mName=null,mClass=10,mSubclass=0,mProtocol=0,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=2,mMaxPacketSize=64,mInterval=0] UsbEndpoint[mAddress=2,mAttributes=2,mMaxPacketSize=64,mInterval=0]]]]
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
MatOfLink
  • 31
  • 5
  • Try to follow [this](https://stackoverflow.com/questions/28137280/how-to-grant-permission-to-open-usb-device-with-usb-manager-opendevice-always) answer as close as possible. Does it work then? – MonsterDruide1 Feb 08 '21 at 16:38
  • I tried to read the post you mentionned as focused as I could, but : - The answer is dated at 2017 and asks to use "grantPermission", which you can no longer use since the new Android policy about permissions - The answer shows a code example, in which he doesn't event use "grantPermission" - The code example uses "requestPermission", which is exactly what I use in my code, and is exactly the source of the problem. So my answer is : No it doesn't work ;) – MatOfLink Feb 09 '21 at 08:09

1 Answers1

0

Did you double check if getManager() delivers a proper instance? Looks like the methods you call on UsbManager manager are throwing the exception.

Maybe use debug to find if there is an instance retreived.

Quadrivics
  • 181
  • 2
  • 10