Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Constants](https://developer.android.com/reference/android/hardware/usb/UsbManager#constants) \| [Methods](https://developer.android.com/reference/android/hardware/usb/UsbManager#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/hardware/usb/UsbManager#inhmethods)

# UsbManager

*** ** * ** ***


`
public


class
UsbManager
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.hardware.usb.UsbManager |

<br />

*** ** * ** ***

This class allows you to access the state of USB and communicate with USB devices.
Currently only host mode is supported in the public API.

### Developer Guides

For more information about communicating with USB hardware, read the
[USB developer guide](https://developer.android.com/guide/topics/connectivity/usb).
.   
Restricted for SDK Runtime environment in API level 34.

<br />

## Summary

| ### Constants ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_ACCESSORY_ATTACHED` Activity intent sent when user attaches a USB accessory. |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_ACCESSORY_DETACHED` Broadcast Action: A broadcast for USB accessory detached event. |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_DEVICE_ATTACHED` Activity intent sent when user attaches a USB device. |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_DEVICE_DETACHED` Broadcast Action: A broadcast for USB device detached event. |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_ACCESSORY` Name of extra for `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_ACCESSORY_ATTACHED` and `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_ACCESSORY_DETACHED` broadcasts containing the `https://developer.android.com/reference/android/hardware/usb/UsbAccessory` object for the accessory. |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_DEVICE` Name of extra for `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_DEVICE_ATTACHED` and `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_DEVICE_DETACHED` broadcasts containing the `https://developer.android.com/reference/android/hardware/usb/UsbDevice` object for the device. |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_PERMISSION_GRANTED` Name of extra added to the `https://developer.android.com/reference/android/app/PendingIntent` passed into `https://developer.android.com/reference/android/hardware/usb/UsbManager#requestPermission(android.hardware.usb.UsbDevice,%20android.app.PendingIntent)` or `https://developer.android.com/reference/android/hardware/usb/UsbManager#requestPermission(android.hardware.usb.UsbAccessory,%20android.app.PendingIntent)` containing a boolean value indicating whether the user granted permission or not. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/android/hardware/usb/UsbAccessory` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#getAccessoryList()() ` Returns a list of currently attached USB accessories. |
| ` https://developer.android.com/reference/java/util/HashMap<https://developer.android.com/reference/java/lang/String, https://developer.android.com/reference/android/hardware/usb/UsbDevice>` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#getDeviceList()() ` Returns a HashMap containing all USB devices currently attached. |
| ` boolean` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#hasPermission(android.hardware.usb.UsbAccessory)(https://developer.android.com/reference/android/hardware/usb/UsbAccessory accessory) ` Returns true if the caller has permission to access the accessory. |
| ` boolean` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#hasPermission(android.hardware.usb.UsbDevice)(https://developer.android.com/reference/android/hardware/usb/UsbDevice device) ` Returns true if the caller has permission to access the device. |
| ` https://developer.android.com/reference/android/os/ParcelFileDescriptor` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#openAccessory(android.hardware.usb.UsbAccessory)(https://developer.android.com/reference/android/hardware/usb/UsbAccessory accessory) ` Opens a file descriptor for reading and writing data to the USB accessory. |
| ` https://developer.android.com/reference/java/io/InputStream` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#openAccessoryInputStream(android.hardware.usb.UsbAccessory)(https://developer.android.com/reference/android/hardware/usb/UsbAccessory accessory) ` Opens an input stream for reading from the USB accessory. |
| ` https://developer.android.com/reference/java/io/OutputStream` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#openAccessoryOutputStream(android.hardware.usb.UsbAccessory)(https://developer.android.com/reference/android/hardware/usb/UsbAccessory accessory) ` Opens an output stream for writing to the USB accessory. |
| ` https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#openDevice(android.hardware.usb.UsbDevice)(https://developer.android.com/reference/android/hardware/usb/UsbDevice device) ` Opens the device so it can be used to send and receive data using `https://developer.android.com/reference/android/hardware/usb/UsbRequest`. |
| ` void` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#requestPermission(android.hardware.usb.UsbDevice,%20android.app.PendingIntent)(https://developer.android.com/reference/android/hardware/usb/UsbDevice device, https://developer.android.com/reference/android/app/PendingIntent pi) ` Requests temporary permission for the given package to access the device. |
| ` void` | ` https://developer.android.com/reference/android/hardware/usb/UsbManager#requestPermission(android.hardware.usb.UsbAccessory,%20android.app.PendingIntent)(https://developer.android.com/reference/android/hardware/usb/UsbAccessory accessory, https://developer.android.com/reference/android/app/PendingIntent pi) ` Requests temporary permission for the given package to access the accessory. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||

## Constants

### ACTION_USB_ACCESSORY_ATTACHED

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final String ACTION_USB_ACCESSORY_ATTACHED
```

Activity intent sent when user attaches a USB accessory.

- `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_ACCESSORY` containing the `https://developer.android.com/reference/android/hardware/usb/UsbAccessory` for the attached accessory

<br />

<br />

Constant Value:

"android.hardware.usb.action.USB_ACCESSORY_ATTACHED"


### ACTION_USB_ACCESSORY_DETACHED

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final String ACTION_USB_ACCESSORY_DETACHED
```

Broadcast Action: A broadcast for USB accessory detached event.

This intent is sent when a USB accessory is detached.

- `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_ACCESSORY` containing the `https://developer.android.com/reference/android/hardware/usb/UsbAccessory` for the attached accessory that was detached

<br />

<br />

Constant Value:

"android.hardware.usb.action.USB_ACCESSORY_DETACHED"


### ACTION_USB_DEVICE_ATTACHED

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final String ACTION_USB_DEVICE_ATTACHED
```

Activity intent sent when user attaches a USB device.

This intent is sent when a USB device is attached to the USB bus when in host mode.

- `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_DEVICE` containing the `https://developer.android.com/reference/android/hardware/usb/UsbDevice` for the attached device

<br />

<br />

Constant Value:

"android.hardware.usb.action.USB_DEVICE_ATTACHED"


### ACTION_USB_DEVICE_DETACHED

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final String ACTION_USB_DEVICE_DETACHED
```

Broadcast Action: A broadcast for USB device detached event.

This intent is sent when a USB device is detached from the USB bus when in host mode.

- `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_DEVICE` containing the `https://developer.android.com/reference/android/hardware/usb/UsbDevice` for the detached device

<br />

<br />

Constant Value:

"android.hardware.usb.action.USB_DEVICE_DETACHED"


### EXTRA_ACCESSORY

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final String EXTRA_ACCESSORY
```

Name of extra for `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_ACCESSORY_ATTACHED` and
`https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_ACCESSORY_DETACHED` broadcasts
containing the `https://developer.android.com/reference/android/hardware/usb/UsbAccessory` object for the accessory.

<br />

Constant Value:

"accessory"


### EXTRA_DEVICE

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final String EXTRA_DEVICE
```

Name of extra for `https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_DEVICE_ATTACHED` and
`https://developer.android.com/reference/android/hardware/usb/UsbManager#ACTION_USB_DEVICE_DETACHED` broadcasts
containing the `https://developer.android.com/reference/android/hardware/usb/UsbDevice` object for the device.

<br />

Constant Value:

"device"


### EXTRA_PERMISSION_GRANTED

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static final String EXTRA_PERMISSION_GRANTED
```

Name of extra added to the `https://developer.android.com/reference/android/app/PendingIntent`
passed into `https://developer.android.com/reference/android/hardware/usb/UsbManager#requestPermission(android.hardware.usb.UsbDevice,%20android.app.PendingIntent)`
or `https://developer.android.com/reference/android/hardware/usb/UsbManager#requestPermission(android.hardware.usb.UsbAccessory,%20android.app.PendingIntent)`
containing a boolean value indicating whether the user granted permission or not.

<br />

Constant Value:

"permission"


## Public methods

### getAccessoryList

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public UsbAccessory[] getAccessoryList ()
```

Returns a list of currently attached USB accessories.
(in the current implementation there can be at most one).


Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_ACCESSORY` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/hardware/usb/UsbAccessory` | list of USB accessories, or null if none are attached. <br /> |

### getDeviceList

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public HashMap<String, UsbDevice> getDeviceList ()
```

Returns a HashMap containing all USB devices currently attached.
USB device name is the key for the returned HashMap.
The result will be empty if no devices are attached, or if
USB host mode is inactive or unsupported.


Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_HOST` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/HashMap<https://developer.android.com/reference/java/lang/String, https://developer.android.com/reference/android/hardware/usb/UsbDevice>` | HashMap containing all connected USB devices. <br /> |

### hasPermission

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean hasPermission (UsbAccessory accessory)
```

Returns true if the caller has permission to access the accessory.
Permission might have been granted temporarily via
`https://developer.android.com/reference/android/hardware/usb/UsbManager#requestPermission(android.hardware.usb.UsbAccessory,%20android.app.PendingIntent)` or
by the user choosing the caller as the default application for the accessory.


Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_ACCESSORY` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

| Parameters ||
|---|---|
| `accessory` | `UsbAccessory`: to check permissions for <br /> |

| Returns ||
|---|---|
| `boolean` | true if caller has permission <br /> |

### hasPermission

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean hasPermission (UsbDevice device)
```

Returns true if the caller has permission to access the device.
Permission might have been granted temporarily via
`https://developer.android.com/reference/android/hardware/usb/UsbManager#requestPermission(android.hardware.usb.UsbDevice,%20android.app.PendingIntent)` or
by the user choosing the caller as the default application for the device.
Permission for USB devices of class `https://developer.android.com/reference/android/hardware/usb/UsbConstants#USB_CLASS_VIDEO` for clients that
target SDK `https://developer.android.com/reference/android/os/Build.VERSION_CODES#P` and above can be granted only if they
have additionally the `https://developer.android.com/reference/android/Manifest.permission#CAMERA` permission.


Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_HOST` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

| Parameters ||
|---|---|
| `device` | `UsbDevice`: to check permissions for <br /> |

| Returns ||
|---|---|
| `boolean` | true if caller has permission <br /> |

### openAccessory

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public ParcelFileDescriptor openAccessory (UsbAccessory accessory)
```

Opens a file descriptor for reading and writing data to the USB accessory.

If data is read from the `https://developer.android.com/reference/java/io/InputStream` created from this file descriptor all
data of a USB transfer should be read at once. If only a partial request is read the rest of
the transfer is dropped.

It is strongly recommended to use newer methods instead of this method,
since this method may provide sub-optimal performance on some devices.
This method could potentially face interim performance degradation as well.


Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_ACCESSORY` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

| Parameters ||
|---|---|
| `accessory` | `UsbAccessory`: the USB accessory to open <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/os/ParcelFileDescriptor` | file descriptor, or null if the accessory could not be opened. <br /> |

### openAccessoryInputStream

Added in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public InputStream openAccessoryInputStream (UsbAccessory accessory)
```

Opens an input stream for reading from the USB accessory.
If accessory is not open at this point, accessory will first be opened.

If data is read from the created `https://developer.android.com/reference/java/io/InputStream` all
data of a USB transfer should be read at once. If only a partial request is read, the rest of
the transfer is dropped.

The caller is responsible for ensuring that the returned stream is closed.


Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_ACCESSORY` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

| Parameters ||
|---|---|
| `accessory` | `UsbAccessory`: the USB accessory to open an input stream for. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/InputStream` | input stream to read from given USB accessory. This value cannot be `null`. <br /> |

### openAccessoryOutputStream

Added in [API level 36](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public OutputStream openAccessoryOutputStream (UsbAccessory accessory)
```

Opens an output stream for writing to the USB accessory.
If accessory is not open at this point, accessory will first be opened.

The caller is responsible for ensuring that the returned stream is closed.


Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_ACCESSORY` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

| Parameters ||
|---|---|
| `accessory` | `UsbAccessory`: the USB accessory to open an output stream for. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/OutputStream` | output stream to write to given USB accessory. This value cannot be `null`. <br /> |

### openDevice

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public UsbDeviceConnection openDevice (UsbDevice device)
```

Opens the device so it can be used to send and receive
data using `https://developer.android.com/reference/android/hardware/usb/UsbRequest`.


Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_HOST` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

| Parameters ||
|---|---|
| `device` | `UsbDevice`: the device to open <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection` | a `https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection`, or `null` if open failed <br /> |

### requestPermission

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void requestPermission (UsbDevice device, 
                PendingIntent pi)
```

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 `https://developer.android.com/reference/android/app/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:

- `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_DEVICE` containing the device passed into this call
- `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_PERMISSION_GRANTED` containing boolean indicating whether permission was granted by the user

Permission for USB devices of class `https://developer.android.com/reference/android/hardware/usb/UsbConstants#USB_CLASS_VIDEO` for clients that target SDK `https://developer.android.com/reference/android/os/Build.VERSION_CODES#P` and above can be granted only if they have additionally the `https://developer.android.com/reference/android/Manifest.permission#CAMERA` permission.   
Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_HOST` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

<br />

| Parameters ||
|---|---|
| `device` | `UsbDevice`: to request permissions for <br /> |
| `pi` | `PendingIntent`: PendingIntent for returning result <br /> |

### requestPermission

Added in [API level 12](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void requestPermission (UsbAccessory accessory, 
                PendingIntent pi)
```

Requests temporary permission for the given package to access the accessory.
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 `https://developer.android.com/reference/android/app/PendingIntent` pi.
If successful, this grants the caller permission to access the accessory only
until the device is disconnected.

The following extras will be added to pi:

- `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_ACCESSORY` containing the accessory passed into this call
- `https://developer.android.com/reference/android/hardware/usb/UsbManager#EXTRA_PERMISSION_GRANTED` containing boolean indicating whether permission was granted by the user

.   
Requires the `https://developer.android.com/reference/android/content/pm/PackageManager#FEATURE_USB_ACCESSORY` feature which can be detected using `https://developer.android.com/reference/android/content/pm/PackageManager#hasSystemFeature(java.lang.String)`.

<br />

<br />

| Parameters ||
|---|---|
| `accessory` | `UsbAccessory`: to request permissions for <br /> |
| `pi` | `PendingIntent`: PendingIntent for returning result <br /> |