Im trying to get a usb connected camera on my android app to work. I've follow te instructions from https://developer.android.com/develop/connectivity/usb/host. However i've encountered two problems: [list] [*] the connection is always null. I can get the device info such as number of interfaces, endpoints, device name etc, but the connection is always null. [*] request permission does not seem to work. I ask for the permission to use USB, but the receiver is never achieved.
package com.example.usbdev; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDeviceConnection; import android.hardware.usb.UsbEndpoint; import android.hardware.usb.UsbInterface; import android.hardware.usb.UsbManager; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import java.util.HashMap; public class MainActivity extends AppCompatActivity { private TextView textView; private Button button; private UsbManager usbManager; private UsbDevice device; private PendingIntent permissionIntent; private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; private Byte[] bytes; private static int TIMEOUT = 0; private boolean forceClaim = true; private final BroadcastReceiver usbReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); textView.append("\nAction: " + action); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { if(device != null){ textView.append("\nPermission granted"); } } else { textView.append("\nPermission denied"); } } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); button = findViewById(R.id.button); permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { registerReceiver(usbReceiver, filter, Context.RECEIVER_NOT_EXPORTED); } button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { executeButton(); } }); } private void executeButton() { usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), PendingIntent.FLAG_IMMUTABLE); HashMap deviceList = usbManager.getDeviceList(); UsbDevice device = null; for (UsbDevice d : deviceList.values()) { device = d; break; // Exit the loop after finding the first device } if (device != null) { // If a device is found, request permission for it textView.append("\nDevice: "+device.getDeviceName()); usbManager.requestPermission(device, permissionIntent); UsbDeviceConnection connection = usbManager.openDevice(device); if (connection != null) { textView.append("\nconnection ok"); } else { textView.append("\nconnection null"); } } else { // No USB devices connected textView.append("\nNo connected devices"); } } } [/list] My manifest: