Example usage for android.hardware.usb UsbManager getAccessoryList

List of usage examples for android.hardware.usb UsbManager getAccessoryList

Introduction

In this page you can find the example usage for android.hardware.usb UsbManager getAccessoryList.

Prototype

@RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY)
public UsbAccessory[] getAccessoryList() 

Source Link

Document

Returns a list of currently attached USB accessories.

Usage

From source file:nl.bennyjacobs.aapbridge.aap.UsbConnection.java

/**
 * Connect to the first usb accessory.//w  w  w.  j av  a  2  s.  c  om
 * 
 * @param context
 * @return A connection to the first usb accessory or null in case of failure.
 * @throws IOException 
 */
public static UsbConnection easyConnect(Context context) throws IOException {
    UsbManager mUSBManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    UsbAccessory[] accessories = mUSBManager.getAccessoryList();
    UsbAccessory accessory = (accessories == null ? null : accessories[0]);
    if (accessory != null) {
        if (mUSBManager.hasPermission(accessory)) {
            UsbConnection connection = new UsbConnection(context, mUSBManager, accessory);
            Log.v(TAG, "Connected to USB accessory");
            return connection;
        }
        throw new IOException("No permission to operate on accessory");
    }
    throw new IOException("No USB accessory found");
}