1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-07 16:06:10 +00:00

Add ProbeDevice.

This commit is contained in:
mike wakerly 2014-03-31 23:12:52 -07:00
parent 66eec6c870
commit 9c577949b0

View File

@ -67,32 +67,47 @@ public class UsbSerialProber {
final List<UsbSerialDriver> result = new ArrayList<UsbSerialDriver>(); final List<UsbSerialDriver> result = new ArrayList<UsbSerialDriver>();
for (final UsbDevice usbDevice : usbManager.getDeviceList().values()) { for (final UsbDevice usbDevice : usbManager.getDeviceList().values()) {
final int vendorId = usbDevice.getVendorId(); final UsbSerialDriver driver = probeDevice(usbDevice);
final int productId = usbDevice.getProductId(); if (driver != null) {
final Class<? extends UsbSerialDriver> driverClass =
mProbeTable.findDriver(vendorId, productId);
if (driverClass != null) {
final UsbSerialDriver driver;
try {
final Constructor<? extends UsbSerialDriver> ctor =
driverClass.getConstructor(UsbDevice.class);
driver = ctor.newInstance(usbDevice);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
result.add(driver); result.add(driver);
} }
} }
return result; return result;
} }
/**
* Probes a single device for a compatible driver.
*
* @param usbDevice the usb device to probe
* @return a new {@link UsbSerialDriver} compatible with this device, or
* {@code null} if none available.
*/
public UsbSerialDriver probeDevice(final UsbDevice usbDevice) {
final int vendorId = usbDevice.getVendorId();
final int productId = usbDevice.getProductId();
final Class<? extends UsbSerialDriver> driverClass =
mProbeTable.findDriver(vendorId, productId);
if (driverClass != null) {
final UsbSerialDriver driver;
try {
final Constructor<? extends UsbSerialDriver> ctor =
driverClass.getConstructor(UsbDevice.class);
driver = ctor.newInstance(usbDevice);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
return driver;
}
return null;
}
} }