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:com.wordpress.ebc81.rtl_ais_android.DeviceOpenActivity.java

/**
 * Opens a certain USB device and prepares an argument to be passed to libusb
 * @param device// w  ww .  j  ava 2s  .co  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.grupohqh.carservices.operator.ManipulateCarActivity.java

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

    if (getIntent().getExtras().containsKey("userId"))
        userId = getIntent().getExtras().getInt("userId");
    if (getIntent().getExtras().containsKey("carOwnerId"))
        carOwnerId = getIntent().getExtras().getInt("carOwnerId");

    CARBRAND_URL = getString(R.string.base_url) + getString(R.string.carbrands_url);
    CARLINE_URL = getString(R.string.base_url) + getString(R.string.carmodels_url);
    SAVECAR_URL = getString(R.string.base_url) + "savecar/";
    SAVEPHOTO_URL = getString(R.string.base_url) + "savephoto/";
    hasCamera = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);

    etTagNumber = (EditText) findViewById(R.id.etTagNumber);
    etUsernameOwner = (EditText) findViewById(R.id.etUsernameOwner);
    etCarBrand = (EditText) findViewById(R.id.etCarBrand);
    etCarModel = (EditText) findViewById(R.id.etCarModel);
    etCarYear = (EditText) findViewById(R.id.etCarYear);
    etColor = (EditText) findViewById(R.id.etCarColor);
    etSerialNumber = (EditText) findViewById(R.id.etSerialNumber);
    etLicensePlate = (EditText) findViewById(R.id.etLicensePlate);
    etKM = (EditText) findViewById(R.id.etKm);
    imgCar = (ImageView) findViewById(R.id.imgCar);
    spCarBrand = (Spinner) findViewById(R.id.spCarBrand);
    spCarModel = (Spinner) findViewById(R.id.spCarModel);
    btnSaveCar = (Button) findViewById(R.id.btnSaveCar);
    btnTakePicture = (Button) findViewById(R.id.btnTakePicture);
    bar = (ProgressBar) findViewById(R.id.progressBar);
    viewUsername = findViewById(R.id.viewUsername);

    if (savedInstanceState != null) {
        bm = savedInstanceState.getParcelable("bm");
        if (bm != null)
            imgCar.setImageBitmap(bm);/*from   w  w  w. ja  v a2  s .  co m*/
    }

    bar.setVisibility(View.GONE);
    etCarBrand.setVisibility(View.GONE);
    etCarModel.setVisibility(View.GONE);
    viewUsername.setVisibility(carOwnerId == 0 ? View.VISIBLE : View.GONE);

    btnTakePicture.setEnabled(hasCamera);
    btnTakePicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(ManipulateCarActivity.this)));
            startActivityForResult(intent, CAPTURE_IMAGE);
        }
    });

    btnSaveCar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (validate())
                new SaveCarAsyncTask().execute(SAVECAR_URL);
        }
    });

    spCarBrand.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0)
                ((TextView) parent.getChildAt(position)).setTextColor(Color.GRAY);
            if (mapBrands.get(brands.get(position)) == otherId) {
                etCarBrand.setVisibility(View.VISIBLE);
                etCarModel.setVisibility(View.VISIBLE);
                spCarModel.setVisibility(View.GONE);
                etCarBrand.requestFocus();
            } else {
                etCarBrand.setVisibility(View.GONE);
                etCarModel.setVisibility(View.GONE);
                spCarModel.setVisibility(View.VISIBLE);
                new LoadCarModelsAsyncTask().execute(CARLINE_URL + mapBrands.get(brands.get(position)) + "/");
                reload = true;
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    spCarModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (position == 0)
                ((TextView) parent.getChildAt(position)).setTextColor(Color.GRAY);
            if (mapModels.get(models.get(position)) == otherId) {
                etCarModel.setVisibility(View.VISIBLE);
                etCarModel.requestFocus();
            } else {
                etCarModel.setVisibility(View.GONE);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    if (useMiniMe) {
        manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        usbCommunication = UsbCommunication.newInstance();

        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED); // will intercept by system
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        filter.addAction(ACTION_USB_PERMISSION);
        registerReceiver(usbReceiver, filter);

        etEPC = (EditText) findViewById(R.id.etEPC);
        btnReadTAG = (Button) findViewById(R.id.btnReadTAG);
        txtStatus = (TextView) findViewById(R.id.txtStatus);
        btnReadTAG.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etEPC.setText("");
                if (txtStatus.getText().toString().equals("conectado")) {
                    readTag();
                } else {
                    Toast.makeText(getBaseContext(), "dispositivo " + txtStatus.getText(), Toast.LENGTH_LONG)
                            .show();
                }
            }
        });
    }

    etKM.addTextChangedListener(new NumberTextWatcher(etKM));
}

From source file:com.numato.usbgenie.ui.MainActivity.java

/**
 * Using the Context.USB_SERVICE it enumreates all the NUMATOLAB devices
 *
 * @return the number of devices enumerated
 *///from  ww  w . j a va  2s . c  om
private int enumerateDevices() {
    int index = 0;

    UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
    ArrayList<UsbDevice> cdcAcmDevices = CdcAcmDriver.ListDevices(manager);

    mDevicesManager.clearDevices();

    ArrayList<Integer> supportedDevices = NumatoUSBDevice.GetSupportedProductIds();

    if (!cdcAcmDevices.isEmpty()) {
        for (UsbDevice cdcAcmDevice : cdcAcmDevices) {
            int vendorId = cdcAcmDevice.getVendorId();

            if (vendorId == NumatoUSBDevice.VID_NUMATOLAB
                    && supportedDevices.contains(cdcAcmDevice.getProductId())) {
                mDevicesManager.addDevice(new NumatoUSBDevice(index, cdcAcmDevice, manager));
                index++;
            }
        }
    }

    return index;
}

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.  j a  va 2s.co  m*/
    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:tv.piratemedia.flightcontroller.BluetoothComputerFragment.java

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    mTransactionView = (ListView) view.findViewById(R.id.list);

    throttle = (VerticalProgressBar) view.findViewById(R.id.throttle);
    LedColor = (ImageView) view.findViewById(R.id.LedColor);

    rollLeft = (ProgressBar) view.findViewById(R.id.rollLeft);
    rollRight = (ProgressBar) view.findViewById(R.id.rollRight);
    pitchForward = (VerticalProgressBar) view.findViewById(R.id.pitchForward);
    pitchBackward = (VerticalProgressBar) view.findViewById(R.id.pitchBackward);
    yawLeft = (ProgressBar) view.findViewById(R.id.yawLeft);
    yawRight = (ProgressBar) view.findViewById(R.id.yawRight);

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

    if (getActivity().getLastNonConfigurationInstance() != null) {
        mAccessory = (UsbAccessory) getActivity().getLastNonConfigurationInstance();
        openAccessory(mAccessory);//from w  w w  .j  a  v a  2s  . co  m
    }

    setupAtimiter();
    setupGPS();
    //setupGyro();
    setupAccel();
}

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

private void initUsbService() {
    usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    initSupportedDeviceIds();
    initUsbDevices();
}

From source file:com.hoho.android.usbserial.examples.SerialConsoleActivity.java

@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "Resumed, port=" + sPort);
    if (sPort == null) {
        mTitleTextView.setText("No serial device.");
    } else {/*from  ww  w .jav  a 2  s . c o  m*/
        final UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        UsbDeviceConnection connection = usbManager.openDevice(sPort.getDriver().getDevice());
        if (connection == null) {
            mTitleTextView.setText("Opening device failed");
            return;
        }

        try {
            sPort.open(connection);
            sPort.setParameters(57600, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1,
                    UsbSerialPort.PARITY_NONE);
        } catch (IOException e) {
            Log.e(TAG, "Error setting up device: " + e.getMessage(), e);
            mTitleTextView.setText("Error opening device: " + e.getMessage());
            try {
                sPort.close();
            } catch (IOException e2) {
                // Ignore.
            }
            sPort = null;
            return;
        }
        mTitleTextView.setText("Serial device: " + sPort.getClass().getSimpleName());
    }
    onDeviceStateChange();
}

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 av a  2 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.rp.podemu.MainActivity.java

private void updateSerialStatus() {
    SerialInterface serialInterface = serialInterfaceBuilder
            .getSerialInterface((UsbManager) getSystemService(Context.USB_SERVICE));
    if (serialInterface != null) {
        this.serialStatusText.setTextColor(Color.rgb(0x00, 0xff, 0x00));
        this.serialStatusText.setText("connected");

        this.serialStatusHint.setText(String.format("VID: 0x%04X, ", serialInterface.getVID())
                + String.format("PID: 0x%04X\n", serialInterface.getPID()) + "Cable: "
                + serialInterface.getName());
    } else {/*from w ww . jav a  2 s .  c  om*/
        this.serialStatusText.setTextColor(Color.rgb(0xff, 0x00, 0x00));
        this.serialStatusText.setText("disconnected");

        this.serialStatusHint.setText(R.string.serial_status_hint);
    }
}