saki4510t / UVCCamera

library and sample to access to UVC web camera on non-rooted Android device
2.99k stars 1.2k forks source link

Crash on opening the app when USB camera is connected. #544

Open ulohani90 opened 4 years ago

ulohani90 commented 4 years ago

I am using this library to get USB Camera stream. But I am facing a very wierd problem. I setup the whole code base in the similar way as the examples given in this project. I copied this class in my project. In sample app it seems to work fine.

https://github.com/saki4510t/UVCCamera/blob/master/usbCameraTest7/src/main/java/com/serenegiant/usbcameratest7/MainActivity.java

But in my case when I open the app with usb camera connected to the device, the app crashes with the following exception. 2020-02-19 19:15:20.825 30697-30736/com.android.testapp E/AndroidRuntime: FATAL EXCEPTION: USBMonitor Process: com.android.testapp, PID: 30697 java.lang.SecurityException: User has not given 10485/com.android.testapp permission to access device /dev/bus/usb/001/004 at android.os.Parcel.createException(Parcel.java:2071) at android.os.Parcel.readException(Parcel.java:2039) at android.os.Parcel.readException(Parcel.java:1987) at android.hardware.usb.IUsbSerialReader$Stub$Proxy.getSerial(IUsbSerialReader.java:123) at android.hardware.usb.UsbDevice.getSerialNumber(UsbDevice.java:143) at com.serenegiant.usb.USBMonitor.getDeviceKeyName(USBMonitor.java:663) at com.serenegiant.usb.USBMonitor.getDeviceKey(USBMonitor.java:696) at com.serenegiant.usb.USBMonitor.updatePermission(USBMonitor.java:399) at com.serenegiant.usb.USBMonitor.hasPermission(USBMonitor.java:389) at com.serenegiant.usb.USBMonitor$2.run(USBMonitor.java:526) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.os.HandlerThread.run(HandlerThread.java:67) Caused by: android.os.RemoteException: Remote stack trace: at com.android.server.usb.UsbUserSettingsManager.checkPermission(UsbUserSettingsManager.java:177) at com.android.server.usb.UsbSerialReader.getSerial(UsbSerialReader.java:96) at android.hardware.usb.IUsbSerialReader$Stub.onTransact(IUsbSerialReader.java:84) at android.os.Binder.execTransactInternal(Binder.java:1021) at android.os.Binder.execTransact(Binder.java:994)

I am not able to even see the Camera Dialog also. It crashes as soon as it opens. But it is working fine in this project. What can be the issue over here? Any help would be appreciated.

rgparask commented 4 years ago

What version of Android is your app targeting, and what version does your device run? Starting from Android 9 (API level 28) the camera permission is required to access uvc usb devices. Additionally, due to the way the Android 10 implementation of UsbUserSettingsManager requests the permission, it does not provide the correct process id, always failing to acquire the permission.

Although it will prevent you from uploading on Google Play, try changing the target sdk of your app to 27 to check if the issue persists.

See here a related issue and the corresponding issue report on Google issue tracker

metuuu commented 3 years ago

Device serial number can't be read without usb device access permission when targetSdkVersion is set to higher than 28. https://developer.android.com/reference/android/hardware/usb/UsbDevice#getSerialNumber()

Could you @saki4510t patch the USBMonitor.java file?https://github.com/saki4510t/UVCCamera/blob/master/libuvccamera/src/main/java/com/serenegiant/usb/USBMonitor.java#L663

Either remove the line 663 or edit it like this:

try { sb.append(device.getSerialNumber());  sb.append("#"); } // API >= 21 & targetSdkVersion has to be <= 28
catch(SecurityException ignore) {}

CAMERA permission is also required when targeting sdk version > 27

You can try if you can get the permission for the USB camera with saki's UVCPermissionTest app. https://github.com/saki4510t/UVCPermissionTest