Code example for UsbManager

Methods: hasPermission, openAccessory

0
        if (mConnected) {
            disconnect(); 
        } 
 
        // Check whether we have permission to access the accessory. 
        if (!mUsbManager.hasPermission(accessory)) {
            mLogger.log("Prompting the user for access to the accessory.");
            Intent intent = new Intent(ACTION_USB_ACCESSORY_PERMISSION);
            intent.setPackage(getPackageName());
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
            mUsbManager.requestPermission(accessory, pendingIntent);
            return; 
        } 
 
        // Open the accessory. 
        ParcelFileDescriptor fd = mUsbManager.openAccessory(accessory);
        if (fd == null) {
            mLogger.logError("Could not obtain accessory connection.");
            return; 
        }