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?