I want to connect an USB CDC Device (an FTDI chip or a CD2010 or a custom made) to my Galaxy A12 (Android SDK 30) using the following code (AndroidStudio Chipmunk):
public class MainActivity extends AppCompatActivity {
public static final String ACTION_USB_PERMISSION = "com.example.usbtestapp.USB_PERMISSION";
static final int intentFlags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) ? PendingIntent.FLAG_IMMUTABLE : 0;
static final int MY_CAMERA_PERMISSION_CODE = 100;
private Context ContextofMainAcitivity;
public IntentFilter USBconnectdisconnect_intentFilter;
public Intent GetEXTRA_PERMISSION_GRANTEDIntent;
public PendingIntent current_usbpermissionIntent;
public UsbDevice current_usbdevice;
public UsbManager current_usbManager;
private static final String TAG = "usbtestapp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContextofMainAcitivity = this;
current_usbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
USBconnectdisconnect_intentFilter = new IntentFilter();
USBconnectdisconnect_intentFilter.addAction(ACTION_USB_PERMISSION);
USBconnectdisconnect_intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
USBconnectdisconnect_intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(currentUSBBroasdcastReceiver, USBconnectdisconnect_intentFilter);
GetEXTRA_PERMISSION_GRANTEDIntent = new Intent(ACTION_USB_PERMISSION);
GetEXTRA_PERMISSION_GRANTEDIntent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
Log.v(TAG, "Waiting for USB devices...");
} // create
BroadcastReceiver currentUSBBroasdcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
current_usbdevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
Log.v(TAG,"ACTION_USB_DEVICE_ATTACHED.");
if (current_usbManager.hasPermission(current_usbdevice)) {
Log.v(TAG,"Permission is already granted.");
return;
}
current_usbpermissionIntent = PendingIntent.getBroadcast(ContextofMainAcitivity, 0, GetEXTRA_PERMISSION_GRANTEDIntent , intentFlags);
if (!current_usbManager.hasPermission(current_usbdevice)) {
Log.v(TAG, "Request Permision from User...");
current_usbManager.requestPermission(current_usbdevice, current_usbpermissionIntent);
}
break;
case UsbManager.ACTION_USB_DEVICE_DETACHED:
Log.v(TAG,"ACTION_USB_DEVICE_DETACHED");
break;
case ACTION_USB_PERMISSION:
if (intent.getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED, false) == true) {
Log.v(TAG,"ACTION_USB_PERMISSION - Granted !");
} else {
Log.v(TAG,"ACTION_USB_PERMISSION - Denied");
}
break;
} //switch
} // onReceive
}; // braadcast
} // class
After connecting a FTDI FT232 - CDC Device this happens:
2022-08-07 14:19:44.206 15767-15767/com.social.usbtestapp V/usbtestapp: Waiting for USB devices...
2022-08-07 14:19:50.765 15767-15767/com.social.usbtestapp V/usbtestapp: ACTION_USB_DEVICE_ATTACHED.
2022-08-07 14:19:50.770 15767-15767/com.social.usbtestapp V/usbtestapp: Request Permision from User...
Then a Dialogbox pops open and shows the device string + asks the user for permission – no matters what is selected OK or CANCEL the result is always:
2022-08-07 14:19:52.759 15767-15767/com.social.usbtestapp V/usbtestapp: ACTION_USB_PERMISSION – Denied
I also checked the FTDI example but finally in their internal functionality to my understanding they do pretty much same. I also checked StackOverflow java - UsbDevice requestPermission is denied without showing the request dialog prompt - Stack Overflow but this didn’t solved my issues.
For any unknown reason I got this also ones to work meaning that after selecting OK in the permission dialog the getBooleanExtra( UsbManager.EXTRA_PERMISSION_GRANTED was really true while adding or removing some additionally SDK’s such as Lolipop or Pie but I couldn’t reproduce this anymore – would be glad for any suggestion .