Example usage for android.hardware.usb UsbManager openDevice

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

Introduction

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

Prototype

@RequiresFeature(PackageManager.FEATURE_USB_HOST)
public UsbDeviceConnection openDevice(UsbDevice device) 

Source Link

Document

Opens the device so it can be used to send and receive data using android.hardware.usb.UsbRequest .

Usage

From source file:com.ekumen.tangobot.loaders.UsbDeviceNodeLoader.java

protected UsbDeviceConnection serialConnectionForDevice(UsbManager manager, UsbSerialDriver driver) {
    return manager.openDevice(driver.getDevice());
}

From source file:fr.bmartel.android.fadecandy.service.FadecandyService.java

/**
 * Open USB device & choose USBEndpoint.
 *
 * @param device Usb device object./* w  w w .  ja  v a 2  s.co  m*/
 * @return Ubs item composed of USBDevice , USBEndpoint & UsbConnection
 */
private UsbItem openDevice(UsbDevice device) {

    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    try {
        UsbDeviceConnection connection = manager.openDevice(device);

        Log.v("USB", "Device name=" + device.getDeviceName());

        UsbInterface intf = null;

        for (int interfaceIndex = 0; interfaceIndex < device.getInterfaceCount(); interfaceIndex++) {

            UsbInterface usbInterface = device.getInterface(interfaceIndex);

            if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC) {
                intf = usbInterface;
            }
        }

        if (intf == null) {
            intf = device.getInterface(0);
        }

        UsbEndpoint endpoint = intf.getEndpoint(0);

        if (connection != null) {
            connection.claimInterface(intf, true);
        } else {
            Log.e(TAG, "USB connection error");
            return null;
        }

        return new UsbItem(device, connection, endpoint);

    } catch (IllegalArgumentException e) {
    }
    return null;
}

From source file:com.hoho.android.usbserial.examples.SerialConsoleActivity.java

@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "Resumed, port=" + sPort);
    if (sPort == null) {
        mTitleTextView.setText("No serial device.");
    } else {//from   w w  w.  j a  v  a  2 s. c o m
        final UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        UsbDeviceConnection connection = usbManager.openDevice(sPort.getDriver().getDevice());
        if (connection == null) {
            mTitleTextView.setText("Opening device failed");
            return;
        }

        try {
            sPort.open(connection);
            sPort.setParameters(57600, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1,
                    UsbSerialPort.PARITY_NONE);
        } catch (IOException e) {
            Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
            mTitleTextView.setText("Error opening device: " + e.getMessage());
            try {
                sPort.close();
            } catch (IOException e2) {
                // Ignore.
            }
            sPort = null;
            return;
        }
        mTitleTextView.setText("Serial device: " + sPort.getClass().getSimpleName());
    }
    onDeviceStateChange();
}

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  ww.ja v  a 2  s  . co 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/*ww 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: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;/*  w w w  . ja  va 2  s .com*/
    }

    // 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();

}