3

Good evening community, I am reaching with the hopes of being educated about the following problem.

My intention with this code is to be able to handle USB permission intents in a receiver registered in a manifest file. The receiver gets USB Attached and detached actions, but not USB permissions when the user either accepts or declines the prompt.

Here is the code for the manifest, receiver and an activity to send the permissions request to the USB manager. And Finally, my target SDK is 28.

Any help is very much appreciated. Thank you very much.

public class BroadcastReceiver extends android.content.BroadcastReceiver{

    public static final String USB_DEVICE_ATTACHED = "android.hardware.usb.action.USB_DEVICE_ATTACHED";
    public static final String USB_DEVICE_DETACHED = "android.hardware.usb.action.USB_DEVICE_DETACHED";
    public static final String USB_PERMISSION ="com.android.example.USB_PERMISSION";

    @Override
    public void onReceive(Context context, Intent intent) {

        Context applicationContext = context.getApplicationContext();

        try{

            if (intent != null) {
                String action = intent.getAction();

                if (!TextUtils.isEmpty(action)) {

                    if (action.equals(USB_DEVICE_ATTACHED) || action.equals(USB_PERMISSION)){

                        UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                        UsbManager usbManager = (UsbManager) applicationContext.getSystemService(Context.USB_SERVICE);

                        if (action.equals(USB_DEVICE_ATTACHED)){

                            if (!usbManager.hasPermission(device)){
                                intent.setAction(USB_PERMISSION);
                                intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
                                intent.setClass(applicationContext, PermissionActivity.class);
                                applicationContext.startActivity(intent);
                                Toast.makeText(applicationContext, "Device Attached.", Toast.LENGTH_LONG).show();
                            }
                            else{
                                Toast.makeText(applicationContext, "Permissions already assigned", Toast.LENGTH_LONG).show();
                            }
                        }
                        else if (action.equals(USB_PERMISSION)){

                            if (usbManager.hasPermission(device)){
                                Toast.makeText(applicationContext, "USB Permissions are granted.", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                    else if (action.equals(USB_DEVICE_DETACHED)) {
                        Toast.makeText(applicationContext, "Device Detached.", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
        catch(Exception e){
            Toast.makeText(applicationContext, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

Here is the activity:

    public class PermissionActivity extends android.support.v7.app.AppCompatActivity {

    public static final String USB_PERMISSION ="com.android.example.USB_PERMISSION";

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Context applicationContext = this.getApplicationContext();

        Intent intent = getIntent();
        if (intent != null )
        {
            if (intent.getAction().equals(USB_PERMISSION)){
                if (!intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false )) {
                    UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    if (device != null) {
                        UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
                        PendingIntent mPermissionIntent = PendingIntent.getBroadcast(applicationContext, 0, new Intent(USB_PERMISSION), 0);
                        mUsbManager.requestPermission(device, mPermissionIntent);

                        Toast.makeText(applicationContext, "Requesting Permission", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}

And finally, the manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.usbtest">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".PermissionActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize"
            android:excludeFromRecents="true"
            android:exported="true"
            android:noHistory="true"
            android:process=":UsbEventReceiverActivityProcess"
            android:taskAffinity="com.example.taskAffinityUsbEventReceiver"
            android:theme="@style/Theme.AppCompat.Translucent">

            <intent-filter>
                <action android:name="com.android.example.USB_PERMISSION"/>
            </intent-filter>
        </activity>

        <receiver android:name=".BroadcastReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
                <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/>
                <action android:name="com.android.example.USB_PERMISSION"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

1 Answer 1

2

I found the problem. Since Android 8.0, there are more restrictions with manifest-declared broadcast receivers and the type of actions that can be received. The USB Permissions action is not part of the limited list of actions that can be received. Here are some links regarding this issue.

https://developer.android.com/guide/components/broadcasts#context-registered-recievers https://developer.android.com/guide/components/broadcast-exceptions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.