Java Code Examples for android.hardware.usb.UsbEndpoint#getAddress()

The following examples show how to use android.hardware.usb.UsbEndpoint#getAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Trezor.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public static Trezor getDevice(final Context context) {
    final UsbManager manager = (UsbManager)context.getSystemService(Context.USB_SERVICE);

    for (final UsbDevice device: manager.getDeviceList().values()) {
        // Check if the device is TREZOR (or AvalonWallet or BWALLET)

        final int vendorId = device.getVendorId();
        final int productId = device.getProductId();

        if ((vendorId != 0x534c || productId != 0x0001) &&
            (vendorId != 0x1209 || productId != 0x53c0) &&
            (vendorId != 0x1209 || productId != 0x53c1) &&
            (vendorId != 0x10c4 || productId != 0xea80)) {
            continue;
        }

        Log.d(TAG, "Hardware Wallet device found");
        if (device.getInterfaceCount() < 1) {
            Log.e(TAG, "Wrong interface count");
            continue;
        }

        // Use first interface
        final UsbInterface iface = device.getInterface(0);
        // Try to find read/write endpoints
        UsbEndpoint readEndpoint = null, writeEndpoint = null;

        for (int i = 0; i < iface.getEndpointCount(); ++i) {
            final UsbEndpoint ep = iface.getEndpoint(i);

            if (readEndpoint == null &&
                    ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT &&
                    ep.getAddress() == 0x81) {
                // number = 1 ; dir = USB_DIR_IN
                readEndpoint = ep;
                continue;
            }
            if (writeEndpoint == null &&
                    ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT &&
                    (ep.getAddress() == 0x01 || ep.getAddress() == 0x02)) {
                // number = 1 ; dir = USB_DIR_OUT
                writeEndpoint = ep;
                continue;
            }
            Log.d(TAG, String.format("ep %d", ep.getAddress()));
        }

        if (isEndpointBad(readEndpoint, "read") || isEndpointBad(writeEndpoint, "write"))
            continue;

        // Try to open the device
        final UsbDeviceConnection conn = manager.openDevice(device);
        if (conn == null || !conn.claimInterface(iface,  true)) {
            Log.e(TAG, conn == null ? "Could not open connection" : "Could not claim interface");
            continue;
        }

        // All OK - return the class
        return new Trezor(device, conn, readEndpoint, writeEndpoint);
    }
    return null;
}
 
Example 2
Source File: Trezor.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public static Trezor getDevice(final Context context, final TrezorGUICallback guiFn,
                               final Network network) {
    final UsbManager manager = (UsbManager)context.getSystemService(Context.USB_SERVICE);

    for (final UsbDevice device: manager.getDeviceList().values()) {
        // Check if the device is TREZOR (or AvalonWallet or BWALLET)

        final int vendorId = device.getVendorId();
        final int productId = device.getProductId();

        if ((vendorId != 0x534c || productId != 0x0001) &&
            (vendorId != 0x1209 || productId != 0x53c0) &&
            (vendorId != 0x1209 || productId != 0x53c1) &&
            (vendorId != 0x10c4 || productId != 0xea80)) {
            continue;
        }

        Log.i(TAG, "Hardware Wallet device found");
        if (device.getInterfaceCount() < 1) {
            Log.e(TAG, "Wrong interface count");
            continue;
        }

        // Use first interface
        final UsbInterface iface = device.getInterface(0);
        // Try to find read/write endpoints
        UsbEndpoint readEndpoint = null, writeEndpoint = null;

        for (int i = 0; i < iface.getEndpointCount(); ++i) {
            final UsbEndpoint ep = iface.getEndpoint(i);

            if (readEndpoint == null &&
                    ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT &&
                    ep.getAddress() == 0x81) {
                // number = 1 ; dir = USB_DIR_IN
                readEndpoint = ep;
                continue;
            }
            if (writeEndpoint == null &&
                    ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT &&
                    (ep.getAddress() == 0x01 || ep.getAddress() == 0x02)) {
                // number = 1 ; dir = USB_DIR_OUT
                writeEndpoint = ep;
                continue;
            }
            Log.e(TAG, String.format("ep %d", ep.getAddress()));
        }

        if (!isEndpointOK(readEndpoint, "read") || !isEndpointOK(writeEndpoint, "write"))
            continue;

        // Try to open the device
        final UsbDeviceConnection conn = manager.openDevice(device);
        if (conn == null || !conn.claimInterface(iface,  true)) {
            Log.e(TAG, conn == null ? "Could not open connection" : "Could not claim interface");
            continue;
        }

        // All OK - return the class
        return new Trezor(guiFn, device, conn, readEndpoint, writeEndpoint, network);
    }
    return null;
}