0

I am requesting the user to grant the permission to establish the connection with the connected usb device but I don't understand how to get the result code indicating success or failure. Using hasPermission not helped. My activity requesting for connection is as below,

MainActivity.java

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usb);
        Button mCheckForDevice = (Button) findViewById(R.id.check);
        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);

        mCheckForDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
                Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
                if(deviceList.size() > 0){
                    UsbDevice device = deviceIterator.next();
                    mUsbManager.requestPermission(device, mPermissionIntent);
    // here I need to print the device info when user accept the permission by clicking on OK button,
    // if CANCEL it has to show a toast with message permission denied by user

                }}
        });
    }

requestPermission is a method that returns void. Document says,

Requests temporary permission for the given package to access the device.
     * This may result in a system dialog being displayed to the user
     * if permission had not already been granted.
     * Success or failure is returned via the {@link android.app.PendingIntent} pi.
     * If successful, this grants the caller permission to access the device only
     * until the device is disconnected.

Can somebody help to find How do I get the Success or failure info returned via the PendingIntent?

Shree
  • 354
  • 2
  • 21
  • 1
    You're passing `this` as the first argument to `PendingIntent.getBroadcast`, which implies that your `MainActivity` implements `BroadcastReceiver` (i.e. it overrides `onReceive`). As the documentation says, _"Success or failure is returned via the `PendingIntent` `pi`. If successful, this grants the caller permission to access the device only until the device is disconnected. The following extras will be added to `pi`: `EXTRA_DEVICE` containing the device passed into this call `EXTRA_PERMISSION_GRANTED` containing boolean indicating whether permission was granted by the user"_. – Michael Feb 12 '18 at 12:30
  • For the pending intent. – Michael Feb 12 '18 at 12:47
  • Thanks for your response Michael, Sorry for making it late. I had used the the receiver. Please check this link for full code https://stackoverflow.com/questions/48700167/send-data-from-android-to-connected-usb-storage-device-in-usb-host-mode Can you tell , what is returned when the dialog of requestPermission is called i.e, when click on Ok or cancel button? – Shree Feb 13 '18 at 08:39
  • _"what is returned when the dialog of requestPermission is called i.e, when click on Ok or cancel button?"_ Re-read my first comment. It's described there. – Michael Feb 13 '18 at 09:19
  • Thank you so much you have saved my time. – Shree Feb 13 '18 at 09:26

0 Answers0