I am using below code to get usb access permission from user when its detected, but it never get detected in android 11. Any help would be highly appreciated
public final String ACTION_USB_PERMISSION = "com.domain.ftd.USB_PERMISSION";
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_USB_PERMISSION)) {
boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED);
Log.e("usb started","really");
if (granted) {
//DO SOMETHING
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usbManager = (UsbManager) getSystemService(this.USB_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_USB_PERMISSION);
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(broadcastReceiver, filter);
}
You need to register the broadcast receiver and call the requestPermission() method to display a dialog that asks users for permission to connect to the device, please refer the official doc and search for keyword "requestPermission()"