Example usage for android.nfc NfcAdapter EXTRA_TAG

List of usage examples for android.nfc NfcAdapter EXTRA_TAG

Introduction

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

Prototype

String EXTRA_TAG

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

Click Source Link

Document

Mandatory extra containing the Tag that was discovered for the #ACTION_NDEF_DISCOVERED , #ACTION_TECH_DISCOVERED , and #ACTION_TAG_DISCOVERED intents.

Usage

From source file:Main.java

public static String[] getTagType(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    String[] techList = tag.getTechList();
    return techList;
}

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 ww.  ja v  a2s  . c  o  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:root.gast.playground.speech.activation.util.TagWriterExecutionActivity.java

private void doWriting() {
    Intent intent = getIntent();/*from  www.ja va2s  .  co m*/
    if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
        Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        NfcUtil.writeTag(this, createMimeTypeMessage(), detectedTag);
    }
    finish();
}

From source file:com.hybris.mobile.activity.NFCWriteActivity.java

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

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    // default failed
    setResult(RESULT_CANCELED);/* www. j  av a  2 s. co  m*/

    if (NFCUtil.supportsNdef(tag)) {
        Ndef ndef = Ndef.get(tag);

        try {
            int maxSize = ndef.getMaxSize();
            int messageSize = this.msg.toByteArray().length;

            if (ndef.isWritable() && maxSize > messageSize) {
                ndef.connect();
                ndef.writeNdefMessage(this.msg);

                if (getResources().getBoolean(R.bool.nfc_read_only)) {
                    if (ndef.canMakeReadOnly()) {
                        boolean success = ndef.makeReadOnly();
                        if (!success)
                            Toast.makeText(this, "Unable to make tag readonly!", Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(this, "Tag is now readonly!", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(this, "This tag cannot be made readonly!", Toast.LENGTH_LONG).show();
                    }
                }

                setResult(RESULT_OK);
                showDialogFragmentWithMessage(R.string.nfc_tag_written);
            } else {
                showDialogFragmentWithMessage(R.string.error_nfc_readonly_size);
            }
        } catch (IOException e) {
            LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_io), e, Hybris.getAppContext());

        } catch (FormatException e) {
            LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_format), e, Hybris.getAppContext());
        } finally {
            try {
                ndef.close();
            } catch (IOException e) {
            }
        }

    } else if (NFCUtil.supportsNdefFormatable(tag)) {
        NdefFormatable ndefFormatable = NdefFormatable.get(tag);

        try {
            ndefFormatable.connect();

            if (getResources().getBoolean(R.bool.nfc_read_only)) {
                ndefFormatable.formatReadOnly(this.msg);
            } else {
                ndefFormatable.format(this.msg);
            }

            setResult(RESULT_OK);
            showDialogFragmentWithMessage(R.string.nfc_tag_written);
        } catch (IOException e) {
            LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_io), e, Hybris.getAppContext());
        } catch (FormatException e) {
            LoggingUtils.e(LOG_CAT, getString(R.string.error_nfc_format), e, Hybris.getAppContext());
        } finally {
            try {
                ndefFormatable.close();
            } catch (IOException e) {
            }
        }

    } else {
        showDialogFragmentWithMessage(R.string.error_nfc_no_ndef);
    }

}

From source file:com.saarang.samples.apps.iosched.ui.NfcBadgeActivity.java

@Override
public void onStart() {
    super.onStart();
    AnalyticsManager.initializeAnalyticsTracker(getApplicationContext());
    // Check for NFC data
    Intent i = getIntent();//from  w  w w .  j  a  v  a  2 s  . c  o  m
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
        LogUtils.LOGI(TAG, "Badge detected");
        /* [ANALYTICS:EVENT]
         * TRIGGER:   Scan another attendee's badge.
         * CATEGORY:  'NFC'
         * ACTION:    'Read'
         * LABEL:     'Badge'. Badge info IS NOT collected.
         * [/ANALYTICS]
         */
        AnalyticsManager.sendEvent("NFC", "Read", "Badge");
        readTag((Tag) i.getParcelableExtra(NfcAdapter.EXTRA_TAG));
    } else if (ACTION_SIMULATE.equals(i.getAction()) && Config.IS_DOGFOOD_BUILD) {
        String simulatedUrl = i.getDataString();
        LogUtils.LOGD(TAG, "Simulating badge scanning with URL " + simulatedUrl);
        // replace https by Unicode character 4, as per normal badge encoding rules
        recordBadge(simulatedUrl.replace("https://", "\u0004"));
    } else {
        LogUtils.LOGW(TAG, "Invalid action in Intent to NfcBadgeActivity: " + i.getAction());
    }
    finish();
}

From source file:com.google.samples.apps.iosched.ui.NfcBadgeActivity.java

@Override
public void onStart() {
    super.onStart();
    AnalyticsManager.initializeAnalyticsTracker(getApplicationContext());
    // Check for NFC data
    Intent i = getIntent();/*from  w w w.  j  av  a  2  s  .  c om*/
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
        LOGI(TAG, "Badge detected");
        /* [ANALYTICS:EVENT]
         * TRIGGER:   Scan another attendee's badge.
         * CATEGORY:  'NFC'
         * ACTION:    'Read'
         * LABEL:     'Badge'. Badge info IS NOT collected.
         * [/ANALYTICS]
         */
        AnalyticsManager.sendEvent("NFC", "Read", "Badge");
        readTag((Tag) i.getParcelableExtra(NfcAdapter.EXTRA_TAG));
    } else if (ACTION_SIMULATE.equals(i.getAction()) && Config.IS_DOGFOOD_BUILD) {
        String simulatedUrl = i.getDataString();
        LOGD(TAG, "Simulating badge scanning with URL " + simulatedUrl);
        // replace https by Unicode character 4, as per normal badge encoding rules
        recordBadge(simulatedUrl.replace("https://", "\u0004"));
    } else {
        LOGW(TAG, "Invalid action in Intent to NfcBadgeActivity: " + i.getAction());
    }
    finish();
}

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

@Override
protected void onHandleIntent(Intent intent) {
    if (BuildConfig.DEBUG) {
        Log.i(this.getClass().getName(), "CardReaderService started");
    }/*from w w w .  j ava 2s. c  o  m*/

    // get an instance of the nfc tag to communicate
    IsoDep isodep = IsoDep.get((Tag) intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));
    try {
        // connect to the nfc tag
        isodep.connect();
        // select application which contains the credit and last transaction
        resultOk = isodep.transceive(selectAid);
        if (resultOk[0] == 0) {
            // get the credit
            creditBytes = isodep.transceive(creditPayload);
            // get the last transaction
            lastTransactionBytes = isodep.transceive(transactionPayload);
        } else {
            if (BuildConfig.DEBUG) {
                Log.w(this.getClass().getName(), "Wrong result: " + arrayToString(resultOk));
            }
        }
        isodep.close();
    } catch (IOException e) {
        if (BuildConfig.DEBUG) {
            Log.e(this.getClass().getName(), e.getMessage());
        }
        unknownErrorBool = true;
    } catch (NullPointerException e) {
        if (BuildConfig.DEBUG) {
            Log.e(this.getClass().getName(), e.getMessage());
        }
        unknownCampusCardErrorBool = true;
    }

    // send the gathered data back to the activity
    sendBroadcast();
}

From source file:com.dimasdanz.kendalipintu.NFCOpenDoorActivity.java

private void handleIntent(Intent intent) {
    String action = intent.getAction();
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
        String type = intent.getType();
        if (MIME_TEXT_PLAIN.equals(type)) {
            Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (SharedPreferencesManager.getLoggedInPrefs(getApplicationContext())) {
                new NdefReaderTask().execute(tag);
            } else {
                Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(i);// w  w  w.  j  ava 2 s  .  c o m
            }
        } else {
            Log.d(TAG, "Wrong mime type: " + type);
        }
    }
}

From source file:ru.tinkoff.acquiring.sdk.nfc.NfcCardScanActivity.java

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

    try {/*w w w.  jav  a  2 s  . c  om*/
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        Card card = new CardParser().parse(tag);

        if (card != null) {
            Intent resultIntent = new Intent();
            resultIntent.putExtra(EXTRA_CARD, card);

            setResult(Activity.RESULT_OK, resultIntent);
            finish();
        }
    } catch (IOException e) {
        Journal.log(e);
    }
}

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;/*from w  w w. ja  v a 2  s.com*/

    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();
    }
}