public class UsbManager extends Object
You can obtain an instance of this class by calling
Context.getSystemService()
.
For more information about communicating with USB hardware, read the USB developer guide.
Modifier and Type | Field and Description |
---|---|
static String |
ACTION_USB_ACCESSORY_ATTACHED
Broadcast Action: A broadcast for USB accessory attached event.
|
static String |
ACTION_USB_ACCESSORY_DETACHED
Broadcast Action: A broadcast for USB accessory detached event.
|
static String |
ACTION_USB_DEVICE_ATTACHED
Broadcast Action: A broadcast for USB device attached event.
|
static String |
ACTION_USB_DEVICE_DETACHED
Broadcast Action: A broadcast for USB device detached event.
|
static String |
ACTION_USB_PORT_CHANGED
Broadcast Action: A broadcast for USB port changes.
|
static String |
ACTION_USB_STATE
Broadcast Action: A sticky broadcast for USB state change events when in device mode.
|
static String |
EXTRA_ACCESSORY
Name of extra for
ACTION_USB_ACCESSORY_ATTACHED and
ACTION_USB_ACCESSORY_DETACHED broadcasts
containing the UsbAccessory object for the accessory. |
static String |
EXTRA_DEVICE
Name of extra for
ACTION_USB_DEVICE_ATTACHED and
ACTION_USB_DEVICE_DETACHED broadcasts
containing the UsbDevice object for the device. |
static String |
EXTRA_PERMISSION_GRANTED
Name of extra added to the
PendingIntent
passed into requestPermission(UsbDevice, PendingIntent)
or requestPermission(UsbAccessory, PendingIntent)
containing a boolean value indicating whether the user granted permission or not. |
static String |
EXTRA_PORT
Name of extra for
ACTION_USB_PORT_CHANGED
containing the UsbPort object for the port. |
static String |
EXTRA_PORT_STATUS
Name of extra for
ACTION_USB_PORT_CHANGED
containing the UsbPortStatus object for the port, or null if the port
was removed. |
static String |
USB_CONFIGURED
Boolean extra indicating whether USB is configured.
|
static String |
USB_CONNECTED
Boolean extra indicating whether USB is connected or disconnected.
|
static String |
USB_DATA_UNLOCKED
Boolean extra indicating whether confidential user data, such as photos, should be
made available on the USB connection.
|
static String |
USB_FUNCTION_ACCESSORY
Name of the Accessory USB function.
|
static String |
USB_FUNCTION_ADB
Name of the adb USB function.
|
static String |
USB_FUNCTION_AUDIO_SOURCE
Name of the audio source USB function.
|
static String |
USB_FUNCTION_MIDI
Name of the MIDI USB function.
|
static String |
USB_FUNCTION_MTP
Name of the MTP USB function.
|
static String |
USB_FUNCTION_NONE
A placeholder indicating that no USB function is being specified.
|
static String |
USB_FUNCTION_PTP
Name of the PTP USB function.
|
static String |
USB_FUNCTION_RNDIS
Name of the RNDIS ethernet USB function.
|
static String |
USB_HOST_CONNECTED
Boolean extra indicating whether USB is connected or disconnected as host.
|
Constructor and Description |
---|
UsbManager(Context context,
IUsbManager service) |
Modifier and Type | Method and Description |
---|---|
static String |
addFunction(String functions,
String function) |
static boolean |
containsFunction(String functions,
String function) |
UsbAccessory[] |
getAccessoryList()
Returns a list of currently attached USB accessories.
|
HashMap<String,UsbDevice> |
getDeviceList()
Returns a HashMap containing all USB devices currently attached.
|
UsbPort[] |
getPorts()
Returns a list of physical USB ports on the device.
|
UsbPortStatus |
getPortStatus(UsbPort port)
Gets the status of the specified USB port.
|
void |
grantPermission(UsbDevice device)
Grants permission for USB device without showing system dialog.
|
void |
grantPermission(UsbDevice device,
String packageName)
Grants permission to specified package for USB device without showing system dialog.
|
boolean |
hasPermission(UsbAccessory accessory)
Returns true if the caller has permission to access the accessory.
|
boolean |
hasPermission(UsbDevice device)
Returns true if the caller has permission to access the device.
|
boolean |
isFunctionEnabled(String function)
Returns true if the specified USB function is currently enabled when in device mode.
|
ParcelFileDescriptor |
openAccessory(UsbAccessory accessory)
Opens a file descriptor for reading and writing data to the USB accessory.
|
UsbDeviceConnection |
openDevice(UsbDevice device)
Opens the device so it can be used to send and receive
data using
UsbRequest . |
static String |
removeFunction(String functions,
String function) |
void |
requestPermission(UsbAccessory accessory,
PendingIntent pi)
Requests temporary permission for the given package to access the accessory.
|
void |
requestPermission(UsbDevice device,
PendingIntent pi)
Requests temporary permission for the given package to access the device.
|
void |
setCurrentFunction(String function)
Sets the current USB function when in device mode.
|
void |
setPortRoles(UsbPort port,
int powerRole,
int dataRole)
Sets the desired role combination of the port.
|
void |
setUsbDataUnlocked(boolean unlocked)
Sets whether USB data (for example, MTP exposed pictures) should be made available
on the USB connection when in device mode.
|
public static final String ACTION_USB_STATE
USB_CONNECTED
boolean indicating whether USB is connected or disconnected.
USB_HOST_CONNECTED
boolean indicating whether USB is connected or
disconnected as host.
USB_CONFIGURED
boolean indicating whether USB is configured.
currently zero if not configured, one for configured.
USB_FUNCTION_ADB
boolean extra indicating whether the
adb function is enabled
USB_FUNCTION_RNDIS
boolean extra indicating whether the
RNDIS ethernet function is enabled
USB_FUNCTION_MTP
boolean extra indicating whether the
MTP function is enabled
USB_FUNCTION_PTP
boolean extra indicating whether the
PTP function is enabled
USB_FUNCTION_PTP
boolean extra indicating whether the
accessory function is enabled
USB_FUNCTION_AUDIO_SOURCE
boolean extra indicating whether the
audio source function is enabled
USB_FUNCTION_MIDI
boolean extra indicating whether the
MIDI function is enabled
public static final String ACTION_USB_PORT_CHANGED
EXTRA_PORT
containing the UsbPort
for the port.
EXTRA_PORT_STATUS
containing the UsbPortStatus
for the port, or null if the port has been removed
public static final String ACTION_USB_DEVICE_ATTACHED
EXTRA_DEVICE
containing the UsbDevice
for the attached device
public static final String ACTION_USB_DEVICE_DETACHED
EXTRA_DEVICE
containing the UsbDevice
for the detached device
public static final String ACTION_USB_ACCESSORY_ATTACHED
EXTRA_ACCESSORY
containing the UsbAccessory
for the attached accessory
public static final String ACTION_USB_ACCESSORY_DETACHED
EXTRA_ACCESSORY
containing the UsbAccessory
for the attached accessory that was detached
public static final String USB_CONNECTED
ACTION_USB_STATE
broadcast.
public static final String USB_HOST_CONNECTED
ACTION_USB_STATE
broadcast.
public static final String USB_CONFIGURED
ACTION_USB_STATE
broadcast.
public static final String USB_DATA_UNLOCKED
ACTION_USB_STATE
broadcast.
public static final String USB_FUNCTION_NONE
setCurrentFunction(String)
.
public static final String USB_FUNCTION_ADB
ACTION_USB_STATE
broadcast
public static final String USB_FUNCTION_RNDIS
ACTION_USB_STATE
broadcast
public static final String USB_FUNCTION_MTP
ACTION_USB_STATE
broadcast
public static final String USB_FUNCTION_PTP
ACTION_USB_STATE
broadcast
public static final String USB_FUNCTION_AUDIO_SOURCE
ACTION_USB_STATE
broadcast
public static final String USB_FUNCTION_MIDI
ACTION_USB_STATE
broadcast
public static final String USB_FUNCTION_ACCESSORY
ACTION_USB_STATE
broadcast
public static final String EXTRA_PORT
ACTION_USB_PORT_CHANGED
containing the UsbPort
object for the port.public static final String EXTRA_PORT_STATUS
ACTION_USB_PORT_CHANGED
containing the UsbPortStatus
object for the port, or null if the port
was removed.public static final String EXTRA_DEVICE
ACTION_USB_DEVICE_ATTACHED
and
ACTION_USB_DEVICE_DETACHED
broadcasts
containing the UsbDevice
object for the device.public static final String EXTRA_ACCESSORY
ACTION_USB_ACCESSORY_ATTACHED
and
ACTION_USB_ACCESSORY_DETACHED
broadcasts
containing the UsbAccessory
object for the accessory.public static final String EXTRA_PERMISSION_GRANTED
PendingIntent
passed into requestPermission(UsbDevice, PendingIntent)
or requestPermission(UsbAccessory, PendingIntent)
containing a boolean value indicating whether the user granted permission or not.public UsbManager(Context context, IUsbManager service)
public HashMap<String,UsbDevice> getDeviceList()
public UsbDeviceConnection openDevice(UsbDevice device)
UsbRequest
.device
- the device to openUsbDeviceConnection
, or null
if open failedpublic UsbAccessory[] getAccessoryList()
public ParcelFileDescriptor openAccessory(UsbAccessory accessory)
accessory
- the USB accessory to openpublic boolean hasPermission(UsbDevice device)
requestPermission(UsbDevice, PendingIntent)
or
by the user choosing the caller as the default application for the device.device
- to check permissions forpublic boolean hasPermission(UsbAccessory accessory)
requestPermission(UsbAccessory, PendingIntent)
or
by the user choosing the caller as the default application for the accessory.accessory
- to check permissions forpublic void requestPermission(UsbDevice device, PendingIntent pi)
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
device
- to request permissions forpi
- PendingIntent for returning resultpublic void requestPermission(UsbAccessory accessory, PendingIntent pi)
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:
EXTRA_ACCESSORY
containing the accessory passed into this call
EXTRA_PERMISSION_GRANTED
containing boolean indicating whether
permission was granted by the user
accessory
- to request permissions forpi
- PendingIntent for returning resultpublic void grantPermission(UsbDevice device)
device
- to request permissions for
public void grantPermission(UsbDevice device, String packageName)
device
- to request permissions forpackageName
- of package to grant permissions
public boolean isFunctionEnabled(String function)
USB functions represent interfaces which are published to the host to access services offered by the device.
function
- name of the USB functionpublic void setCurrentFunction(String function)
USB functions represent interfaces which are published to the host to access services offered by the device.
This method is intended to select among primary USB functions. The system may
automatically activate additional functions such as USB_FUNCTION_ADB
or USB_FUNCTION_ACCESSORY
based on other settings and states.
The allowed values are: USB_FUNCTION_NONE
, USB_FUNCTION_AUDIO_SOURCE
,
USB_FUNCTION_MIDI
, USB_FUNCTION_MTP
, USB_FUNCTION_PTP
,
or USB_FUNCTION_RNDIS
.
Note: This function is asynchronous and may fail silently without applying the requested changes.
function
- name of the USB function, or null to restore the default function
public void setUsbDataUnlocked(boolean unlocked)
public UsbPort[] getPorts()
This list is guaranteed to contain all dual-role USB Type C ports but it might be missing other ports depending on whether the kernel USB drivers have been updated to publish all of the device's ports through the new "dual_role_usb" device class (which supports all types of ports despite its name).
public UsbPortStatus getPortStatus(UsbPort port)
port
- The port to query.public void setPortRoles(UsbPort port, int powerRole, int dataRole)
The supported role combinations depend on what is connected to the port and may be
determined by consulting
UsbPortStatus.isRoleCombinationSupported
.
Note: This function is asynchronous and may fail silently without applying
the requested changes. If this function does cause a status change to occur then
a ACTION_USB_PORT_CHANGED
broadcast will be sent.
powerRole
- The desired power role: UsbPort.POWER_ROLE_SOURCE
or UsbPort.POWER_ROLE_SINK
, or 0 if no power role.dataRole
- The desired data role: UsbPort.DATA_ROLE_HOST
or UsbPort.DATA_ROLE_DEVICE
, or 0 if no data role.