Example usage for android.hardware.usb UsbDevice getDeviceName

List of usage examples for android.hardware.usb UsbDevice getDeviceName

Introduction

In this page you can find the example usage for android.hardware.usb UsbDevice getDeviceName.

Prototype

public @NonNull String getDeviceName() 

Source Link

Document

Returns the name of the device.

Usage

From source file:org.ros.android.android_acm_serial.UsbDeviceDetachedReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    String deviceName = usbDevice.getDeviceName();
    AcmDevice acmDevice = acmDevices.remove(deviceName);
    if (acmDevice != null) {
        try {/* ww  w . j ava  2s  . c  o m*/
            acmDevice.close();
        } catch (RosRuntimeException e) {
            // Ignore spurious errors on disconnect.
        }
    }
    if (DEBUG) {
        log.info("USB device removed: " + deviceName);
    }
}

From source file:org.ros.android.android_acm_serial.AcmDeviceActivity.java

private void onUsbDeviceAttached(Intent intent) {
    if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
        UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        String deviceName = usbDevice.getDeviceName();
        if (!acmDevices.containsKey(deviceName)) {
            newAcmDevice(usbDevice);//from www. j ava  2 s.co m
        } else if (DEBUG) {
            log.info("Ignoring already connected device: " + deviceName);
        }
    }
}

From source file:org.ros.android.android_acm_serial.AcmDeviceActivity.java

/**
 * Creates a new AcmDevice for the newly connected
 *//*  www  .  j  a v  a  2s  .c o  m*/
private void newAcmDevice(UsbDevice usbDevice) {
    try {
        Preconditions.checkNotNull(usbDevice);
        String deviceName = usbDevice.getDeviceName();
        Preconditions.checkState(!acmDevices.containsKey(deviceName),
                "Already connected to device: " + deviceName);
        Preconditions.checkState(usbManager.hasPermission(usbDevice), "Permission denied: " + deviceName);

        UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
        Preconditions.checkNotNull(usbDeviceConnection, "Failed to open device: " + deviceName);
        if (DEBUG) {
            log.info("Adding new ACM device: " + deviceName);
        }
        AcmDevice acmDevice = new AcmDevice(usbDeviceConnection, usbDevice);
        acmDevices.put(deviceName, acmDevice);
        AcmDeviceActivity.this.onPermissionGranted(acmDevice);
    } catch (IllegalStateException e) {
        log.info("A precondition failed: " + e);
    } catch (IllegalArgumentException e) {
        log.info("Failed to create ACM device: " + e);
    }
}

From source file:com.nps.micro.UsbService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Received start id " + startId + ": " + intent);
    List<String> devicesNames = new ArrayList<String>();
    int numberOfDevices = intent.getIntExtra(MainActivity.PACKAGE + '.' + "numberOfDevices", 0);
    for (int i = 0; i < numberOfDevices; i++) {
        devicesNames.add(intent.getStringExtra(MainActivity.PACKAGE + '.' + "device" + i));
    }/* www . j  a va2  s . co  m*/
    HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
    Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
    while (deviceIterator.hasNext()) {
        UsbDevice dev = deviceIterator.next();
        if (devicesNames.contains(dev.getDeviceName())) {
            devices.add(dev);
        }
    }
    initMicrocontrollers();
    status = new Status(getString(R.string.ready), false);
    return START_STICKY;
}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

private void getDeviceName(CallbackContext callbackContext) {
    Activity activity = cordova.getActivity();
    UsbManager mUsbManager = (UsbManager) activity.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> map = mUsbManager.getDeviceList();
    Iterator<UsbDevice> it = map.values().iterator();
    while (it.hasNext()) {
        UsbDevice device = it.next();
        // use device info
        Log.v(POCKETDUINO, "!!!--- USB Device Name ---!!!");
        Log.v(POCKETDUINO, device.getDeviceName());
        Log.v(POCKETDUINO, "!!!--- USB Device Product ID ---!!!");
        Log.v(POCKETDUINO, Integer.toString(device.getProductId()));
        Log.v(POCKETDUINO, "!!!--- USB Device Vendor ID ---!!!");
        Log.v(POCKETDUINO, Integer.toString(device.getVendorId()));
        Log.v(POCKETDUINO, "!!!--- USB Device Hash Code ---!!!");
        Log.v(POCKETDUINO, Integer.toString(device.hashCode()));
    }/*  ww w. j  a va2 s . c om*/
}

From source file:com.dslr.dashboard.PtpService.java

public void searchForUsbCamera() {
    boolean deviceFound = false;
    try {//from ww  w .j  a v  a2 s .c  o m
        if (mUsbDevice == null) {
            Log.d(TAG, "Ptp service usb device not initialized, search for one");

            if (mUsbManager != null) {
                HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
                Log.d(TAG, "Found USB devices count: " + devices.size());
                Iterator<UsbDevice> iterator = devices.values().iterator();
                while (iterator.hasNext()) {
                    UsbDevice usbDevice = iterator.next();
                    Log.d(TAG,
                            "USB Device: " + usbDevice.getDeviceName() + " Product ID: "
                                    + usbDevice.getProductId() + " Vendor ID: " + usbDevice.getVendorId()
                                    + " Interface count: " + usbDevice.getInterfaceCount());
                    for (int i = 0; i < usbDevice.getInterfaceCount(); i++) {
                        UsbInterface intf = usbDevice.getInterface(i);
                        Log.d(TAG, "Interface class: " + intf.getInterfaceClass());
                        if (intf.getInterfaceClass() == android.hardware.usb.UsbConstants.USB_CLASS_STILL_IMAGE) {
                            //mUsbDevice = usbDevice;
                            Log.d(TAG, "Ptp Service imaging usb device found requesting permission");
                            mUsbManager.requestPermission(usbDevice, mPermissionIntent);
                            deviceFound = true;
                            break;
                        }
                    }
                    if (deviceFound)
                        break;
                }
            } else
                Log.d(TAG, "USB Manager is unavailable");

        } else {
            Log.d(TAG, "Ptp service usb imaging device already present");
            //_usbManager.requestPermission(_usbDevice, mPermissionIntent);
        }
    } catch (Exception e) {
        Log.d(TAG, "PtpService search for USB camrea exception: " + e.getMessage());
    }

}

From source file:fr.bmartel.android.fadecandy.service.FadecandyService.java

/**
 * Open USB device & choose USBEndpoint.
 *
 * @param device Usb device object.//from  w  w w  .  j  a v  a  2 s  .  c  om
 * @return Ubs item composed of USBDevice , USBEndpoint & UsbConnection
 */
private UsbItem openDevice(UsbDevice device) {

    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    try {
        UsbDeviceConnection connection = manager.openDevice(device);

        Log.v("USB", "Device name=" + device.getDeviceName());

        UsbInterface intf = null;

        for (int interfaceIndex = 0; interfaceIndex < device.getInterfaceCount(); interfaceIndex++) {

            UsbInterface usbInterface = device.getInterface(interfaceIndex);

            if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_VENDOR_SPEC) {
                intf = usbInterface;
            }
        }

        if (intf == null) {
            intf = device.getInterface(0);
        }

        UsbEndpoint endpoint = intf.getEndpoint(0);

        if (connection != null) {
            connection.claimInterface(intf, true);
        } else {
            Log.e(TAG, "USB connection error");
            return null;
        }

        return new UsbItem(device, connection, endpoint);

    } catch (IllegalArgumentException e) {
    }
    return null;
}

From source file:com.googlecode.android_scripting.facade.USBHostSerialFacade.java

/**
 * usbserialGetDeviceList: {{{1/*  w  w w. jav  a2 s .co m*/
 */
@Rpc(description = "Returns USB devices reported by USB Host API.", returns = "Map of id and string information ',' separated")
public Map<String, String> usbserialGetDeviceList() {
    Map<String, String> ret = new HashMap<String, String>();
    Map<String, UsbDevice> map = mUsbManager.getDeviceList();
    for (Map.Entry<String, UsbDevice> entry : map.entrySet()) {
        UsbDevice dev = entry.getValue();
        String v = "[\"";
        v += dev.getDeviceName();
        v += String.format("\",\"%04X", dev.getVendorId());
        v += String.format("\",\"%04X", dev.getProductId());
        v += "\",\"" + dev.hashCode();
        v += "\"]";
        ret.put(entry.getKey(), v);
    }
    return ret;
}

From source file:marto.rtl_tcp_andro.DeviceOpenActivity.java

/**
 * Opens a certain USB device and prepares an argument to be passed to libusb
 * @param device/*from   ww  w. ja  v a2 s . c o  m*/
 */
@SuppressLint("NewApi")
public void openDevice(final UsbDevice device) {
    final UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    if (device != null && !manager.hasPermission(device)) {
        android.util.Log.d("rtl_tcp_andro", "No permissions for device, requesting!");
        manager.requestPermission(device, permissionIntent);
        return;
    }

    if (device == null || !manager.hasPermission(device))
        finishWithError(err_info.permission_denied);

    final UsbDeviceConnection connection = manager.openDevice(device);

    if (connection == null)
        finishWithError(err_info.unknown_error);

    startBinary(arguments, connection.getFileDescriptor(), properDeviceName(device.getDeviceName()));
}

From source file:com.wordpress.ebc81.rtl_ais_android.DeviceOpenActivity.java

/**
 * Opens a certain USB device and prepares an argument to be passed to libusb
 * @param device/*from w ww  .j ava 2 s  .  c  om*/
 */
//@SuppressLint("NewApi")
public void openDevice(final UsbDevice device) {
    final UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

    if (device != null && !manager.hasPermission(device)) {
        android.util.Log.d("rtl_tcp_andro", "No permissions for device, requesting!");
        manager.requestPermission(device, permissionIntent);
        return;
    }

    if (device == null || !manager.hasPermission(device))
        finishWithError(err_info.permission_denied);

    final UsbDeviceConnection connection = manager.openDevice(device);

    if (connection == null)
        finishWithError(err_info.unknown_error);

    startBinary(arguments, connection.getFileDescriptor(), properDeviceName(device.getDeviceName()));
}