0

In my Android project I'm trying to communicate a tablet with an Arduino device through the USB port, and it works for version 5.1 (API 22: Lollipop) but it doesn't for 6.0 (API 23: Marshmallow) because of the changes in permissions implemented in this Android version.

I've set my target SDK version to API 22, but still doesn't work.

In My Activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    [...]

    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(
                ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);
    filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);

    UsbAccessory[] accessories = mUsbManager.getAccessoryList();
        UsbAccessory accessory = (accessories == null ? null : accessories[0]);
        if (accessory != null) {
            if (mUsbManager.hasPermission(accessory)) {
                openAccessory(accessory);
            } else {
                synchronized (mUsbReceiver) {
                    if (!mPermissionRequestPending) {
                        mUsbManager.requestPermission(accessory,
                                mPermissionIntent);
                        mPermissionRequestPending = true;
                    }
                }
            }
        }
    }

What's wrong? What I'm missing?

Jérôme
  • 13,328
  • 7
  • 56
  • 106

2 Answers2

1

Finally it was that Arduino ADK (last update 2012) do not work for Android API versions higher than 6.0 (included), no matter how many permissions I did ask for. I had to change my strategy and make my Android device the host and the Arduino the accessory.

Problem solved, but the Arduino does not charge the tablet as was intended.

0

Try to grant permission.

String permission = android.Manifest.permission.YOUR_PERMISSION;

private static void requestPermission(Activity activity) {

    if (hasPermission(permission)) return;

    ActivityCompat.requestPermissions(activity, new String[]{permission}, REQUEST_CODE);
}

public static boolean hasPermission() {
    return ContextCompat.checkSelfPermission(SupAndroid.appContext, permission) == PERMISSION_GRANTED;
}

Waiting for callback in onActivityResult of your Activity

Zeon
  • 535
  • 8
  • 23
  • I'm trying to do that, but I don't know which permissions would allow me to transfer and receive data through the USB port. – Oriol Trujillo Jul 11 '18 at 10:46
  • Finally it wasn't that, because I was using Arduino as host, therefore the permission requests should be included in the ADK module. But thanks anyway, requesting permissions in Android inspired the final solution. – Oriol Trujillo Jul 30 '18 at 06:58