Added in API level 12

UsbManager

open class UsbManager
kotlin.Any
   ↳ android.hardware.usb.UsbManager

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.

Summary

Constants
static String

Activity intent sent when user attaches a USB accessory.

static String

Broadcast Action: A broadcast for USB accessory detached event.

static String

Activity intent sent when user attaches a USB device.

static String

Broadcast Action: A broadcast for USB device detached event.

static String

Name of extra for ACTION_USB_ACCESSORY_ATTACHED and ACTION_USB_ACCESSORY_DETACHED broadcasts containing the UsbAccessory object for the accessory.

static String

Name of extra for ACTION_USB_DEVICE_ATTACHED and ACTION_USB_DEVICE_DETACHED broadcasts containing the UsbDevice object for the device.

static String

Name of extra added to the android.app.PendingIntent passed into requestPermission(android.hardware.usb.UsbDevice,android.app.PendingIntent) or requestPermission(android.hardware.usb.UsbAccessory,android.app.PendingIntent) containing a boolean value indicating whether the user granted permission or not.

Public methods
open Array<UsbAccessory!>!

Returns a list of currently attached USB accessories.

open HashMap<String!, UsbDevice!>!

Returns a HashMap containing all USB devices currently attached.

open Boolean

Returns true if the caller has permission to access the device.

open Boolean

Returns true if the caller has permission to access the accessory.

open ParcelFileDescriptor!

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

open UsbDeviceConnection!

Opens the device so it can be used to send and receive data using android.hardware.usb.UsbRequest.

open Unit

Requests temporary permission for the given package to access the device.

open Unit

Requests temporary permission for the given package to access the accessory.

Constants

ACTION_USB_ACCESSORY_ATTACHED

Added in API level 12
static val ACTION_USB_ACCESSORY_ATTACHED: String

Activity intent sent when user attaches a USB accessory.

Value: "android.hardware.usb.action.USB_ACCESSORY_ATTACHED"

ACTION_USB_ACCESSORY_DETACHED

Added in API level 12
static val ACTION_USB_ACCESSORY_DETACHED: String

Broadcast Action: A broadcast for USB accessory detached event. This intent is sent when a USB accessory is detached.

Value: "android.hardware.usb.action.USB_ACCESSORY_DETACHED"

ACTION_USB_DEVICE_ATTACHED

Added in API level 12
static val ACTION_USB_DEVICE_ATTACHED: String

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.

Value: "android.hardware.usb.action.USB_DEVICE_ATTACHED"

ACTION_USB_DEVICE_DETACHED

Added in API level 12
static val ACTION_USB_DEVICE_DETACHED: String

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.

Value: "android.hardware.usb.action.USB_DEVICE_DETACHED"

EXTRA_ACCESSORY

Added in API level 12
static val EXTRA_ACCESSORY: String

Name of extra for ACTION_USB_ACCESSORY_ATTACHED and ACTION_USB_ACCESSORY_DETACHED broadcasts containing the UsbAccessory object for the accessory.

Value: "accessory"

EXTRA_DEVICE

Added in API level 12
static val EXTRA_DEVICE: String

Name of extra for ACTION_USB_DEVICE_ATTACHED and ACTION_USB_DEVICE_DETACHED broadcasts containing the UsbDevice object for the device.

Value: "device"

EXTRA_PERMISSION_GRANTED

Added in API level 12
static val EXTRA_PERMISSION_GRANTED: String

Name of extra added to the android.app.PendingIntent passed into requestPermission(android.hardware.usb.UsbDevice,android.app.PendingIntent) or requestPermission(android.hardware.usb.UsbAccessory,android.app.PendingIntent) containing a boolean value indicating whether the user granted permission or not.

Value: "permission"

Public methods

getAccessoryList

Added in API level 12
open fun getAccessoryList(): Array<UsbAccessory!>!

Returns a list of currently attached USB accessories. (in the current implementation there can be at most one)
Requires the PackageManager#FEATURE_USB_ACCESSORY feature which can be detected using PackageManager.hasSystemFeature(String).

Return
Array<UsbAccessory!>! list of USB accessories, or null if none are attached.

getDeviceList

Added in API level 12
open fun getDeviceList(): HashMap<String!, UsbDevice!>!

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 PackageManager#FEATURE_USB_HOST feature which can be detected using PackageManager.hasSystemFeature(String).

Return
HashMap<String!, UsbDevice!>! HashMap containing all connected USB devices.

hasPermission

Added in API level 12
open fun hasPermission(device: UsbDevice!): Boolean

Returns true if the caller has permission to access the device. Permission might have been granted temporarily via requestPermission(android.hardware.usb.UsbDevice,android.app.PendingIntent) or by the user choosing the caller as the default application for the device. Permission for USB devices of class UsbConstants#USB_CLASS_VIDEO for clients that target SDK android.os.Build.VERSION_CODES#P and above can be granted only if they have additionally the android.Manifest.permission#CAMERA permission.
Requires the PackageManager#FEATURE_USB_HOST feature which can be detected using PackageManager.hasSystemFeature(String).

Parameters
device UsbDevice!: to check permissions for
Return
Boolean true if caller has permission

hasPermission

Added in API level 12
open fun hasPermission(accessory: UsbAccessory!): Boolean

Returns true if the caller has permission to access the accessory. Permission might have been granted temporarily via requestPermission(android.hardware.usb.UsbAccessory,android.app.PendingIntent) or by the user choosing the caller as the default application for the accessory.
Requires the PackageManager#FEATURE_USB_ACCESSORY feature which can be detected using PackageManager.hasSystemFeature(String).

Parameters
accessory UsbAccessory!: to check permissions for
Return
Boolean true if caller has permission

openAccessory

Added in API level 12
open fun openAccessory(accessory: UsbAccessory!): ParcelFileDescriptor!

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

If data is read from the 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.
Requires the PackageManager#FEATURE_USB_ACCESSORY feature which can be detected using PackageManager.hasSystemFeature(String).

Parameters
accessory UsbAccessory!: the USB accessory to open
Return
ParcelFileDescriptor! file descriptor, or null if the accessory could not be opened.

openDevice

Added in API level 12
open fun openDevice(device: UsbDevice!): UsbDeviceConnection!

Opens the device so it can be used to send and receive data using android.hardware.usb.UsbRequest.
Requires the PackageManager#FEATURE_USB_HOST feature which can be detected using PackageManager.hasSystemFeature(String).

Parameters
device UsbDevice!: the device to open
Return
UsbDeviceConnection! a UsbDeviceConnection, or null if open failed

requestPermission

Added in API level 12
open fun requestPermission(
    device: UsbDevice!,
    pi: PendingIntent!
): Unit

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 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:

Permission for USB devices of class UsbConstants#USB_CLASS_VIDEO for clients that target SDK android.os.Build.VERSION_CODES#P and above can be granted only if they have additionally the android.Manifest.permission#CAMERA permission.
Requires the PackageManager#FEATURE_USB_HOST feature which can be detected using PackageManager.hasSystemFeature(String).

Parameters
device UsbDevice!: to request permissions for
pi PendingIntent!: PendingIntent for returning result

requestPermission

Added in API level 12
open fun requestPermission(
    accessory: UsbAccessory!,
    pi: PendingIntent!
): Unit

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 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:


Requires the PackageManager#FEATURE_USB_ACCESSORY feature which can be detected using PackageManager.hasSystemFeature(String).

Parameters
accessory UsbAccessory!: to request permissions for
pi PendingIntent!: PendingIntent for returning result