Example usage for android.content Context USB_SERVICE

List of usage examples for android.content Context USB_SERVICE

Introduction

In this page you can find the example usage for android.content Context USB_SERVICE.

Prototype

String USB_SERVICE

To view the source code for android.content Context USB_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.hardware.usb.UsbManager for access to USB devices (as a USB host) and for controlling this device's behavior as a USB device.

Usage

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 a  v a 2s.co m

    // 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: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();/* w  w w  . ja  v a  2 s  . co  m*/
}

From source file:com.rp.podemu.MainActivity.java

public void start_service(View v) {

    try {/*ww  w.jav a  2s  .c  o  m*/
        SerialInterface serialInterface = serialInterfaceBuilder
                .getSerialInterface((UsbManager) getSystemService(Context.USB_SERVICE));
        // reconnect usb

        updateSerialStatus();
        if (serialInterface != null) {
            startService(serviceIntent);

            if (bindService(serviceIntent, serviceConnection, BIND_IMPORTANT)) {
                PodEmuLog.debug("Service succesfully bound");
                serviceBound = true;
            } else {
                PodEmuLog.debug("Service NOT bound");
            }

            updateServiceButton();
        }

    } catch (Exception e) {
        PodEmuLog.printStackTrace(e);
        throw e;
    }

}

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

public void conectarManual(Context ctx) throws TransmisionErrorException {
    Log.i(Constantes.TAG_CONECTOR, "Conectando manualmente. modo - Host");
    /* Get the USB manager from the requesting context */
    this.manager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);

    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Iterator<UsbDevice> it = deviceList.values().iterator();

    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(ctx, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    ctx.registerReceiver(mUsbReceiver, filter);

    if (it.hasNext()) {
        device = it.next();//w w  w.  j av a 2  s  . c o m
        manager.requestPermission(device, mPermissionIntent);
        conectar();
    } else {
        Log.i(Constantes.TAG_CONECTOR, "No se han encontrado dispositivos");
        throw new TransmisionErrorException("No se han encontrado dispositivos");
    }
}

From source file:com.nfc.gemkey.MainActivity.java

@Override
public void onCreate(Bundle icicle) {
    if (DEBUG)//from w ww  . j  a va2s .  c o m
        Log.d(TAG, "onCreate");
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    mAdapter = NfcAdapter.getDefaultAdapter(this);

    // Create a generic PendingIntent that will be deliver to this activity. The NFC stack
    // will fill in the intent with the details of the discovered tag before delivering to
    // this activity.
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Setup an intent filter for all MIME based dispatches
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
    mFilters = new IntentFilter[] { tagDetected };

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

    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED);
    registerReceiver(mUsbReceiver, filter);

    if (getLastNonConfigurationInstance() != null) {
        Log.i(TAG, "open usb accessory@onCreate");
        mAccessory = (UsbAccessory) getLastNonConfigurationInstance();
        openAccessory(mAccessory);
    }

    buttonLED = (ToggleButton) findViewById(R.id.nfc_btn);
    buttonLED.setBackgroundResource(
            buttonLED.isChecked() ? R.drawable.btn_toggle_yes : R.drawable.btn_toggle_no);
    buttonLED.setOnCheckedChangeListener(mKeyLockListener);

    tagId = (TextView) findViewById(R.id.nfc_tag);
    tagId.setText(R.string.nfc_scan_tag);

    // Avoid NetworkOnMainThreadException
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
            .detectNetwork().penaltyLog().build());

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects()
            .detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

    setVolumeControlStream(AudioManager.STREAM_MUSIC);
}

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

public void conectarManual(Context ctx) throws TransmisionErrorException {
    Log.i(C.ROBOT_TAG, "Conectando manualmente. modo - Host");
    /* Get the USB manager from the requesting context */
    this.manager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);

    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
    Iterator<UsbDevice> it = deviceList.values().iterator();

    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(ctx, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    ctx.registerReceiver(mUsbReceiver, filter);

    if (it.hasNext()) {
        device = it.next();//from  www.ja  v a 2  s. c o m
        manager.requestPermission(device, mPermissionIntent);
        //conectar();
    } else {
        Log.i(C.ROBOT_TAG, "No se han encontrado dispositivos");
        throw new TransmisionErrorException("No se han encontrado dispositivos");
    }
}

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    usbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
    registerReceiver(usbDevicePermissionReceiver, new IntentFilter(ACTION_USB_PERMISSION));
    registerReceiver(usbDeviceDetachedReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
    onUsbDeviceAttached(getIntent());/*from w  w w.  j a v a  2  s  .  c  o  m*/
}

From source file:org.chromium.ChromeUsb.java

/**
 * Overridden execute method/*from w w w  .  j  ava 2 s  . c om*/
 * @param action the string representation of the action to execute
 * @param args
 * @param callbackContext the cordova {@link CallbackContext}
 * @return true if the action exists, false otherwise
 * @throws JSONException if the args parsing fails
 */
@Override
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    final JSONObject params = args.getJSONObject(ARG_INDEX_PARAMS);
    final CordovaArgs finalArgs = args;
    Log.d(TAG, "Action: " + action + " params: " + params);

    if (mUsbManager == null) {
        mUsbManager = (UsbManager) webView.getContext().getSystemService(Context.USB_SERVICE);
        mPermissionIntent = PendingIntent.getBroadcast(webView.getContext(), 0,
                new Intent(ACTION_USB_PERMISSION), 0);
    }

    // TODO: Process commands asynchronously on a worker pool thread.
    try {
        if ("getDevices".equals(action)) {
            getDevices(args, params, callbackContext);
            return true;
        } else if ("openDevice".equals(action)) {
            openDevice(args, params, callbackContext);
            return true;
        } else if ("closeDevice".equals(action)) {
            closeDevice(args, params, callbackContext);
            return true;
        } else if ("listInterfaces".equals(action)) {
            listInterfaces(args, params, callbackContext);
            return true;
        } else if ("claimInterface".equals(action)) {
            claimInterface(args, params, callbackContext);
            return true;
        } else if ("releaseInterface".equals(action)) {
            releaseInterface(args, params, callbackContext);
            return true;
        } else if ("controlTransfer".equals(action)) {
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    try {
                        controlTransfer(finalArgs, params, callbackContext);
                    } catch (Exception e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });
            return true;
        } else if ("bulkTransfer".equals(action)) {
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    try {
                        bulkTransfer(finalArgs, params, callbackContext);
                    } catch (Exception e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });
            return true;
        } else if ("interruptTransfer".equals(action)) {
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    try {
                        interruptTransfer(finalArgs, params, callbackContext);
                    } catch (Exception e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });
            return true;
        }
    } catch (UsbError e) {
        callbackContext.error(e.getMessage());
        return true;
    } catch (JSONException e) {
        callbackContext.error(e.getMessage());
        return true;
    }
    return false;
}

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???.//from   ww w.  ja  v a  2 s .  c o  m
    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.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 ww . j a  v a2 s  .  com*/
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;
}