Example usage for android.hardware.usb UsbManager requestPermission

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

Introduction

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

Prototype

@RequiresFeature(PackageManager.FEATURE_USB_ACCESSORY)
public void requestPermission(UsbAccessory accessory, PendingIntent pi) 

Source Link

Document

Requests temporary permission for the given package to access the accessory.

Usage

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/* w  ww  . j a va  2  s.  c om*/
 */
@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 . ja  v  a 2s.  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.numato.usbgenie.ui.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Listen to the SwipeTorefresh events
    mDevicesSwipeRefreshLayout.setOnRefreshListener(this);

    mDevicesManager = DevicesManager.getInstance();

    mDeviceListView.setOnItemClickListener(new OnItemClickListener() {
        @Override//from   w  ww .jav a 2 s.c om
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // Get the USB device
            NumatoUSBDevice numatoUSBDevice = mDevicesManager.getDevices().get(position);

            // request USB permission for the device
            UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
            PendingIntent mPermissionIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
                    new Intent(ACTION_USB_PERMISSION).putExtra(AppConstant.EXTRA_DEVICE_INDEX, position), 0);
            IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
            registerReceiver(mUsbReceiver, filter);
            manager.requestPermission(numatoUSBDevice.getDevice(), mPermissionIntent);

        }
    });

    enumerateDevices();

    /*
     * Create a new ArrayAdapter and initialize the list with it.
     */
    mDeviceListArrayAdapter = new DeviceListArrayAdapter(this, new ArrayList<NumatoUSBDevice>());
    mDeviceListView.setAdapter(mDeviceListArrayAdapter);

    listDevices();
}

From source file:com.ekumen.tangobot.application.MainActivity.java

@Override
protected void init(NodeMainExecutor nodeMainExecutor) {
    mLog.info("MainActivity init");

    // Store a reference to the NodeMainExecutor
    mNodeMainExecutor = nodeMainExecutor;

    mMasterUri = getMasterUri();/*from   w ww .ja  va  2 s . c  o  m*/
    mHostName = getRosHostname();

    mLog.info(mMasterUri);
    updateMasterUriUI(mMasterUri.toString());

    // Trigger asking permission to access any devices that are already connected
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    for (UsbDevice device : manager.getDeviceList().values()) {
        manager.requestPermission(device, mUsbPermissionIntent);
    }

    checkRosMasterConnection();
    configureParameterServer();

    startBaseControllerNode();

    startExtrinsicsPublisherNodes();
    startMapServerNode();

    // Start Tango node and navigation stack.
    startTangoRosNode();
    startMoveBaseNode();
}

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);//from ww w  .  jav  a2  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;//www. j  a  v  a2s .c o  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.j a v  a2 s .  c  om
*/
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);
    }
}