public UsbDeviceAssert hasInterfaceCount(int count) { isNotNull(); int actualCount = actual.getInterfaceCount(); assertThat(actualCount) // .overridingErrorMessage("Expected interface count <%s> but was <%s>.", count, actualCount) // .isEqualTo(count); return this; }
public void open() throws IOException { if (!device_found) { throw new IOException("Device not Connected"); } mConnection = mUsbManager.openDevice(mUsbDevice); Log.d(TAG, "Claiming interfaces, count=" + mUsbDevice.getInterfaceCount()); mConnection.controlTransfer(0x21, 0x22, 0x1, 0, null, 0, 0); mControlInterface = mUsbDevice.getInterface(0); Log.d(TAG, "Control interface=" + mControlInterface); if (!mConnection.claimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.getEndpoint(0); Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); Log.d(TAG, "Claiming data interface."); mDataInterface = mUsbDevice.getInterface(1); Log.d(TAG, "data interface=" + mDataInterface); if (!mConnection.claimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } mReadEndpoint = mDataInterface.getEndpoint(1); Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); mWriteEndpoint = mDataInterface.getEndpoint(0); Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); connected = true; setBaudRate(1000000); //Thread.sleep(1000); clear(); }
public UsbDeviceAssert hasInterfaceCount(int count) { isNotNull(); int actualCount = actual.getInterfaceCount(); assertThat(actualCount) // .overridingErrorMessage("Expected interface count <%s> but was <%s>.", count, actualCount) // .isEqualTo(count); return this; }
private static int findFirstCDC(UsbDevice device) { int interfaceCount = device.getInterfaceCount(); for (int iIndex = 0; iIndex < interfaceCount; ++iIndex) { if (device.getInterface(iIndex).getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) { return iIndex; } } Log.i(CLASS_ID, "There is no CDC class interface"); return -1; }
private UsbMidiDevice(UsbDevice device) { super(device); int ifaceCount = device.getInterfaceCount(); for (int i = 0; i < ifaceCount; ++i) { UsbMidiInterface iface = asMidiInterface(device.getInterface(i)); if (iface != null) { interfaces.add(iface); } } }
/** * Find {@link UsbInterface} from {@link UsbDevice} with the direction * * @param usbDevice the UsbDevice * @param direction {@link UsbConstants#USB_DIR_IN} or {@link UsbConstants#USB_DIR_OUT} * @param deviceFilters the List of {@link DeviceFilter} * @return {@link Set<UsbInterface>} always not null */ @NonNull public static Set<UsbInterface> findMidiInterfaces(@NonNull UsbDevice usbDevice, int direction, @NonNull List<DeviceFilter> deviceFilters) { Set<UsbInterface> usbInterfaces = new HashSet<>(); int count = usbDevice.getInterfaceCount(); for (int i = 0; i < count; i++) { UsbInterface usbInterface = usbDevice.getInterface(i); if (findMidiEndpoint(usbDevice, usbInterface, direction, deviceFilters) != null) { usbInterfaces.add(usbInterface); } } return Collections.unmodifiableSet(usbInterfaces); }
private void createAllPorts(UsbDeviceStatus usbDeviceStatus){ int interfaceCount = usbDeviceStatus.usbDevice.getInterfaceCount(); for(int i=0;i<=interfaceCount-1;i++) { if(usbDeviceStatus.usbDeviceConnection == null) { usbDeviceStatus.usbDeviceConnection = usbManager.openDevice(usbDeviceStatus.usbDevice); } UsbSerialDevice usbSerialDevice = UsbSerialDevice.createUsbSerialDevice( usbDeviceStatus.usbDevice, usbDeviceStatus.usbDeviceConnection, i); serialDevices.add(usbSerialDevice); } }
public static boolean isCdcDevice(UsbDevice device) { int iIndex = device.getInterfaceCount(); for(int i=0;i<=iIndex-1;i++) { UsbInterface iface = device.getInterface(i); if(iface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) return true; } return false; }
/** * Find all {@link UsbInterface} from {@link UsbDevice} * * @param usbDevice the UsbDevice * @param deviceFilters the List of {@link DeviceFilter} * @return {@link Set<UsbInterface>} always not null */ @NonNull public static Set<UsbInterface> findAllMidiInterfaces(@NonNull UsbDevice usbDevice, @NonNull List<DeviceFilter> deviceFilters) { Set<UsbInterface> usbInterfaces = new HashSet<>(); int count = usbDevice.getInterfaceCount(); for (int i = 0; i < count; i++) { UsbInterface usbInterface = usbDevice.getInterface(i); if (findMidiEndpoint(usbDevice, usbInterface, UsbConstants.USB_DIR_IN, deviceFilters) != null) { usbInterfaces.add(usbInterface); } if (findMidiEndpoint(usbDevice, usbInterface, UsbConstants.USB_DIR_OUT, deviceFilters) != null) { usbInterfaces.add(usbInterface); } } return Collections.unmodifiableSet(usbInterfaces); }
WebcamConnection(@NonNull Context context, @NonNull android.hardware.usb.UsbDevice usbDevice) throws UnknownDeviceException, DevicePermissionDenied { this.usbManager = new UsbManager(context); // A Webcam must have at least a control interface and a video interface if (usbDevice.getInterfaceCount() < 2) { throw new UnknownDeviceException(); } // Claim the control interface Timber.d("Initializing native layer."); usbDeviceConnection = usbManager.registerDevice(usbDevice); parseAssiociationDescriptors(); }
@Override public void open(UsbDeviceConnection connection) throws IOException { if (mConnection != null) { throw new IOException("Already open"); } mConnection = connection; boolean opened = false; try { for (int i = 0; i < mDevice.getInterfaceCount(); i++) { if (connection.claimInterface(mDevice.getInterface(i), true)) { Log.d(TAG, "claimInterface " + i + " SUCCESS"); } else { throw new IOException("Error claiming interface " + i); } } reset(); opened = true; } finally { if (!opened) { close(); mConnection = null; } } }
@Override public void open(UsbDeviceConnection connection) throws IOException { if (mConnection != null) { throw new IOException("Already open"); } mConnection = connection; boolean opened = false; try { for (int i = 0; i < mDevice.getInterfaceCount(); i++) { if (connection.claimInterface(mDevice.getInterface(i), true)) { Log.d(TAG, "claimInterface " + i + " SUCCESS"); } else { throw new IOException("Error claiming interface " + i); } } reset(); opened = true; } finally { if (!opened) { close(); mConnection = null; } } }
private void openInterface() throws IOException { Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount()); mControlInterface = mDevice.getInterface(0); Log.d(TAG, "Control iface=" + mControlInterface); // class should be USB_CLASS_COMM if (!mConnection.claimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.getEndpoint(0); Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); Log.d(TAG, "Claiming data interface."); mDataInterface = mDevice.getInterface(1); Log.d(TAG, "data iface=" + mDataInterface); // class should be USB_CLASS_CDC_DATA if (!mConnection.claimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } mReadEndpoint = mDataInterface.getEndpoint(1); Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); mWriteEndpoint = mDataInterface.getEndpoint(0); Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); }
private void openInterface() throws IOException { Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount()); mControlInterface = mDevice.getInterface(0); Log.d(TAG, "Control iface=" + mControlInterface); // class should be USB_CLASS_COMM if (!mConnection.claimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.getEndpoint(0); Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); Log.d(TAG, "Claiming data interface."); mDataInterface = mDevice.getInterface(1); Log.d(TAG, "data iface=" + mDataInterface); // class should be USB_CLASS_CDC_DATA if (!mConnection.claimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } mReadEndpoint = mDataInterface.getEndpoint(1); Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); mWriteEndpoint = mDataInterface.getEndpoint(0); Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); }
@Override public void open(UsbDeviceConnection connection) throws IOException { if (mConnection != null) { throw new IOException("Already open"); } mConnection = connection; boolean opened = false; try { for (int i = 0; i < mDevice.getInterfaceCount(); i++) { if (connection.claimInterface(mDevice.getInterface(i), true)) { Log.d(TAG, "claimInterface " + i + " SUCCESS"); } else { throw new IOException("Error claiming interface " + i); } } reset(); opened = true; } finally { if (!opened) { close(); mConnection = null; } } }
@Override public void open(UsbDeviceConnection connection) throws IOException { if (mConnection != null) { throw new IOException("Already open"); } mConnection = connection; boolean opened = false; try { for (int i = 0; i < mDevice.getInterfaceCount(); i++) { if (connection.claimInterface(mDevice.getInterface(i), true)) { Log.d(TAG, "claimInterface " + i + " SUCCESS"); } else { throw new IOException("Error claiming interface " + i); } } reset(); opened = true; } finally { if (!opened) { close(); mConnection = null; } } }
/** * Enumerate the endpoints and interfaces on the connected device. * * @param device Device to query. * @return String description of the device configuration. */ public static String readDevice(UsbDevice device) { StringBuilder sb = new StringBuilder(); sb.append("Device Name: " + device.getDeviceName() + "\n"); sb.append(String.format( "Device Class: %s -> Subclass: 0x%02x -> Protocol: 0x%02x\n", nameForClass(device.getDeviceClass()), device.getDeviceSubclass(), device.getDeviceProtocol())); for (int i = 0; i < device.getInterfaceCount(); i++) { UsbInterface intf = device.getInterface(i); sb.append(String.format(Locale.US, "-- Interface %d Class: %s -> Subclass: 0x%02x -> Protocol: 0x%02x\n", intf.getId(), nameForClass(intf.getInterfaceClass()), intf.getInterfaceSubclass(), intf.getInterfaceProtocol())); sb.append(String.format(Locale.US, " -- Endpoint Count: %d\n", intf.getEndpointCount())); } return sb.toString(); }
public UsbDevice findUsbDevice() { logger.log(String.format("Looking for TeensyUSB VID=0x%x PID=0x%x", getVid(), getPid())); HashMap<String, UsbDevice> deviceHash = usbManager.getDeviceList(); if (deviceHash.size() == 0) { logger.log("No connected USB devices found"); return null; } logger.log("Found " + deviceHash.size() + " connected USB devices:"); UsbDevice usbDevice = null; for (String key : deviceHash.keySet()) { UsbDevice dev = deviceHash.get(key); String msg = String.format(Locale.US, "USB Device: %s, VID:PID - %x:%x, %d interfaces", key, dev.getVendorId(), dev.getProductId(), dev.getInterfaceCount() ); if (isCompatibleUsbDevice(dev)) { usbDevice = dev; msg = "Using " + msg; } else { msg = "Skipping " + msg; } logger.log(msg); } return usbDevice; }
/** * Tests to see if a {@link android.hardware.usb.UsbDevice} * supports the PTP protocol (typically used by digital cameras) * * @param device the device to test * @return true if the device is a PTP device. */ static public boolean isCamera(UsbDevice device) { int count = device.getInterfaceCount(); for (int i = 0; i < count; i++) { UsbInterface intf = device.getInterface(i); if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_STILL_IMAGE && intf.getInterfaceSubclass() == 1 && intf.getInterfaceProtocol() == 1) { return true; } } return false; }
static boolean deviceIsTrezor(UsbDevice usbDevice) { // no usable interfaces if (usbDevice.getInterfaceCount() <= 0) { return false; } // TREZOR v1 if (usbDevice.getVendorId() == 0x534c) { return usbDevice.getProductId() == 0x0001; } // TREZOR v2 if (usbDevice.getVendorId() == 0x1209) { return usbDevice.getProductId() == 0x53c0 || usbDevice.getProductId() == 0x53c1; } return false; }