Example usage for android.nfc NfcAdapter ACTION_TECH_DISCOVERED

List of usage examples for android.nfc NfcAdapter ACTION_TECH_DISCOVERED

Introduction

In this page you can find the example usage for android.nfc NfcAdapter ACTION_TECH_DISCOVERED.

Prototype

String ACTION_TECH_DISCOVERED

To view the source code for android.nfc NfcAdapter ACTION_TECH_DISCOVERED.

Click Source Link

Document

Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag.

Usage

From source file:Main.java

/**
 * For Activities which want to treat new Intents as Intents with a new Tag attached. If the given Intent has a Tag extra, the {@link #mTag} and
 * {@link #mUID} will be updated and a Toast message will be shown in the calling Context (Activity). This method will also check if the
 * device/tag supports Mifare Classic (see return values).
 * /*from   w w w. ja v  a 2s.  co m*/
 * @param intent The Intent which should be checked for a new Tag.
 * @param context The Context in which the Toast will be shown.
 * @return <ul>
 *         <li>1 - The device/tag supports Mifare Classic</li>
 *         <li>0 - The device/tag does not support Mifare Classic</li>
 *         <li>-1 - Wrong Intent (action is not "ACTION_TECH_DISCOVERED").</li>
 *         </ul>
 * @see #mTag
 * @see #mUID
 */
public static int treatAsNewTag(Intent intent, Context context) {
    // Check if Intent has a NFC Tag.
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        mTag = tag;
        mUID = tag.getId();

        // Show Toast message with UID.
        //         String id = " (UID: ";
        //         id += byte2HexString(tag.getId());
        //         id += ")";
        //         Toast.makeText(context, id, Toast.LENGTH_LONG).show();

        // Return "1" if device supports Mifare Classic. "0" otherwise.
        return (Arrays.asList(tag.getTechList()).contains(MifareClassic.class.getName())) ? 1 : 0;
    }
    return -1;
}

From source file:jp.co.brilliantservice.android.writertdtext.HomeActivity.java

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

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null) {
        Toast.makeText(getApplicationContext(), "not found NFC feature", Toast.LENGTH_SHORT).show();
        finish();//from w  ww  . j a v  a  2 s . c om
        return;
    }

    if (!mNfcAdapter.isEnabled()) {
        Toast.makeText(getApplicationContext(), "NFC feature is not available", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
    IntentFilter[] intentFilter = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED), };
    String[][] techList = new String[][] { { android.nfc.tech.NdefFormatable.class.getName() },
            { android.nfc.tech.Ndef.class.getName() } };
    mNfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilter, techList);

}

From source file:de.lazyheroproductions.campuscardreader.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // get an instance of the local broadcast manager to receive messages which are send inside the application
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter(CardReaderIntentService.CAMPUS_CARD_INTENT));
    creditTextView = (TextView) findViewById(R.id.credit);
    transactionTextView = (TextView) findViewById(R.id.last_transaction);

    if (BuildConfig.DEBUG) {
        Log.i(this.getClass().getName(), "received intent on create");
    }/* w  ww. j  av  a2s. c  o m*/
    startNfcIntentService(getIntent());

    // intercept all NFC related Intents and redirect them to this activity while this activity is activ and on the front
    // this is called the "foreground dispatch"
    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter nfcTech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    mFilters = new IntentFilter[] { nfcTech };
    mTechLists = new String[][] { new String[] { IsoDep.class.getName() }, { NfcA.class.getName() } };

    //adview related stuff
    //        adView = (AdView) this.findViewById(R.id.adView);
    setUpAdview();
}

From source file:com.macleod2486.androidswissknife.components.NFCTool.java

private void setUpWrite(String message) {

    Log.i("NFCTool", "Message received " + message);
    Intent nfcIntent = new Intent(activity.getApplicationContext(), NFCActivity.class);
    nfcIntent.putExtra("NFCMode", "write");
    nfcIntent.putExtra("NFCMessage", message);
    nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, nfcIntent, 0);

    IntentFilter filter = new IntentFilter();
    filter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
    filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
    filter.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);

    IntentFilter[] filterArray = new IntentFilter[] { filter };

    String[][] techListsArray = new String[][] { new String[] { Ndef.class.getName() },
            new String[] { Ndef.class.getName() } };

    adapter.disableReaderMode(activity);
    adapter.enableForegroundDispatch(activity, pendingIntent, filterArray, techListsArray);

    Toast.makeText(this.activity, "Please scan tag with device.", Toast.LENGTH_LONG).show();
}

From source file:jp.co.brilliantservice.android.writertduri.HomeActivity.java

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    String action = intent.getAction();
    if (TextUtils.isEmpty(action))
        return;/* w  w  w.j a  v  a  2s. c  om*/

    if (!action.equals(NfcAdapter.ACTION_TECH_DISCOVERED))
        return;

    // NdefMessage?
    String uri = "http://www.brilliantservice.co.jp/";
    NdefMessage message = createUriMessage(uri);

    // ??????????
    Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    List<String> techList = Arrays.asList(tag.getTechList());
    if (techList.contains(Ndef.class.getName())) {
        Ndef ndef = Ndef.get(tag);
        writeNdefToNdefTag(ndef, message);

        Toast.makeText(getApplicationContext(), "wrote NDEF to NDEF tag", Toast.LENGTH_SHORT).show();
    } else if (techList.contains(NdefFormatable.class.getName())) {
        NdefFormatable ndef = NdefFormatable.get(tag);
        writeNdefToNdefFormatable(ndef, message);

        Toast.makeText(getApplicationContext(), "wrote NDEF to NDEFFormatable tag", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), "NDEF Not Supported", Toast.LENGTH_SHORT).show();
    }
}

From source file:org.ndeftools.NfcDetectorActivity.java

/**
 * /*from   w w  w  .  j a v a 2s .c o m*/
 * Initialize Nfc fields
 * 
 */

protected void initializeNfc() {
    nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    writeTagFilters = new IntentFilter[] { ndefDetected, tagDetected, techDetected };
}

From source file:jp.co.brilliantservice.android.writertdtext.HomeActivity.java

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    String action = intent.getAction();
    if (TextUtils.isEmpty(action))
        return;/*ww  w .j  a va  2s. c  o m*/

    if (!action.equals(NfcAdapter.ACTION_TECH_DISCOVERED))
        return;

    // NdefMessage?
    String languageCode = "en";
    String text = String.format("Hello, World.(%s)", DATE_FORMAT.format(System.currentTimeMillis()));
    NdefMessage message = createTextMessage(text, languageCode);

    // ??????????
    Tag tag = (Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    List<String> techList = Arrays.asList(tag.getTechList());
    if (techList.contains(Ndef.class.getName())) {
        Ndef ndef = Ndef.get(tag);
        writeNdefToNdefTag(ndef, message);

        Toast.makeText(getApplicationContext(), "wrote NDEF to NDEF tag", Toast.LENGTH_SHORT).show();
    } else if (techList.contains(NdefFormatable.class.getName())) {
        NdefFormatable ndef = NdefFormatable.get(tag);
        writeNdefToNdefFormatable(ndef, message);

        Toast.makeText(getApplicationContext(), "wrote NDEF to NDEFFormatable tag", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getApplicationContext(), "NDEF Not Supported", Toast.LENGTH_SHORT).show();
    }
}

From source file:de.yazo_games.mensaguthaben.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    Log.i(TAG, "activity started");

    FragmentManager fm = getSupportFragmentManager();

    valueFragment = (ValueFragment) fm.findFragmentByTag(VALUE_TAG);
    if (valueFragment == null) {
        valueFragment = new ValueFragment();
    }//from  w w  w  .j  av  a  2 s .  c o m
    fm.beginTransaction().replace(R.id.main, valueFragment, VALUE_TAG).commit();

    if (getIntent().getAction().equals(ACTION_FULLSCREEN)) {
        ValueData valueData = (ValueData) getIntent().getSerializableExtra(EXTRA_VALUE);
        valueFragment.setValueData(valueData);

        setResult(0);

    }

    Boolean autostart = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("autostart", true);
    AutostartRegister.register(getPackageManager(), autostart);

    Toolbar t = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(t);
    ViewCompat.setTransitionName(t, "toolbar");

    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mIntentFilter = new IntentFilter("android.nfc.action.ADAPTER_STATE_CHANGED");

    // 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
    IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    mFilters = new IntentFilter[] { tech, };
    mTechLists = new String[][] { new String[] { IsoDep.class.getName(), NfcA.class.getName() } };

    if (getIntent().getAction().equals(ACTION_FULLSCREEN) && !hasNewData) {
        ValueData valueData = (ValueData) getIntent().getSerializableExtra(EXTRA_VALUE);
        Log.w(TAG, "restoring data for fullscreen");
        valueFragment.setValueData(valueData);

    }
}

From source file:de.lazyheroproductions.campuscardreader.MainActivity.java

private void startNfcIntentService(Intent intent) {
    // as far as I can tell is a double check with
    // NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())
    // unnecessary, but take this as an assumption
    //        if(intent.hasExtra(NfcAdapter.EXTRA_TAG)){
    if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
        Intent newIntent = new Intent(getApplicationContext(), CardReaderIntentService.class);
        newIntent.putExtra(NfcAdapter.EXTRA_TAG, intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));
        startService(newIntent);// ww w . j ava  2s.  c om
    }
}

From source file:org.adictolinux.mfcclone.MainActivity.java

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

    mTitle = mDrawerTitle = getTitle();/*from  w w  w . j av a  2 s .c  o  m*/
    menuTitles = getResources().getStringArray(R.array.menu_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, menuTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mDrawerLayout, /* DrawerLayout object */
            R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open, /* "open drawer" description for accessibility */
            R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mAdapter = NfcAdapter.getDefaultAdapter(this);
    mPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    mFilters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED) };

    // Setup a tech list for all NfcF tags
    mTechLists = new String[][] { new String[] { MifareClassic.class.getName() } };

    // Escondemos el teclado hasta que el user toca una entrada de texto
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    if (savedInstanceState == null) {
        replaceFragment(new ForceKeysFragment(), 0);
    }
}