Java Code Examples for android.hardware.usb.UsbManager#getDeviceList()

The following examples show how to use android.hardware.usb.UsbManager#getDeviceList() . 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: UsbUtils.java    From libcommon with Apache License 2.0 8 votes vote down vote up
/**
 * 接続されているUSBの機器リストをLogCatに出力
 * @param context
 */
public static void dumpDevices(@NonNull final Context context) {
	final UsbManager usbManager = ContextUtils.requireSystemService(context, UsbManager.class);
	final HashMap<String, UsbDevice> list = usbManager.getDeviceList();
	if ((list != null) && !list.isEmpty()) {
		final Set<String> keys = list.keySet();
		if (keys != null && keys.size() > 0) {
			final StringBuilder sb = new StringBuilder();
			for (final String key: keys) {
				final UsbDevice device = list.get(key);
				final int num_interface = device != null ? device.getInterfaceCount() : 0;
				sb.setLength(0);
				for (int i = 0; i < num_interface; i++) {
					sb.append(String.format(Locale.US, "interface%d:%s",
						i, device.getInterface(i).toString()));
				}
				Log.i(TAG, "key=" + key + ":" + device + ":" + sb.toString());
			}
		} else {
			Log.i(TAG, "no device");
		}
	} else {
		Log.i(TAG, "no device");
	}
}
 
Example 2
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 3
Source File: SyncingService.java    From xDrip-Experimental with GNU General Public License v3.0 6 votes vote down vote up
static public boolean isG4Connected(Context c){
    UsbManager manager = (UsbManager) c.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Log.i("USB DEVICES = ", deviceList.toString());
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    Log.i("USB DEVICES = ", String.valueOf(deviceList.size()));

    while(deviceIterator.hasNext()){
        UsbDevice device = deviceIterator.next();
        if (device.getVendorId() == 8867 && device.getProductId() == 71
                && device.getDeviceClass() == 2 && device.getDeviceSubclass() ==0
                && device.getDeviceProtocol() == 0){
            Log.i("CALIBRATION-CHECK-IN: ", "Dexcom Found!");
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: SyncingService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public UsbDevice findDexcom() {
    Log.i("CALIBRATION-CHECK-IN: ", "Searching for dexcom");
    mUsbManager = (UsbManager) getApplicationContext().getSystemService(Context.USB_SERVICE);
    Log.i("USB MANAGER = ", mUsbManager.toString());
    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
    Log.i("USB DEVICES = ", deviceList.toString());
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    Log.i("USB DEVICES = ", String.valueOf(deviceList.size()));

    while(deviceIterator.hasNext()){
        UsbDevice device = deviceIterator.next();
        if (device.getVendorId() == 8867 && device.getProductId() == 71
                && device.getDeviceClass() == 2 && device.getDeviceSubclass() ==0
                && device.getDeviceProtocol() == 0){
            dexcom = device;
            Log.i("CALIBRATION-CHECK-IN: ", "Dexcom Found!");
            return device;
        } else {
            Log.w("CALIBRATION-CHECK-IN: ", "that was not a dexcom (I dont think)");
        }
    }
    return null;
}
 
Example 5
Source File: SyncingService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
static public boolean isG4Connected(Context c){
    UsbManager manager = (UsbManager) c.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Log.i("USB DEVICES = ", deviceList.toString());
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    Log.i("USB DEVICES = ", String.valueOf(deviceList.size()));

    while(deviceIterator.hasNext()){
        UsbDevice device = deviceIterator.next();
        if (device.getVendorId() == 8867 && device.getProductId() == 71
                && device.getDeviceClass() == 2 && device.getDeviceSubclass() ==0
                && device.getDeviceProtocol() == 0){
            Log.i("CALIBRATION-CHECK-IN: ", "Dexcom Found!");
            return true;
        }
    }
    return false;
}
 
Example 6
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 7
Source File: SyncingService.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
static public boolean isG4Connected(Context c){
    UsbManager manager = (UsbManager) c.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Log.i("USB DEVICES = ", deviceList.toString());
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    Log.i("USB DEVICES = ", String.valueOf(deviceList.size()));

    while(deviceIterator.hasNext()){
        UsbDevice device = deviceIterator.next();
        if (device.getVendorId() == 8867 && device.getProductId() == 71
                && device.getDeviceClass() == 2 && device.getDeviceSubclass() ==0
                && device.getDeviceProtocol() == 0){
            Log.i("CALIBRATION-CHECK-IN: ", "Dexcom Found!");
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: BTChipTransportAndroid.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public static UsbDevice getDevice(UsbManager manager) {
	HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
	for (UsbDevice device : deviceList.values()) {
		if ((device.getVendorId() == VID || device.getVendorId() == VID2) &&
		   ((device.getProductId() == PID_WINUSB) || (device.getProductId() == PID_HID) ||
		    (device.getProductId() == PID_NANOS) || (device.getProductId() == PID_BLUE) ||
		    (device.getProductId() == PID_HID_LEDGER) || (device.getProductId() == PID_HID_LEDGER_PROTON))) {
			return device;
		}
	}
	return null;		
}
 
Example 9
Source File: BTChipTransportAndroid.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static UsbDevice getDevice(UsbManager manager) {
	HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
	for (UsbDevice device : deviceList.values()) {
		if ((device.getVendorId() == VID || device.getVendorId() == VID2) &&
		   ((device.getProductId() == PID_WINUSB) || (device.getProductId() == PID_HID) ||
		    (device.getProductId() == PID_NANOS) || (device.getProductId() == PID_BLUE) ||
		    (device.getProductId() == PID_HID_LEDGER) || (device.getProductId() == PID_HID_LEDGER_PROTON))) {
			return device;
		}
	}
	return null;		
}
 
Example 10
Source File: UsbTools.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static UsbDevice getUsbDevice(final int vendorId, final int productId, final String search) {

        final UsbManager manager = (UsbManager) xdrip.getAppContext().getSystemService(Context.USB_SERVICE);
        if (manager == null) return null;
        final HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        for (Map.Entry<String, UsbDevice> entry : deviceList.entrySet()) {
            final UsbDevice device = entry.getValue();
            if (device.getVendorId() == vendorId && device.getProductId() == productId
                    && device.toString().contains(search)) {
                Log.d(TAG, "Found device: " + entry.getKey() + " " + device.toString());
                return device;
            }
        }
        return null;
    }
 
Example 11
Source File: PtpUsbService.java    From remoteyourcam-usb with Apache License 2.0 5 votes vote down vote up
private UsbDevice lookupCompatibleDevice(UsbManager manager) {
    Map<String, UsbDevice> deviceList = manager.getDeviceList();
    for (Map.Entry<String, UsbDevice> e : deviceList.entrySet()) {
        UsbDevice d = e.getValue();
        if (d.getVendorId() == PtpConstants.CanonVendorId || d.getVendorId() == PtpConstants.NikonVendorId) {
            return d;
        }
    }
    return null;
}
 
Example 12
Source File: UsbTools.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static UsbDevice getUsbDevice(final int vendorId, final int productId, final String search) {

        final UsbManager manager = (UsbManager) xdrip.getAppContext().getSystemService(Context.USB_SERVICE);
        if (manager == null) return null;
        final HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        for (Map.Entry<String, UsbDevice> entry : deviceList.entrySet()) {
            final UsbDevice device = entry.getValue();
            if (device.getVendorId() == vendorId && device.getProductId() == productId
                    && device.toString().contains(search)) {
                Log.d(TAG, "Found device: " + entry.getKey() + " " + device.toString());
                return device;
            }
        }
        return null;
    }
 
Example 13
Source File: UsbSerialProber.java    From usb-with-serial-port with Apache License 2.0 5 votes vote down vote up
/**
 * Finds and builds all possible {@link UsbSerialDriver UsbSerialDrivers}
 * from the currently-attached {@link UsbDevice} hierarchy. This method does
 * not require permission from the Android USB system, since it does not
 * open any of the devices.
 *
 * @param usbManager
 *
 * @return a list, possibly empty, of all compatible drivers
 */
@Keep
public List<UsbSerialDriver> findAllDrivers(final UsbManager usbManager) {
    final List<UsbSerialDriver> result = new ArrayList<UsbSerialDriver>();
    HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    while (deviceIterator.hasNext()) {
        UsbDevice usbDevice = deviceIterator.next();
        UsbSerialDriver driver = probeDevice(usbDevice);
        if (driver != null) {
            result.add(driver);
        }
    }
    return result;
}
 
Example 14
Source File: SerialPortBuilder.java    From UsbSerial with MIT License 5 votes vote down vote up
public List<UsbDevice> getPossibleSerialPorts(Context context){

        usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

        HashMap<String, UsbDevice> allDevices = usbManager.getDeviceList();
        List<UsbDevice> devices =  Stream.of(allDevices.values())
                .filter(UsbSerialDevice::isSupported)
                .toList();

        return devices;
    }
 
Example 15
Source File: BTChipTransportAndroidHID.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
public static UsbDevice getDevice(UsbManager manager) {
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    for (UsbDevice device : deviceList.values()) {
        Timber.d("%04X:%04X %s, %s", device.getVendorId(), device.getProductId(), device.getManufacturerName(), device.getProductName());
        if (device.getVendorId() == VID) {
            final int deviceProductId = device.getProductId();
            for (int pid : PID_HIDS) {
                if (deviceProductId == pid)
                    return device;
            }
        }
    }
    return null;
}
 
Example 16
Source File: FTDI_USB_Handler.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
private void enumerate() {
	l("enumerating");

	UsbManager usbman = (UsbManager) activity
			.getSystemService(Context.USB_SERVICE);

	HashMap<String, UsbDevice> devlist = usbman.getDeviceList();
	Iterator<UsbDevice> deviter = devlist.values().iterator();
	PendingIntent pi = PendingIntent.getBroadcast(activity, 0, new Intent(
			ACTION_USB_PERMISSION), 0);

	while (deviter.hasNext()) {
		UsbDevice d = deviter.next();
		l("Found device: "
				+ String.format("%04X:%04X", d.getVendorId(), d.getProductId()));
		if (String.format("%04X:%04X", d.getVendorId(), d.getProductId()).equals(
				VID_PID)) {
			// we need to upload the hex file, first request permission
			l("Device under: " + d.getDeviceName());
			activity.registerReceiver(mPermissionReceiver, new IntentFilter(
					ACTION_USB_PERMISSION));
			if (!usbman.hasPermission(d))
				usbman.requestPermission(d, pi);
			else
				init_USB(d);

			// init_USB(d);
			break;
		}
	}
	l("no more devices found");
}
 
Example 17
Source File: BTChipTransportAndroid.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
public static UsbDevice getDevice(UsbManager manager) {
	HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
	for (UsbDevice device : deviceList.values()) {
		if ((device.getVendorId() == VID || device.getVendorId() == VID2) &&
		   ((device.getProductId() == PID_WINUSB) || (device.getProductId() == PID_HID) ||
		    (device.getProductId() == PID_NANOS) || (device.getProductId() == PID_BLUE) ||
			(device.getProductId() == PID_NANOX) || (device.getProductId() == PID_HID_LEDGER) ||
			(device.getProductId() == PID_HID_LEDGER_PROTON))) {
			return device;
		}
	}
	return null;		
}
 
Example 18
Source File: MedtronicCnlService.java    From 600SeriesAndroidUploader with MIT License 4 votes vote down vote up
private void getUsbInfo() {

        UsbManager mManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = mManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        while (deviceIterator.hasNext()) {
            UsbDevice device = deviceIterator.next();
            Log.i(TAG, "Model: " + device.getDeviceName());
            Log.i(TAG, "ID: " + device.getDeviceId());
            Log.i(TAG, "Class: " + device.getDeviceClass());
            Log.i(TAG, "Protocol: " + device.getDeviceProtocol());
            Log.i(TAG, "Vendor ID " + device.getVendorId());
            Log.i(TAG, "Product ID: " + device.getProductId());
            Log.i(TAG, "Interface count: " + device.getInterfaceCount());
            Log.i(TAG, "---------------------------------------");
            // Get interface details
            for (int index = 0; index < device.getInterfaceCount(); index++) {
                UsbInterface mUsbInterface = device.getInterface(index);
                Log.i(TAG, "  *****     *****");
                Log.i(TAG, "  Interface index: " + index);
                Log.i(TAG, "  Interface ID: " + mUsbInterface.getId());
                Log.i(TAG, "  Inteface class: " + mUsbInterface.getInterfaceClass());
                Log.i(TAG, "  Interface protocol: " + mUsbInterface.getInterfaceProtocol());
                Log.i(TAG, "  Endpoint count: " + mUsbInterface.getEndpointCount());
                // Get endpoint details
                for (int epi = 0; epi < mUsbInterface.getEndpointCount(); epi++) {
                    UsbEndpoint mEndpoint = mUsbInterface.getEndpoint(epi);
                    Log.i(TAG, "    ++++   ++++   ++++");
                    Log.i(TAG, "    Endpoint index: " + epi);
                    Log.i(TAG, "    Attributes: " + mEndpoint.getAttributes());
                    Log.i(TAG, "    Direction: " + mEndpoint.getDirection());
                    Log.i(TAG, "    Number: " + mEndpoint.getEndpointNumber());
                    Log.i(TAG, "    Interval: " + mEndpoint.getInterval());
                    Log.i(TAG, "    Packet size: " + mEndpoint.getMaxPacketSize());
                    Log.i(TAG, "    Type: " + mEndpoint.getType());
                }
            }
        }
        Log.i(TAG, " No more devices connected.");
    }
 
Example 19
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 20
Source File: SettingsActivity.java    From Easycam with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
private ArrayList<DeviceEntry> enumerateUsbDevices() {

			ArrayList<DeviceEntry> validStreamingDeviceList = new ArrayList<>(5);

			synchronized (JsonManager.lock) {

				//Make sure the JsonManger has been initialized
				if (!JsonManager.isInitialized())
					JsonManager.initialize();


				// Enumerate a list of currently connected Usb devices so we can pick out which
				// ones are supported easycap devices
				UsbManager mUsbManager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
				HashMap<String, UsbDevice> usbDeviceList = mUsbManager.getDeviceList();
				Iterator<UsbDevice> deviceIterator = usbDeviceList.values().iterator();

				// Make sure the device list is empty before enumeration
				validStreamingDeviceList.clear();

				while(deviceIterator.hasNext()) {

					UsbDevice uDevice = deviceIterator.next();

					DeviceInfo devInfo = JsonManager.getDevice(uDevice.getVendorId(), uDevice.getProductId(),
							DeviceInfo.DeviceStandard.NTSC);

					// If a supported device is listed in json list, request permission
					// to access it
					if (devInfo != null) {

						Log.i(TAG, "Supported usb device found: " + uDevice.toString());
						Log.i(TAG, "Device ID: " + uDevice.getDeviceId());
						Log.i(TAG, "Device Name: " + uDevice.getDeviceName());
						Log.i(TAG, "Vendor: ID " + uDevice.getVendorId());
						Log.i(TAG, "Product ID: " + uDevice.getProductId());

						DeviceEntry devEntry = new DeviceEntry();

						devEntry.deviceDescription = devInfo.getDescription() + " @ " + uDevice.getDeviceName();
						devEntry.deviceName = uDevice.getDeviceName() + ":" + devInfo.getVendorID()
								+ ":" + devInfo.getProductID();

						validStreamingDeviceList.add(devEntry);

					}
				}

			}

			return validStreamingDeviceList;
		}