FileDocCategorySizeDatePackage
UsbManager.javaAPI DocAndroid 5.1 API16709Thu Mar 12 22:22:10 GMT 2015android.hardware.usb

UsbManager

public 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_STATE
Broadcast 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_ATTACHED
Broadcast 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_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.
  • {@link #EXTRA_DEVICE} containing the {@link android.hardware.usb.UsbDevice} for the detached device
public static final String
ACTION_USB_ACCESSORY_ATTACHED
Broadcast 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_DETACHED
Broadcast 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_CONNECTED
Boolean extra indicating whether USB is connected or disconnected. Used in extras for the {@link #ACTION_USB_STATE} broadcast. {@hide}
public static final String
USB_CONFIGURED
Boolean extra indicating whether USB is configured. Used in extras for the {@link #ACTION_USB_STATE} broadcast. {@hide}
public static final String
USB_FUNCTION_MASS_STORAGE
Name of the USB mass storage USB function. Used in extras for the {@link #ACTION_USB_STATE} broadcast {@hide}
public static final String
USB_FUNCTION_ADB
Name of the adb USB function. Used in extras for the {@link #ACTION_USB_STATE} broadcast {@hide}
public static final String
USB_FUNCTION_RNDIS
Name of the RNDIS ethernet USB function. Used in extras for the {@link #ACTION_USB_STATE} broadcast {@hide}
public static final String
USB_FUNCTION_MTP
Name of the MTP USB function. Used in extras for the {@link #ACTION_USB_STATE} broadcast {@hide}
public static final String
USB_FUNCTION_PTP
Name of the PTP USB function. Used in extras for the {@link #ACTION_USB_STATE} broadcast {@hide}
public static final String
USB_FUNCTION_AUDIO_SOURCE
Name of the audio source USB function. Used in extras for the {@link #ACTION_USB_STATE} broadcast {@hide}
public static final String
USB_FUNCTION_ACCESSORY
Name of the Accessory USB function. Used in extras for the {@link #ACTION_USB_STATE} broadcast {@hide}
public static final String
EXTRA_DEVICE
Name 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_ACCESSORY
Name 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_GRANTED
Name 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
Constructors Summary
public UsbManager(android.content.Context context, IUsbManager service)
{@hide}


          
         
        mContext = context;
        mService = service;
    
Methods Summary
public UsbAccessory[]getAccessoryList()
Returns a list of currently attached USB accessories. (in the current implementation there can be at most one)

return
list of USB accessories, or null if none are attached.

        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.StringgetDefaultFunction()
Returns the current default USB function.

return
name of the default function. {@hide}

        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.HashMapgetDeviceList()
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.

return
HashMap containing all connected USB devices.

        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 booleanhasPermission(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.

param
device to check permissions for
return
true if caller has permission

        try {
            return mService.hasDevicePermission(device);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in hasPermission", e);
            return false;
        }
    
public booleanhasPermission(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.

param
accessory to check permissions for
return
true if caller has permission

        try {
            return mService.hasAccessoryPermission(accessory);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in hasPermission", e);
            return false;
        }
    
public booleanisFunctionEnabled(java.lang.String function)
Returns true if the specified USB function is currently enabled.

param
function name of the USB function
return
true if the USB function is enabled. {@hide}

        return propertyContainsFunction("sys.usb.config", function);
    
public android.os.ParcelFileDescriptoropenAccessory(UsbAccessory accessory)
Opens a file descriptor for reading and writing data to the USB accessory.

param
accessory the USB accessory to open
return
file descriptor, or null if the accessor could not be opened.

        try {
            return mService.openAccessory(accessory);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in openAccessory", e);
            return null;
        }
    
public UsbDeviceConnectionopenDevice(UsbDevice device)
Opens the device so it can be used to send and receive data using {@link android.hardware.usb.UsbRequest}.

param
device the device to open
return
a {@link UsbDeviceConnection}, or {@code null} if open failed

        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 booleanpropertyContainsFunction(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 voidrequestPermission(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

param
device to request permissions for
param
pi PendingIntent for returning result

        try {
            mService.requestDevicePermission(device, mContext.getPackageName(), pi);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in requestPermission", e);
        }
    
public voidrequestPermission(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

param
accessory to request permissions for
param
pi PendingIntent for returning result

        try {
            mService.requestAccessoryPermission(accessory, mContext.getPackageName(), pi);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in requestPermission", e);
        }
    
public voidsetCurrentFunction(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.

param
function name of the USB function, or null to restore the default function
param
makeDefault true if the function should be set as the new default function {@hide}

        try {
            mService.setCurrentFunction(function, makeDefault);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setCurrentFunction", e);
        }
    
public voidsetMassStorageBackingFile(java.lang.String path)
Sets the file path for USB mass storage backing file.

param
path backing file path {@hide}

        try {
            mService.setMassStorageBackingFile(path);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in setDefaultFunction", e);
        }