Example usage for android.hardware.usb UsbManager EXTRA_DEVICE

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

Introduction

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

Prototype

String EXTRA_DEVICE

To view the source code for android.hardware.usb UsbManager EXTRA_DEVICE.

Click Source Link

Document

Name of extra for #ACTION_USB_DEVICE_ATTACHED and #ACTION_USB_DEVICE_DETACHED broadcasts containing the UsbDevice object for 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 {//from  ww  w .j  a va  2 s .c  o  m
            acmDevice.close();
        } catch (RosRuntimeException e) {
            // Ignore spurious errors on disconnect.
        }
    }
    if (DEBUG) {
        log.info("USB device removed: " + deviceName);
    }
}

From source file:es.udc.robotcontrol.testapp.comunication.ConectorPlaca.java

@Override
public void conectar(Context ctx, Intent intent) throws TransmisionErrorException {

    Log.i(Constantes.TAG_CONECTOR, "Conectando a [ " + intent.getAction() + " ]. modo - Host");
    device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    Log.i(Constantes.TAG_CONECTOR, "Conectando a [ " + device.getDeviceName() + " ]. modo - Host");
    /* Get the USB manager from the requesting context */
    this.manager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);
    conectar();/*from  w  w w.  j  a  va  2s  . c  om*/
}

From source file:es.udc.fic.android.robot_control.robot.ConectorPlaca.java

public void conectar(Context ctx, Intent intent) throws TransmisionErrorException {

    Log.i(C.ROBOT_TAG, "Conectando a [ " + intent.getAction() + " ]. modo - Host");
    device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    Log.i(C.ROBOT_TAG, "Conectando a [ " + device.getDeviceName() + " ]. modo - Host");
    /* Get the USB manager from the requesting context */
    this.manager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);
    conectar();/*from  w w  w . j av  a  2 s .co m*/
}

From source file:org.chromium.latency.walt.MainActivity.java

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

    final UsbDevice usbDevice;
    Intent intent = getIntent();/*  w  w w  .j  a v a 2 s .c o m*/
    if (intent != null && intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
        setIntent(null); // done with the intent
        usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    } else {
        usbDevice = null;
    }

    // Connect and sync clocks, but a bit later as it takes time
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (usbDevice == null) {
                waltDevice.connect();
            } else {
                waltDevice.connect(usbDevice);
            }
        }
    }, 1000);

    if (intent != null && AutoRunFragment.TEST_ACTION.equals(intent.getAction())) {
        getSupportFragmentManager().popBackStack("Automated Test", FragmentManager.POP_BACK_STACK_INCLUSIVE);
        Fragment autoRunFragment = new AutoRunFragment();
        autoRunFragment.setArguments(intent.getExtras());
        switchScreen(autoRunFragment, "Automated Test");
    }
}

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   w w  w  .  j  ava2  s . c  o  m*/
        } else if (DEBUG) {
            log.info("Ignoring already connected device: " + deviceName);
        }
    }
}

From source file:svenmeier.coxswain.MainActivity.java

/**
 * Check whether the intent contains a {@link UsbDevice}, and pass it to {@link GymService}.
 *
 * @param intent possible USB device connect
 *//*from w w  w  .ja  v a 2  s . c om*/
private boolean checkUsbDevice(Intent intent) {
    if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) {
        UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (device != null) {
            GymService.start(this, device);

            if (gym.program == null) {
                // try to unlock device - has no effect if this activity is already running :/
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                        | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                        | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            } else {
                // program is already selected so restart workout
                WorkoutActivity.start(this);
            }
        }

        return true;
    }

    return false;
}

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

    // Load raw resources
    for (Pair<Integer, String> ip : mResourcesToLoad) {
        mOpenedResources.add(new ParameterLoaderNode.Resource(
                getResources().openRawResource(ip.first.intValue()), ip.second));
    }/*from  w  w  w.ja  va  2 s .  co m*/

    mSharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    // UI
    initializeUI();

    // USB handling code
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    mUsbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    mUsbAttachedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mLog.info("Received USB Intent");
            if (intent.getAction() == ACTION_USB_PERMISSION
                    && intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                onDeviceReady((UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE));
            }
        }
    };
    mUsbDetachedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mLog.info("Received USB disconnection Intent");
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            onDeviceDetached(device);
        }
    };
}

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scale);//  w w  w . ja  v a 2s  .  com

    Log.v(TAG, "onCreate");

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    mSettings = getSharedPreferences(PREFS, 0);

    mUnitsText = mSettings.getString("unitsText", "grams");
    mUnitsRatio = mSettings.getFloat("unitsRatio", (float) 1.0);

    mTts = new TextToSpeech(this, this);

    mUnitsView = (TextView) findViewById(R.id.text_unit);
    mUnitsView.setText(mUnitsText);

    findViewById(R.id.text_unit).setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            while (mTts.isSpeaking())
                ;
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say Units");
            startActivityForResult(intent, 0);

            if (adView != null) {
                adView.loadAd(new AdRequest());
            }

        }
    });

    mWeightTextView = (TextView) findViewById(R.id.text_weight);
    mWeightTextView.setText("00.00");
    /*
     * TextPaint weightTextPaint = mWeightTextView.getPaint(); CharSequence
     * weightText = mWeightTextView.getText(); while (weightText !=
     * TextUtils.ellipsize(weightText, weightTextPaint,
     * getWindowManager().getDefaultDisplay
     * ().getWidth()*2/3,TextUtils.TruncateAt.END)) {
     * weightTextPaint.setTextSize(weightTextPaint.getTextSize() - 1); }
     */

    mWeightTextView.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Zero'd", Toast.LENGTH_LONG).show();
            mZeroGrams = mWeightGrams;
            if (adView != null) {
                adView.loadAd(new AdRequest());
            }
        }
    });
    mWeightTextView.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            mZeroGrams = 0;
            Toast.makeText(getApplicationContext(), "Reset", Toast.LENGTH_LONG).show();
            if (adView != null) {
                adView.loadAd(new AdRequest());
            }
            return true;
        }
    });

    disableAdsText = (TextView) findViewById((R.id.text_disableAds));
    disableAdsText.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            new AlertDialog.Builder(ScaleActivity.this).setTitle("Keep Software Free and Open Source")
                    .setMessage("Ads help support further development, but they are OPTIONAL."
                            + " If you choose to disable ads, please consider donating. All dontations"
                            + " go towards purchasing hardware for open source development. "
                            + "Disabling ads or donating will not change the features availble in this app."
                            + " Thank you. rob@theultimatelabs.com")
                    .setPositiveButton("Disable Ads", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            adLayout.removeAllViews();
                            adView.removeAllViews();
                            disableAdsText.setVisibility(View.INVISIBLE);
                            mSettings.edit().putBoolean("ads", false).commit();
                            adView = null;
                        }
                    }).setCancelable(true).setNegativeButton("Keep Ads", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).setNeutralButton("Disable Ads + Donate", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            adLayout.removeAllViews();
                            adView.removeAllViews();
                            disableAdsText.setVisibility(View.INVISIBLE);
                            mSettings.edit().putBoolean("ads", false).commit();
                            adView = null;
                            startActivity(new Intent(Intent.ACTION_VIEW,
                                    Uri.parse("http://blog.theultimatelabs.com/p/donate.html")));
                        }
                    }).show();
        }
    });

    TextView aboutText = (TextView) findViewById((R.id.text_about));
    aboutText.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(getApplicationContext(), AboutActivity.class));
        }
    });

    /*
     * .setMessage() new AlertDialog.Builder(this) .setMessage(mymessage)
     * .setTitle(title) .setCancelable(true)
     * .setNeutralButton(android.R.string.cancel, new
     * DialogInterface.OnClickListener() { public void
     * onClick(DialogInterface dialog, int whichButton){} }) .show(); }}
     */
    // /

    mDensitiesJson = loadJsonResource(R.raw.densities);
    mVolumesJson = loadJsonResource(R.raw.volumes);
    mWeightsJson = loadJsonResource(R.raw.weights);

    // Initiate a generic request to load it with an ad
    if (mSettings.getBoolean("ads", true)) {
        // Create the adViewj
        adView = new AdView(this, AdSize.SMART_BANNER, "a15089dfb39c5a8");

        // Log.w(TAG, new Integer(R.id.layout_ads).toString());
        adLayout = (LinearLayout) findViewById(R.id.layout_ads);

        // Add the adView to it
        adLayout.addView(adView, 0);
        disableAdsText.setVisibility(View.VISIBLE);
    } else {
        disableAdsText.setVisibility(View.INVISIBLE);
        adView = null;
    }

    Intent intent = getIntent();
    mDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

    findScale();

}

From source file:org.chromium.ChromeUsb.java

private void openDevice(CordovaArgs args, JSONObject params, final CallbackContext callbackContext)
        throws JSONException, UsbError {
    // First recover the device object from Id.
    int devId = params.getInt("device");
    ConnectedDevice dev = null;/* w  w  w.  j av  a2 s  .com*/
    int vid = -1, pid = -1;
    {
        HashMap<String, UsbDevice> devices = mUsbManager.getDeviceList();
        UsbDevice usbDev = null;
        for (UsbDevice d : devices.values()) {
            if (d.getDeviceId() == devId) {
                usbDev = d;
                break;
            }
        }
        if (usbDev != null) {
            if (mUsbReceiver == null) {
                mUsbReceiver = new BroadcastReceiver() {
                    public void onReceive(Context context, Intent intent) {
                        String action = intent.getAction();
                        if (ACTION_USB_PERMISSION.equals(action)) {
                            synchronized (this) {
                                UsbDevice device = (UsbDevice) intent
                                        .getParcelableExtra(UsbManager.EXTRA_DEVICE);

                                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                                    if (device != null) {
                                        UsbDeviceConnection usbConn = mUsbManager.openDevice(device);
                                        if (usbConn == null) {
                                            throw new UsbError(
                                                    "UsbManager.openDevice returned null opening " + device);
                                        }
                                        ConnectedDevice dev = new RealDevice(device, usbConn);
                                        int vid = device.getVendorId();
                                        int pid = device.getProductId();

                                        if (dev == null || vid < 0 || pid < 0) {
                                            throw new UsbError("Unknown device ID: " + device);
                                        }
                                        int handle = mNextConnectionId++;
                                        mConnections.put(handle, dev);
                                        JSONObject jsonHandle = new JSONObject();
                                        try {
                                            jsonHandle.put("handle", handle);
                                            jsonHandle.put("vendorId", vid);
                                            jsonHandle.put("productId", pid);
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                        callbackContext.success(jsonHandle);
                                    }
                                } else {
                                    Log.d(TAG, "permission denied for device " + device);
                                }
                            }
                        }
                    }
                };

                IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                webView.getContext().registerReceiver(mUsbReceiver, filter);
            }

            mUsbManager.requestPermission(usbDev, mPermissionIntent);
        } else if (devId == FakeDevice.ID) {
            dev = new FakeDevice();
            vid = FakeDevice.VID;
            pid = FakeDevice.PID;

            if (dev == null || vid < 0 || pid < 0) {
                throw new UsbError("Unknown device ID: " + devId);
            }
            int handle = mNextConnectionId++;
            mConnections.put(handle, dev);
            JSONObject jsonHandle = new JSONObject();
            jsonHandle.put("handle", handle);
            jsonHandle.put("vendorId", vid);
            jsonHandle.put("productId", pid);
            callbackContext.success(jsonHandle);
        }
    }
}

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

private void onUsbDeviceAttached(Intent intent) {
    if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
        UsbDevice usbDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        onDeviceReady(usbDevice);/*from   w  ww.  j  av  a 2 s .  co  m*/
    }
}