Android Open Source - usbseriallib Usb Serial Device Factory






From Project

Back to project page usbseriallib.

License

The source code is released under:

GNU General Public License

If you think the Android project usbseriallib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ecgtheow.UsbSerialLib;
//from w  ww  .j av a2s. c  o  m
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import android.hardware.usb.UsbDevice;
import android.util.Log;

public class UsbSerialDeviceFactory {
  private static final String TAG = "UsbSerialDeviceFactory";
  
  private static final ArrayList<UsbSerialDeviceDescriptor> devices = new ArrayList<UsbSerialDeviceDescriptor>() {
    private static final long serialVersionUID = 4274183412791649462L; /* Shut eclipse up */

  {
    add(new PL2303Descriptor());
    add(new CP210xDescriptor());
  }};
  
  public static UsbSerialDevice createDevice(UsbDevice device, UsbDeviceReadEvent read_event) {
    for(UsbSerialDeviceDescriptor driver_descriptor : devices) {
      if(driver_descriptor.knownDevice(device.getVendorId(), device.getProductId())) {
        try {
          UsbSerialDevice driver = driver_descriptor.driverClass().getConstructor(UsbDevice.class, UsbSerialDeviceDescriptor.class, UsbDeviceReadEvent.class).newInstance(device, driver_descriptor, read_event);
          return driver;
        } catch (InstantiationException e) {
          Log.d(TAG, "Exception thrown", e);
          return null;
        } catch (IllegalAccessException e) {
          Log.d(TAG, "Exception thrown", e);
          return null;
        } catch (IllegalArgumentException e) {
          Log.d(TAG, "Exception thrown", e);
          return null;
        } catch (SecurityException e) {
          Log.d(TAG, "Exception thrown", e);
          return null;
        } catch (InvocationTargetException e) {
          Log.d(TAG, "Exception thrown", e);
          return null;
        } catch (NoSuchMethodException e) {
          Log.d(TAG, "Exception thrown", e);
          return null;
        }
      }
    }
    return null;
  }
  
  public static boolean knownDevice(UsbDevice device) {
    for(UsbSerialDeviceDescriptor driver_descriptor : devices) {
      if(driver_descriptor.knownDevice(device.getVendorId(), device.getProductId())) {
        return true;
      }
    }
    
    return false;
  }
}




Java Source Code List

com.ecgtheow.UsbSerialLib.BaudRate.java
com.ecgtheow.UsbSerialLib.CP210xDescriptor.java
com.ecgtheow.UsbSerialLib.CP210x.java
com.ecgtheow.UsbSerialLib.DataBits.java
com.ecgtheow.UsbSerialLib.PL2303Descriptor.java
com.ecgtheow.UsbSerialLib.PL2303Type.java
com.ecgtheow.UsbSerialLib.PL2303.java
com.ecgtheow.UsbSerialLib.Parity.java
com.ecgtheow.UsbSerialLib.StopBits.java
com.ecgtheow.UsbSerialLib.UsbDeviceConnectionEvent.java
com.ecgtheow.UsbSerialLib.UsbDeviceId.java
com.ecgtheow.UsbSerialLib.UsbDeviceReadEvent.java
com.ecgtheow.UsbSerialLib.UsbSerialDeviceDescriptor.java
com.ecgtheow.UsbSerialLib.UsbSerialDeviceFactory.java
com.ecgtheow.UsbSerialLib.UsbSerialDevice.java
com.ecgtheow.UsbSerialLib.UsbSerialLib.java