UsbManagerpublic class UsbManager extends Object 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.
You can obtain an instance of this class by calling
{@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
{@samplecode
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);}
Developer Guides
For more information about communicating with USB hardware, read the
USB developer guide.
|
Fields Summary |
---|
private static final String | TAG | public static final String | ACTION_USB_STATEBroadcast Action: A sticky broadcast for USB state change events when in device mode.
This is a sticky broadcast for clients that includes USB connected/disconnected state,
- {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
- {@link #USB_CONFIGURED} boolean indicating whether USB is configured.
currently zero if not configured, one for configured.
- {@link #USB_FUNCTION_MASS_STORAGE} boolean extra indicating whether the
mass storage function is enabled
- {@link #USB_FUNCTION_ADB} boolean extra indicating whether the
adb function is enabled
- {@link #USB_FUNCTION_RNDIS} boolean extra indicating whether the
RNDIS ethernet function is enabled
- {@link #USB_FUNCTION_MTP} boolean extra indicating whether the
MTP function is enabled
- {@link #USB_FUNCTION_PTP} boolean extra indicating whether the
PTP function is enabled
- {@link #USB_FUNCTION_PTP} boolean extra indicating whether the
accessory function is enabled
- {@link #USB_FUNCTION_AUDIO_SOURCE} boolean extra indicating whether the
audio source function is enabled
{@hide} | public static final String | ACTION_USB_DEVICE_ATTACHEDBroadcast Action: A broadcast for USB device attached event.
This intent is sent when a USB device is attached to the USB bus when in host mode.
- {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice}
for the attached device
| public static final String | ACTION_USB_DEVICE_DETACHEDBroadcast 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.
- {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice}
for the detached device
| public static final String | ACTION_USB_ACCESSORY_ATTACHEDBroadcast Action: A broadcast for USB accessory attached event.
This intent is sent when a USB accessory is attached.
- {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory}
for the attached accessory
| public static final String | ACTION_USB_ACCESSORY_DETACHEDBroadcast Action: A broadcast for USB accessory detached event.
This intent is sent when a USB accessory is detached.
- {@link #EXTRA_ACCESSORY} containing the {@link UsbAccessory}
for the attached accessory that was detached
| public static final String | USB_CONNECTEDBoolean extra indicating whether USB is connected or disconnected.
Used in extras for the {@link #ACTION_USB_STATE} broadcast.
{@hide} | public static final String | USB_CONFIGUREDBoolean extra indicating whether USB is configured.
Used in extras for the {@link #ACTION_USB_STATE} broadcast.
{@hide} | public static final String | USB_FUNCTION_MASS_STORAGEName of the USB mass storage USB function.
Used in extras for the {@link #ACTION_USB_STATE} broadcast
{@hide} | public static final String | USB_FUNCTION_ADBName of the adb USB function.
Used in extras for the {@link #ACTION_USB_STATE} broadcast
{@hide} | public static final String | USB_FUNCTION_RNDISName of the RNDIS ethernet USB function.
Used in extras for the {@link #ACTION_USB_STATE} broadcast
{@hide} | public static final String | USB_FUNCTION_MTPName of the MTP USB function.
Used in extras for the {@link #ACTION_USB_STATE} broadcast
{@hide} | public static final String | USB_FUNCTION_PTPName of the PTP USB function.
Used in extras for the {@link #ACTION_USB_STATE} broadcast
{@hide} | public static final String | USB_FUNCTION_AUDIO_SOURCEName of the audio source USB function.
Used in extras for the {@link #ACTION_USB_STATE} broadcast
{@hide} | public static final String | USB_FUNCTION_ACCESSORYName of the Accessory USB function.
Used in extras for the {@link #ACTION_USB_STATE} broadcast
{@hide} | public static final String | EXTRA_DEVICEName of extra for {@link #ACTION_USB_DEVICE_ATTACHED} and
{@link #ACTION_USB_DEVICE_DETACHED} broadcasts
containing the UsbDevice object for the device. | public static final String | EXTRA_ACCESSORYName of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} and
{@link #ACTION_USB_ACCESSORY_DETACHED} broadcasts
containing the UsbAccessory object for the accessory. | public static final String | EXTRA_PERMISSION_GRANTEDName of extra added to the {@link android.app.PendingIntent}
passed into {@link #requestPermission(UsbDevice, PendingIntent)}
or {@link #requestPermission(UsbAccessory, PendingIntent)}
containing a boolean value indicating whether the user granted permission or not. | private final android.content.Context | mContext | private final IUsbManager | mService |
Methods Summary |
---|
public UsbAccessory[] | getAccessoryList()Returns a list of currently attached USB accessories.
(in the current implementation there can be at most one)
try {
UsbAccessory accessory = mService.getCurrentAccessory();
if (accessory == null) {
return null;
} else {
return new UsbAccessory[] { accessory };
}
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in getAccessoryList", e);
return null;
}
| public java.lang.String | getDefaultFunction()Returns the current default USB function.
String functions = SystemProperties.get("persist.sys.usb.config", "");
int commaIndex = functions.indexOf(',");
if (commaIndex > 0) {
return functions.substring(0, commaIndex);
} else {
return functions;
}
| public java.util.HashMap | 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.
Bundle bundle = new Bundle();
try {
mService.getDeviceList(bundle);
HashMap<String,UsbDevice> result = new HashMap<String,UsbDevice>();
for (String name : bundle.keySet()) {
result.put(name, (UsbDevice)bundle.get(name));
}
return result;
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in getDeviceList", e);
return null;
}
| public boolean | hasPermission(UsbDevice device)Returns true if the caller has permission to access the device.
Permission might have been granted temporarily via
{@link #requestPermission(UsbDevice, PendingIntent)} or
by the user choosing the caller as the default application for the device.
try {
return mService.hasDevicePermission(device);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in hasPermission", e);
return false;
}
| public boolean | hasPermission(UsbAccessory accessory)Returns true if the caller has permission to access the accessory.
Permission might have been granted temporarily via
{@link #requestPermission(UsbAccessory, PendingIntent)} or
by the user choosing the caller as the default application for the accessory.
try {
return mService.hasAccessoryPermission(accessory);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in hasPermission", e);
return false;
}
| public boolean | isFunctionEnabled(java.lang.String function)Returns true if the specified USB function is currently enabled.
return propertyContainsFunction("sys.usb.config", function);
| public android.os.ParcelFileDescriptor | openAccessory(UsbAccessory accessory)Opens a file descriptor for reading and writing data to the USB accessory.
try {
return mService.openAccessory(accessory);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in openAccessory", e);
return null;
}
| public UsbDeviceConnection | openDevice(UsbDevice device)Opens the device so it can be used to send and receive
data using {@link android.hardware.usb.UsbRequest}.
try {
String deviceName = device.getDeviceName();
ParcelFileDescriptor pfd = mService.openDevice(deviceName);
if (pfd != null) {
UsbDeviceConnection connection = new UsbDeviceConnection(device);
boolean result = connection.open(deviceName, pfd);
pfd.close();
if (result) {
return connection;
}
}
} catch (Exception e) {
Log.e(TAG, "exception in UsbManager.openDevice", e);
}
return null;
| private static boolean | propertyContainsFunction(java.lang.String property, java.lang.String function)
String functions = SystemProperties.get(property, "");
int index = functions.indexOf(function);
if (index < 0) return false;
if (index > 0 && functions.charAt(index - 1) != ',") return false;
int charAfter = index + function.length();
if (charAfter < functions.length() && functions.charAt(charAfter) != ',") return false;
return true;
| public void | requestPermission(UsbDevice device, android.app.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 {@link 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:
- {@link #EXTRA_DEVICE} containing the device passed into this call
- {@link #EXTRA_PERMISSION_GRANTED} containing boolean indicating whether
permission was granted by the user
try {
mService.requestDevicePermission(device, mContext.getPackageName(), pi);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in requestPermission", e);
}
| public void | requestPermission(UsbAccessory accessory, android.app.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 {@link 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:
- {@link #EXTRA_ACCESSORY} containing the accessory passed into this call
- {@link #EXTRA_PERMISSION_GRANTED} containing boolean indicating whether
permission was granted by the user
try {
mService.requestAccessoryPermission(accessory, mContext.getPackageName(), pi);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in requestPermission", e);
}
| public void | setCurrentFunction(java.lang.String function, boolean makeDefault)Sets the current USB function.
If function is null, then the current function is set to the default function.
try {
mService.setCurrentFunction(function, makeDefault);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setCurrentFunction", e);
}
| public void | setMassStorageBackingFile(java.lang.String path)Sets the file path for USB mass storage backing file.
try {
mService.setMassStorageBackingFile(path);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setDefaultFunction", e);
}
|
|