Example usage for android.hardware.usb UsbManager EXTRA_DEVICE

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

Introduction

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

Prototype

String EXTRA_DEVICE

To view the source code for android.hardware.usb UsbManager EXTRA_DEVICE.

Click Source Link

Document

Name of extra for #ACTION_USB_DEVICE_ATTACHED and #ACTION_USB_DEVICE_DETACHED broadcasts containing the UsbDevice object for the device.

Usage

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

/**
* Searches for connected mass storage devices, and initializes them if it
* could find some./*from w w w. jav a2s . c  o 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);
    }
}