Example usage for android.hardware.usb UsbManager getDeviceList

List of usage examples for android.hardware.usb UsbManager getDeviceList

Introduction

In this page you can find the example usage for android.hardware.usb UsbManager getDeviceList.

Prototype

@RequiresFeature(PackageManager.FEATURE_USB_HOST)
public HashMap<String, UsbDevice> getDeviceList() 

Source Link

Document

Returns a HashMap containing all USB devices currently attached.

Usage

From source file:info.martinmarinov.dvbservice.DeviceChooserActivity.java

private void handleException(DvbException e) {
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    Collection<UsbDevice> availableDevices = manager.getDeviceList().values();
    int[] productIds = new int[availableDevices.size()];
    int[] vendorIds = new int[availableDevices.size()];

    int id = 0;/*from   w w w . ja v a 2s  .c  om*/
    for (UsbDevice usbDevice : availableDevices) {
        productIds[id] = usbDevice.getProductId();
        vendorIds[id] = usbDevice.getVendorId();
        id++;
    }

    response.putExtra(CONTRACT_ERROR_CODE, e.getErrorCode().name());
    response.putExtra(CONTRACT_RAW_TRACE, StackTraceSerializer.serialize(e));
    response.putExtra(CONTRACT_USB_PRODUCT_IDS, productIds);
    response.putExtra(CONTRACT_USB_VENDOR_IDS, vendorIds);

    finishWith(RESULT_ERROR);
}

From source file:com.android.usbtuner.UsbInputController.java

/**
 * See if any USB tuner hardware is attached in the system.
 *
 * @param context {@link Context} instance
 * @return {@code true} if any tuner device we support is plugged in
 */// w w w . j ava  2  s.  co  m
private boolean isTunerConnected(Context context) {
    UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
    Map<String, UsbDevice> deviceList = manager.getDeviceList();
    for (UsbDevice device : deviceList.values()) {
        if (DEBUG) {
            Log.d(TAG, "Device: " + device);
        }
        for (TunerDevice tuner : TUNER_DEVICES) {
            if (tuner.equals(device)) {
                Log.i(TAG, "Tuner found");
                return true;
            }
        }
    }
    return false;
}

From source file:org.deviceconnect.android.deviceplugin.fabo.setting.fragment.FaBoConnectFragment.java

@Override
public void onResume() {
    super.onResume();

    // USB????Broadcast Receiver.
    IntentFilter mIntentFilter = new IntentFilter();
    mIntentFilter.addAction(FaBoConst.DEVICE_TO_ARDUINO_OPEN_USB_RESULT);
    mIntentFilter.addAction(FaBoConst.DEVICE_TO_ARDUINO_CHECK_USB_RESULT);
    mIntentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    mContext.registerReceiver(mUSBResultEvent, mIntentFilter);

    // Arduino Uno???.
    Intent intent = new Intent(FaBoConst.DEVICE_TO_ARDUINO_CHECK_USB);
    mContext.sendBroadcast(intent);/*from w  w  w .j av  a 2s  .  com*/

    // USB???.
    UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();

    for (UsbDevice device : deviceList.values()) {
        if (device.getVendorId() == 10755) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mTextViewCommment.setText(R.string.arduinoorg_find);
                    mOutputButton.setEnabled(false);
                }
            });
        } else if (device.getVendorId() == 9025) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mTextViewCommment.setText(R.string.arduinocc_find);
                    mOutputButton.setEnabled(true);
                }
            });
            break;
        }
    }
}

From source file:com.wordpress.ebc81.rtl_ais_android.tools.DialogManager.java

@SuppressLint("NewApi")
private Dialog genUSBDeviceDialog() {

    try {/*from   w w  w  .  j a v  a 2s  .c om*/
        if (!(getActivity() instanceof DeviceOpenActivity))
            return null;
        final DeviceOpenActivity sdrviewer = (DeviceOpenActivity) getActivity();

        final UsbManager manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
        final HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        if (deviceList.isEmpty()) {
            sdrviewer.finishWithError(err_info.no_devices_found);
            return null;
        }
        final String[] options = new String[deviceList.size()];
        int i = 0;
        for (final String s : deviceList.keySet())
            options[i++] = s;

        return new AlertDialog.Builder(getActivity()).setItems(options, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                final String selected = options[which];

                final UsbDevice selected_device = deviceList.get(selected);

                sdrviewer.openDevice(selected_device);
            }
        }).setOnCancelListener(new AlertDialog.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                sdrviewer.finishWithError(err_info.no_devices_found);
            }
        }).setTitle(R.string.choose_device).create();
    } catch (Throwable t) {
        t.printStackTrace();
        return null;
    }
}

From source file:org.deviceconnect.android.deviceplugin.fabo.setting.fragment.FaBoFirmwareFragment.java

@Override
public void onResume() {
    super.onResume();

    // SerialPort??
    mStkWriter = new StkWriter(getActivity().getBaseContext());
    mStkWriter.setListener(this);

    // USB???.// w  ww .j a va2 s .  com
    UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();

    for (UsbDevice device : deviceList.values()) {
        if (device.getVendorId() == 10755) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mTextViewCommment.setText(R.string.arduinoorg_find);
                    mButtonConnect.setEnabled(false);
                    mButtonSend.setVisibility(Button.INVISIBLE);
                    mButtonBack.setVisibility(Button.INVISIBLE);
                }
            });
        } else if (device.getVendorId() == 9025) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mTextViewCommment.setText(R.string.arduinocc_find_sendfirmware);
                    mButtonConnect.setEnabled(true);
                    mButtonSend.setVisibility(Button.INVISIBLE);
                    mButtonBack.setVisibility(Button.INVISIBLE);
                }
            });
            break;
        }
    }
}

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();//from   w w  w  . java  2 s  .c o  m
        // 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()));
    }
}

From source file:com.ekumen.tangobot.application.MainActivity.java

@Override
protected void init(NodeMainExecutor nodeMainExecutor) {
    mLog.info("MainActivity init");

    // Store a reference to the NodeMainExecutor
    mNodeMainExecutor = nodeMainExecutor;

    mMasterUri = getMasterUri();//from   w w w. ja va 2s .  com
    mHostName = getRosHostname();

    mLog.info(mMasterUri);
    updateMasterUriUI(mMasterUri.toString());

    // Trigger asking permission to access any devices that are already connected
    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    for (UsbDevice device : manager.getDeviceList().values()) {
        manager.requestPermission(device, mUsbPermissionIntent);
    }

    checkRosMasterConnection();
    configureParameterServer();

    startBaseControllerNode();

    startExtrinsicsPublisherNodes();
    startMapServerNode();

    // Start Tango node and navigation stack.
    startTangoRosNode();
    startMoveBaseNode();
}

From source file:com.theultimatelabs.scale.ScaleActivity.java

public void findScale() {

    if (mDevice == null) {
        UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while (deviceIterator.hasNext()) {
            mDevice = deviceIterator.next();
            Log.v(TAG, String.format(
                    "name=%s deviceId=%d productId=%d vendorId=%d deviceClass=%d subClass=%d protocol=%d interfaceCount=%d",
                    mDevice.getDeviceName(), mDevice.getDeviceId(), mDevice.getProductId(),
                    mDevice.getVendorId(), mDevice.getDeviceClass(), mDevice.getDeviceSubclass(),
                    mDevice.getDeviceProtocol(), mDevice.getInterfaceCount()));
            break;
        }/* w  w  w.j ava2  s  . c o m*/
    }

    if (mDevice != null) {
        new ScaleListener().execute();
    } else {
        new AlertDialog.Builder(ScaleActivity.this).setTitle("Scale Not Found")
                .setMessage("Please connect scale with OTG cable and turn the scale on").setCancelable(false)
                .setPositiveButton("Try Again", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        findScale();
                    }

                }).setNegativeButton("Close", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                }).setNeutralButton("Buy Scale", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                                Uri.parse("http://theultimatelabsstore.blogspot.com/p/store.html"));
                        startActivity(browserIntent);

                    }

                }).show();
    }

}

From source file:com.swiftnav.piksidroid.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.setupUI();

    UsbManager mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver, filter);

    filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
    registerReceiver(mUsbReceiverDisconnect, filter);

    HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();

    for (UsbDevice device : deviceList.values()) {
        if ((device.getVendorId() == Utils.PIKSI_VID) && (device.getProductId() == Utils.PIKSI_PID))
            if (!mUsbManager.hasPermission(device)) {
                mUsbManager.requestPermission(device, mPermissionIntent);
            } else {
                ((EditText) findViewById(R.id.console)).setText("");
                piksiConnected(device);//from w  w  w .jav  a  2  s.  c  o  m
            }
    }
}

From source file:com.amaze.carbonfilemanager.activities.MainActivity.java

/**
 * Method finds whether a USB device is connected or not
 * @return true if device is connected// www  . j  a v  a2s .  c  o m
 */
private boolean isUsbDeviceConnected() {
    UsbManager usbManager = (UsbManager) getSystemService(USB_SERVICE);
    if (usbManager.getDeviceList().size() != 0) {
        // we need to set this every time as there is no way to know that whether USB device was
        // disconnected after closing the app and another one was connected
        // in that case the uri will obviously change
        // other wise we could persist the uri even after reopening the app by not writing
        // this preference when it's not null
        sharedPref.edit().putString(KEY_PREF_OTG, VALUE_PREF_OTG_NULL).apply();
        return true;
    } else {
        sharedPref.edit().putString(KEY_PREF_OTG, null).apply();
        return false;
    }
}