Java Code Examples for android.hardware.usb.UsbInterface#getInterfaceClass()

The following examples show how to use android.hardware.usb.UsbInterface#getInterfaceClass() . 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: UsbHidDevice.java    From UsbHid with MIT License 7 votes vote down vote up
public static UsbHidDevice[] enumerate(Context context, int vid, int pid) throws Exception {
    UsbManager usbManager = (UsbManager) context.getApplicationContext().getSystemService(Context.USB_SERVICE);
    if (usbManager == null) {
        throw new Exception("no usb service");
    }

    Map<String, UsbDevice> devices = usbManager.getDeviceList();
    List<UsbHidDevice> usbHidDevices = new ArrayList<>();
    for (UsbDevice device : devices.values()) {
        if ((vid == 0 || device.getVendorId() == vid) && (pid == 0 || device.getProductId() == pid)) {
            for (int i = 0; i < device.getInterfaceCount(); i++) {
                UsbInterface usbInterface = device.getInterface(i);
                if (usbInterface.getInterfaceClass() == INTERFACE_CLASS_HID) {
                    UsbHidDevice hidDevice = new UsbHidDevice(device, usbInterface, usbManager);
                    usbHidDevices.add(hidDevice);
                }
            }
        }
    }
    return usbHidDevices.toArray(new UsbHidDevice[usbHidDevices.size()]);
}
 
Example 2
Source File: CarlifeUtil.java    From apollo-DuerOS with Apache License 2.0 6 votes vote down vote up
public boolean isUsbStorageDevice(Context context) {
    UsbManager mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    if (null == mUsbManager) {
        LogUtil.e(TAG, "There is no devices");
        return false;
    } else {
        LogUtil.v(TAG, "Usb Devices: " + String.valueOf(mUsbManager.toString()));
    }
    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    while (deviceIterator.hasNext()) {
        UsbDevice device = deviceIterator.next();
        if (STORAGE_INTERFACE_CONUT == device.getInterfaceCount()) {
            UsbInterface usbInter = device.getInterface(STORAGE_INTERFACE_ID);
            if ((STORAGE_INTERFACE_CLASS == usbInter.getInterfaceClass())
                    && (STORAGE_INTERFACE_SUBCLASS == usbInter.getInterfaceSubclass())
                    && (STORAGE_INTERFACE_PROTOCOL == usbInter.getInterfaceProtocol())) {
                LogUtil.e(TAG, "This is mass storage 1");
                return true;
            }
        }
    }

    return false;
}
 
Example 3
Source File: CarlifeUtil.java    From apollo-DuerOS with Apache License 2.0 6 votes vote down vote up
public boolean isUsbStorageDevice(UsbDevice device) {
    if( device == null ) {
        LogUtil.e(TAG, "this device is null");
        return false;
    }

    if (STORAGE_INTERFACE_CONUT == device.getInterfaceCount()) {
        UsbInterface usbInter = device.getInterface(STORAGE_INTERFACE_ID);
        if ((STORAGE_INTERFACE_CLASS == usbInter.getInterfaceClass())
                && (STORAGE_INTERFACE_SUBCLASS == usbInter.getInterfaceSubclass())
                && (STORAGE_INTERFACE_PROTOCOL == usbInter.getInterfaceProtocol())) {
            LogUtil.e(TAG, "this device is mass storage 2");
            return true;
        }
    }

    return false;
}
 
Example 4
Source File: UsbMidiDeviceFactoryAndroid.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Request a device access permission if there is a MIDI interface in the device.
 *
 * @param device a USB device
 */
private void requestDevicePermissionIfNecessary(UsbDevice device) {
    for (UsbDevice d : mRequestedDevices) {
        if (d.getDeviceId() == device.getDeviceId()) {
            // It is already requested.
            return;
        }
    }

    for (int i = 0; i < device.getInterfaceCount(); ++i) {
        UsbInterface iface = device.getInterface(i);
        if (iface.getInterfaceClass() == UsbConstants.USB_CLASS_AUDIO
                && iface.getInterfaceSubclass() == UsbMidiDeviceAndroid.MIDI_SUBCLASS) {
            // There is at least one interface supporting MIDI.
            mUsbManager.requestPermission(device,
                    PendingIntent.getBroadcast(ContextUtils.getApplicationContext(), 0,
                            new Intent(ACTION_USB_PERMISSION), 0));
            mRequestedDevices.add(device);
            break;
        }
    }
}
 
Example 5
Source File: UsbSession.java    From yubikit-android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets interface of a specified class
 * @param usbClass UsbConstants that identifies interface class (e.g. UsbConstants.USB_CLASS_CSCID or UsbConstants.USB_CLASS_HID)
 * @return interface of device
 */
private UsbInterface getInterface(int usbClass) {
    UsbInterface selectedInterface = null;
    for (int i = 0; i < usbDevice.getInterfaceCount(); i++) {
        UsbInterface usbInterface = usbDevice.getInterface(i);
        if (usbInterface.getInterfaceClass() == usbClass) {
            selectedInterface = usbInterface;
            break;
        }
    }
    return selectedInterface;
}
 
Example 6
Source File: ConnectManager.java    From apollo-DuerOS with Apache License 2.0 5 votes vote down vote up
public boolean isADBDeviceIn() {
    final int usbClassAdb = 255;
    final int usbSubClassAdb = 66;
    final int usbProtocolAdb = 1;


    UsbManager mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
    if (null == mUsbManager) {
        return  false;
    }

    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    Log.d( TAG, "device count=" + deviceList.size() );

    int nInedex = 0;
    boolean bGetADBDevice = false;
    while (deviceIterator.hasNext()) {
        UsbDevice device = deviceIterator.next();
        int interfaceCount = device.getInterfaceCount();
        Log.d( TAG, "Device Info index ::" + nInedex + "Interface Count ::" + interfaceCount );
        for (int interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++) {
            UsbInterface usbInterface = device.getInterface(interfaceIndex);
            Log.d( TAG, "Interface  ::[Class=" + usbInterface.getInterfaceClass() + "][Sub Class="
                    + usbInterface.getInterfaceSubclass()
                    + "][Protocol=" + usbInterface.getInterfaceProtocol() + "]");

            if ((usbClassAdb == usbInterface.getInterfaceClass())
                    && (usbSubClassAdb == usbInterface.getInterfaceSubclass())
                    && (usbProtocolAdb == usbInterface.getInterfaceProtocol())) {
                Log.d( TAG, "GetADB Initeface !!!!!!" );
                bGetADBDevice  = true;
                break;
            }
        }
        ++nInedex;
    }
    return  bGetADBDevice;
}
 
Example 7
Source File: DeviceFilter.java    From libcommon with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
public DeviceFilter(@NonNull final UsbDevice device, final boolean isExclude) {
	mVendorId = device.getVendorId();
	mProductId = device.getProductId();
	mClass = device.getDeviceClass();
	mSubclass = device.getDeviceSubclass();
	mProtocol = device.getDeviceProtocol();
	// getInterfaceCountは内部配列のlengthを返すので負にはならないはずだけど年のために下限を0にする
	final int count = Math.max(device.getInterfaceCount(), 0);
	mIntfClass = new int[count];
	mIntfSubClass = new int[count];
	mIntfProtocol = new int[count];
	for (int i = 0; i < count; i++) {
		final UsbInterface intf = device.getInterface(i);
		mIntfClass[i] = intf.getInterfaceClass();
		mIntfSubClass[i] = intf.getInterfaceSubclass();
		mIntfProtocol[i] = intf.getInterfaceProtocol();
	}
	if (BuildCheck.isLollipop()) {
		mManufacturerName = device.getManufacturerName();
		mProductName = device.getProductName();
		mSerialNumber = device.getSerialNumber();
	} else {
		mManufacturerName = null;
		mProductName = null;
		mSerialNumber = null;
	}
	this.isExclude = isExclude;
}
 
Example 8
Source File: UsbMidiDeviceAndroid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a UsbMidiDeviceAndroid.
 * @param manager
 * @param device The USB device which this object is assocated with.
 */
UsbMidiDeviceAndroid(UsbManager manager, UsbDevice device) {
    mConnection = manager.openDevice(device);
    mEndpointMap = new SparseArray<UsbEndpoint>();
    mRequestMap = new HashMap<UsbEndpoint, UsbRequest>();
    mHandler = new Handler();
    mUsbDevice = device;
    mIsClosed = false;
    mHasInputThread = false;
    mNativePointer = 0;

    for (int i = 0; i < device.getInterfaceCount(); ++i) {
        UsbInterface iface = device.getInterface(i);
        if (iface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO
                || iface.getInterfaceSubclass() != MIDI_SUBCLASS) {
            continue;
        }
        mConnection.claimInterface(iface, true);
        for (int j = 0; j < iface.getEndpointCount(); ++j) {
            UsbEndpoint endpoint = iface.getEndpoint(j);
            if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
                mEndpointMap.put(endpoint.getEndpointNumber(), endpoint);
            }
        }
    }
    // Start listening for input endpoints.
    // This function will create and run a thread if there is USB-MIDI endpoints in the
    // device. Note that because UsbMidiDevice is shared among all tabs and the thread
    // will be terminated when the device is disconnected, at most one thread can be created
    // for each connected USB-MIDI device.
    startListen(device);
}
 
Example 9
Source File: FTDISioIds.java    From UsbSerial with MIT License 5 votes vote down vote up
private static boolean isMicrochipFtdi(UsbDevice dev) {
    if (dev.getVendorId() != 0x04d8 || dev.getProductId() != 0x000a)
        return false;
    for (int i = 0; i < dev.getInterfaceCount(); i++) {
        UsbInterface intf = dev.getInterface(i);
        if (intf.getInterfaceClass() == 0xff && intf.getInterfaceSubclass() == 0xff &&
                intf.getInterfaceProtocol() == 0x00)
            return true;
    }
    return false;
}
 
Example 10
Source File: UsbSerialDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
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;
}
 
Example 11
Source File: ConnectManager.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
public boolean isMobileDeviceIn() {
    final int storageInterfaceConut = 1;
    final int storageInterfaceId = 0;
    final int storageInterfaceClass = 8;
    int storageInterfaceSubclass = 6;
    int storageInterfaceProtocol = 80;

    UsbManager mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
    if (null == mUsbManager) {
        Log.d( TAG, "############## UsbManager Error!");
        return  false;
    }

    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    Log.d( TAG, "############################");
    Log.d( TAG, "device count=" + deviceList.size() );

    int nInedex = 0;
    boolean bGetDevice = false;
    while (deviceIterator.hasNext()) {
        UsbDevice device = deviceIterator.next();
        if (storageInterfaceConut == device.getInterfaceCount()) {
            UsbInterface usbInter = device.getInterface(storageInterfaceId);
            if ((storageInterfaceClass == usbInter.getInterfaceClass())
                    && (storageInterfaceSubclass == usbInter.getInterfaceSubclass())
                    && (storageInterfaceProtocol == usbInter.getInterfaceProtocol())) {
                LogUtil.e(TAG, "This is mass storage 1");
                break;
            }
        }

        int interfaceCount = device.getInterfaceCount();
        Log.d( TAG, "Device Info [PID:" + device.getProductId() + "][VID:"
                + device.getDeviceId()
                + "] Interface Count ::" + interfaceCount );
        if ( interfaceCount > 0 ) {
            bGetDevice = true;
        }
        ++nInedex;
    }
    return  bGetDevice;
}
 
Example 12
Source File: UsbIpService.java    From USBIPServerForAndroid with GNU General Public License v3.0 4 votes vote down vote up
private UsbDeviceInfo getInfoForDevice(UsbDevice dev, UsbDeviceConnection devConn) {
	UsbDeviceInfo info = new UsbDeviceInfo();
	UsbIpDevice ipDev = new UsbIpDevice();
	
	ipDev.path = dev.getDeviceName();
	ipDev.busnum = deviceIdToBusNum(dev.getDeviceId());
	ipDev.devnum =  deviceIdToDevNum(dev.getDeviceId());
	ipDev.busid = String.format("%d-%d", ipDev.busnum, ipDev.devnum);
	
	ipDev.idVendor = (short) dev.getVendorId();
	ipDev.idProduct = (short) dev.getProductId();
	ipDev.bcdDevice = -1;
	
	ipDev.bDeviceClass = (byte) dev.getDeviceClass();
	ipDev.bDeviceSubClass = (byte) dev.getDeviceSubclass();
	ipDev.bDeviceProtocol = (byte) dev.getDeviceProtocol();
	
	ipDev.bConfigurationValue = 0;
	ipDev.bNumConfigurations = 1;
	
	ipDev.bNumInterfaces = (byte) dev.getInterfaceCount();
	
	info.dev = ipDev;
	info.interfaces = new UsbIpInterface[ipDev.bNumInterfaces];
	
	for (int i = 0; i < ipDev.bNumInterfaces; i++) {
		info.interfaces[i] = new UsbIpInterface();
		UsbInterface iface = dev.getInterface(i);
		
		info.interfaces[i].bInterfaceClass = (byte) iface.getInterfaceClass();
		info.interfaces[i].bInterfaceSubClass = (byte) iface.getInterfaceSubclass();
		info.interfaces[i].bInterfaceProtocol = (byte) iface.getInterfaceProtocol();
	}
	
	AttachedDeviceContext context = connections.get(dev.getDeviceId());
	UsbDeviceDescriptor devDesc = null;
	if (context != null) {
		// Since we're attached already, we can directly query the USB descriptors
		// to fill some information that Android's USB API doesn't expose
		devDesc = UsbControlHelper.readDeviceDescriptor(context.devConn);
		
		ipDev.bcdDevice = devDesc.bcdDevice;
		ipDev.bNumConfigurations = devDesc.bNumConfigurations;
	}
	
	ipDev.speed = detectSpeed(dev, devDesc);
	
	return info;
}
 
Example 13
Source File: CdcAcmSerialDriver.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void openInterface() throws IOException {
    Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount());

    int controlInterfaceCount = 0;
    int dataInterfaceCount = 0;
    mControlInterface = null;
    mDataInterface = null;
    for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
        UsbInterface usbInterface = mDevice.getInterface(i);
        if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_COMM) {
            if(controlInterfaceCount == mPortNumber) {
                mControlIndex = i;
                mControlInterface = usbInterface;
            }
            controlInterfaceCount++;
        }
        if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
            if(dataInterfaceCount == mPortNumber) {
                mDataInterface = usbInterface;
            }
            dataInterfaceCount++;
        }
    }

    if(mControlInterface == null) {
        throw new IOException("No control interface");
    }
    Log.d(TAG, "Control iface=" + mControlInterface);

    if (!mConnection.claimInterface(mControlInterface, true)) {
        throw new IOException("Could not claim control interface");
    }

    mControlEndpoint = mControlInterface.getEndpoint(0);
    if (mControlEndpoint.getDirection() != UsbConstants.USB_DIR_IN || mControlEndpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
        throw new IOException("Invalid control endpoint");
    }

    if(mDataInterface == null) {
        throw new IOException("No data interface");
    }
    Log.d(TAG, "data iface=" + mDataInterface);

    if (!mConnection.claimInterface(mDataInterface, true)) {
        throw new IOException("Could not claim data interface");
    }

    for (int i = 0; i < mDataInterface.getEndpointCount(); i++) {
        UsbEndpoint ep = mDataInterface.getEndpoint(i);
        if (ep.getDirection() == UsbConstants.USB_DIR_IN && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
            mReadEndpoint = ep;
        if (ep.getDirection() == UsbConstants.USB_DIR_OUT && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
            mWriteEndpoint = ep;
    }
}