1

I am working on a uvc camera function, and can normally request permissions and use them under android10.0 system, but in android10.0 system, I directly receive permission request failure, and there is no pop-up permission request prompt popup window, I also tried to add intent-filter and meta-data to the activity tag of the manifest file. When the USB was inserted, I directly selected the relevant application and granted permissions, but the USB camera still did not work properly,code as follows

<!--    AndroidManifrest.xml-->
<uses-feature android:name="android.hardware.usb.host" />

<!--    Activity-->
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    //定义广播标识
    private static final String ACTION_USB_PERMISSION = "com.tangtang.uvctest.USB_PERMISSION";
    //注册接收广播 获取到usb 权限时系统广播
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                            //成功
                            toastMsg("permission succ for device " + device);
                        }
                    } else {
                        //The code will execute directly when requesting permissions
                        toastMsg("permission denied for device " + device);
                    }
                }
            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},0);
        }
        findViewById(R.id.test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestPermission1();
            }
        });
    }

    private void requestPermission1() {
        //获取service
        UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        //获取设备列表(一般只有一个,usb 口只有一个)
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        //绑定广播
        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
        IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
        //注册接收广播
        registerReceiver(mUsbReceiver, filter);
        //枚举设备
        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            //请求权限
            manager.requestPermission(device, mPermissionIntent);
        }

    }

    private void toastMsg(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUsbReceiver);
    }
}

tangtang
  • 11
  • 2
  • This issue you facing on an android device or non-rooted android device. if you are facing for non rooted then you can try this https://github.com/saki4510t/UVCCamera – Sandeep Sankla Mar 04 '20 at 04:11
  • My project uses this library, because there is a problem, so I wrote this example, the problem of the library is the same as the example – tangtang Mar 04 '20 at 05:30
  • 1
    I checked the issues of saki4510t / UVCCamera on github, and someone encountered the same problem. I solved the problem by changing targetSdkVersion to 27, but I do n’t understand why there would be a problem with targetSdkVersion >= 28. The official document does not describe it. – tangtang Mar 04 '20 at 07:19

2 Answers2

0

CAMERA permission is 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

Metu
  • 1,033
  • 1
  • 13
  • 20
0

This is fixed for my Samsung note10 plus 5G smartphone with software update . Android has released fix for this with software update.(It depends on device manufacturer if your device receives that update) .Check this google issue for more details https://issuetracker.google.com/issues/145082934.