Example usage for android.hardware.usb UsbManager hasPermission

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

Introduction

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

Prototype

@RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY)
public boolean hasPermission(UsbAccessory accessory) 

Source Link

Document

Returns true if the caller has permission to access the accessory.

Usage

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

/**
 * Connect to the first usb accessory.//from w  w  w . j ava2  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");
}

From source file:marto.rtl_tcp_andro.DeviceOpenActivity.java

/**
 * Opens a certain USB device and prepares an argument to be passed to libusb
 * @param device//from w w w  .  j  av a2 s .  c o m
 */
@SuppressLint("NewApi")
public void openDevice(final UsbDevice device) {
    final UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    if (device != null && !manager.hasPermission(device)) {
        android.util.Log.d("rtl_tcp_andro", "No permissions for device, requesting!");
        manager.requestPermission(device, permissionIntent);
        return;
    }

    if (device == null || !manager.hasPermission(device))
        finishWithError(err_info.permission_denied);

    final UsbDeviceConnection connection = manager.openDevice(device);

    if (connection == null)
        finishWithError(err_info.unknown_error);

    startBinary(arguments, connection.getFileDescriptor(), properDeviceName(device.getDeviceName()));
}

From source file:com.wordpress.ebc81.rtl_ais_android.DeviceOpenActivity.java

/**
 * Opens a certain USB device and prepares an argument to be passed to libusb
 * @param device/*w w  w  .j  a  va  2 s  .  c  o m*/
 */
//@SuppressLint("NewApi")
public void openDevice(final UsbDevice device) {
    final UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    if (device != null && !manager.hasPermission(device)) {
        android.util.Log.d("rtl_tcp_andro", "No permissions for device, requesting!");
        manager.requestPermission(device, permissionIntent);
        return;
    }

    if (device == null || !manager.hasPermission(device))
        finishWithError(err_info.permission_denied);

    final UsbDeviceConnection connection = manager.openDevice(device);

    if (connection == null)
        finishWithError(err_info.unknown_error);

    startBinary(arguments, connection.getFileDescriptor(), properDeviceName(device.getDeviceName()));
}

From source file:com.swiftnav.piksidroid.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.setupUI();

    UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);

    filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiverDisconnect, filter);

    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();

    for (UsbDevice device : deviceList.values()) {
        if ((device.getVendorId() == Utils.PIKSI_VID) && (device.getProductId() == Utils.PIKSI_PID))
            if (!mUsbManager.hasPermission(device)) {
                mUsbManager.requestPermission(device, mPermissionIntent);
            } else {
                ((EditText) findViewById(R.id.console)).setText("");
                piksiConnected(device);/* ww  w .  ja va  2  s. co m*/
            }
    }
}

From source file:de.tum.mw.lfe.drtrc.MainActivity.java

private void initUsb() {
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    // Find all available drivers from attached devices.
    List<UsbSerialDriver> availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager);
    if (availableDrivers.isEmpty()) {
        l("No device/driver");
        return;//from  w  w  w .j  ava 2 s  .co  m
    }

    // Open a connection to the first available driver.
    UsbSerialDriver mDriver = availableDrivers.get(0);
    //are we allowed to access?
    UsbDevice device = mDriver.getDevice();

    if (!manager.hasPermission(device)) {
        //ask for permission
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(GET_USB_PERMISSION), 0);
        mContext.registerReceiver(mPermissionReceiver, new IntentFilter(GET_USB_PERMISSION));
        manager.requestPermission(device, pi);
        l("USB ask for permission");
        return;
    }

    UsbDeviceConnection connection = manager.openDevice(mDriver.getDevice());
    if (connection == null) {
        l("USB connection == null");
        return;
    }

    try {
        sPort = (UsbSerialPort) mDriver.getPorts().get(0);//Most have just one port (port 0)
        sPort.open(connection);
        sPort.setParameters(115200, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
    } catch (IOException e) {
        l("Error setting up device: " + e.getMessage());
        try {
            sPort.close();
        } catch (IOException e2) {
            /*ignore*/}
        sPort = null;
        return;
    }

    onDeviceStateChange();

}

From source file:com.github.mjdev.libaums.usbfileman.MainActivity.java

/**
* Searches for connected mass storage devices, and initializes them if it
* could find some./*w  ww .jav a2 s.  co  m*/
*/
private void discoverDevice() {
    UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    UsbMassStorageDevice[] devices = UsbMassStorageDevice.getMassStorageDevices(this);

    if (devices.length == 0) {
        Log.w(TAG, "no device found!");
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("No device");
        listView.setAdapter(null);
        return;
    }

    // we only use the first device
    device = devices[0];

    UsbDevice usbDevice = (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);

    if (usbDevice != null && usbManager.hasPermission(usbDevice)) {
        Log.d(TAG, "received usb device via intent");
        // requesting permission is not needed in this case
        setupDevice();
    } else {
        // first request permission from user to communicate with the
        // underlying
        // UsbDevice
        PendingIntent permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION),
                0);
        usbManager.requestPermission(device.getUsbDevice(), permissionIntent);
    }
}