FileDocCategorySizeDatePackage
UsbManager.javaAPI DocAndroid 5.1 API7247Thu Mar 12 22:22:30 GMT 2015com.android.future.usb

UsbManager

public class UsbManager extends Object
This is a wrapper class for the USB Manager to support USB accessories.

You can obtain an instance of this class by calling {@link #getInstance}

Fields Summary
private static final String
TAG
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. Call {@link #getAccessory(android.content.Intent)} to retrieve the {@link com.google.android.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. Call {@link #getAccessory(android.content.Intent)} to retrieve the {@link com.google.android.usb.UsbAccessory} for the attached accessory that was detached.
public static final String
EXTRA_PERMISSION_GRANTED
Name of extra added to the {@link android.app.PendingIntent} passed into {#requestPermission} or {#requestPermission} containing a boolean value indicating whether the user granted permission or not.
private final android.content.Context
mContext
private final android.hardware.usb.IUsbManager
mService
Constructors Summary
private UsbManager(android.content.Context context, android.hardware.usb.IUsbManager service)


         
        mContext = context;
        mService = service;
    
Methods Summary
public static UsbAccessorygetAccessory(android.content.Intent intent)
Returns the {@link com.google.android.usb.UsbAccessory} for a {@link #ACTION_USB_ACCESSORY_ATTACHED} or {@link #ACTION_USB_ACCESSORY_ATTACHED} broadcast Intent. This can also be used to retrieve the accessory from the result of a call to {#requestPermission}.

return
UsbAccessory for the intent.

        android.hardware.usb.UsbAccessory accessory =
            intent.getParcelableExtra(android.hardware.usb.UsbManager.EXTRA_ACCESSORY);
        if (accessory == null) {
            return null;
        } else {
            return new UsbAccessory(accessory);
        }
    
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 {
            android.hardware.usb.UsbAccessory accessory = mService.getCurrentAccessory();
            if (accessory == null) {
                return null;
            } else {
                return new UsbAccessory[] { new UsbAccessory(accessory) };
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in getAccessoryList" , e);
            return null;
        }
    
public static com.android.future.usb.UsbManagergetInstance(android.content.Context context)
Returns a new instance of this class.

param
context the caller's {@link android.content.Context}
return
UsbManager instance.

        IBinder b = ServiceManager.getService(Context.USB_SERVICE);
        return new UsbManager(context, IUsbManager.Stub.asInterface(b));
    
public booleanhasPermission(UsbAccessory accessory)
Returns true if the caller has permission to access the accessory. Permission might have been granted temporarily via {@link #requestPermission} 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(new android.hardware.usb.UsbAccessory(
                    accessory.getManufacturer(),accessory.getModel(),
                    accessory.getDescription(), accessory.getVersion(),
                    accessory.getUri(), accessory.getSerial()));
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in hasPermission", e);
            return false;
        }
    
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(new android.hardware.usb.UsbAccessory(
                    accessory.getManufacturer(),accessory.getModel(),
                    accessory.getDescription(), accessory.getVersion(),
                    accessory.getUri(), accessory.getSerial()));
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in openAccessory" , e);
            return null;
        }
    
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. The boolean extra {@link #EXTRA_PERMISSION_GRANTED} will be attached to the PendingIntent to indicate success or failure. If successful, this grants the caller permission to access the device only until the device is disconnected.

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

        try {
            mService.requestAccessoryPermission(new android.hardware.usb.UsbAccessory(
                    accessory.getManufacturer(),accessory.getModel(),
                    accessory.getDescription(), accessory.getVersion(),
                    accessory.getUri(), accessory.getSerial()),
                    mContext.getPackageName(), pi);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in requestPermission", e);
        }