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

The following examples show how to use android.hardware.usb.UsbInterface#getInterfaceSubclass() . 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: UsbSession.java    From yubikit-android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and starts session for communication with yubikey using HID interface
 * @return session for communication with yubikey (supported over USB only)
 * @throws IOException if Keyboard HID interface or endpoints are not found
 */
public @NonNull
UsbHidConnection openHidKeyboardConnection() throws IOException {
    UsbInterface hidInterface = getInterface(UsbConstants.USB_CLASS_HID);
    if (hidInterface == null) {
        throw new IOException("No HID interface found");
    }

    if (hidInterface.getInterfaceSubclass() != UsbConstants.USB_INTERFACE_SUBCLASS_BOOT) {
        throw new IOException("No expected HID interface");
    }

    UsbDeviceConnection connection = openConnection();
    if (connection == null) {
        throw new IOException("exception in UsbManager.openDevice");
    }

    if (!connection.claimInterface(hidInterface, true)) {
        connection.close();
        throw new IOException("Interface couldn't be claimed");
    }

    return new UsbHidConnection(connection, hidInterface);
}
 
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: 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 6
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 7
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 8
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 9
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 10
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;
}