6

The usual way of accessing a USB device in host mode on Android is as follows:

UsbManager manager = (UsbManager)  getSystemService(Context.USB_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(...);
manager.requestPermission(driver.getDevice(), pi);

This causes the system to show a popup asking for permissions to access USB device - and when user clicks "allow", our app is notified through PendingIntent.

But how to obtain this permission in an instrumented JUnit test (that runs on a phone)?

1) We might try to do this in @Before method - but since the permission request is asynchronous, we cannot just "wait" for user response.

2) In android testing support library there's GrantPermissionRule that can be injected into test with @Rule annotation. But I could not find standard Android rule name for "usb host" permission! (and probably there's no any)

So, how to ask for permission to access USB device in host mode from a JUnit test running on device? Is there a way that works?

0 Answers0