Example usage for android.nfc Tag getId

List of usage examples for android.nfc Tag getId

Introduction

In this page you can find the example usage for android.nfc Tag getId.

Prototype

public byte[] getId() 

Source Link

Document

Get the Tag Identifier (if it has one).

Usage

From source file:Main.java

public static void setTag(Tag tag) {
    mTag = tag;
    mUID = tag.getId();
}

From source file:Main.java

public static String getTagUuid(Tag tag) {
    byte[] tagIDBytes = tag.getId();
    return mBytesToHexString(tagIDBytes);
}

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).
 * // ww  w.  j ava  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:Main.java

static JSONObject ndefToJSON(Ndef ndef) {
    JSONObject json = new JSONObject();

    if (ndef != null) {
        try {/*w  w w. j  av  a 2s .c  om*/

            Tag tag = ndef.getTag();
            // tag is going to be null for NDEF_FORMATABLE until NfcUtil.parseMessage is refactored
            if (tag != null) {
                json.put("id", byteArrayToJSON(tag.getId()));
                json.put("techTypes", new JSONArray(Arrays.asList(tag.getTechList())));
            }

            json.put("type", translateType(ndef.getType()));
            json.put("maxSize", ndef.getMaxSize());
            json.put("isWritable", ndef.isWritable());
            json.put("ndefMessage", messageToJSON(ndef.getCachedNdefMessage()));
            // Workaround for bug in ICS (Android 4.0 and 4.0.1) where
            // mTag.getTagService(); of the Ndef object sometimes returns null
            // see http://issues.mroland.at/index.php?do=details&task_id=47
            try {
                json.put("canMakeReadOnly", ndef.canMakeReadOnly());
            } catch (NullPointerException e) {
                json.put("canMakeReadOnly", JSONObject.NULL);
            }
        } catch (JSONException e) {
            Log.e(TAG, "Failed to convert ndef into json: " + ndef.toString(), e);
        }
    }
    return json;
}

From source file:com.codebutler.farebot.mifare.Card.java

public static Card dumpTag(byte[] tagId, Tag tag) throws Exception {
    final String[] techs = tag.getTechList();
    if (ArrayUtils.contains(techs, "android.nfc.tech.NfcB"))
        return CEPASCard.dumpTag(tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.IsoDep"))
        return DesfireCard.dumpTag(tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.NfcF"))
        return FelicaCard.dumpTag(tagId, tag);
    else//from  w w w.  ja  v a  2 s  . c  om
        throw new UnsupportedTagException(techs, Utils.getHexString(tag.getId()));
}

From source file:com.codebutler.farebot.card.Card.java

public static Card dumpTag(byte[] tagId, Tag tag) throws Exception {
    final String[] techs = tag.getTechList();
    if (ArrayUtils.contains(techs, "android.nfc.tech.NfcB"))
        return CEPASCard.dumpTag(tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.IsoDep"))
        return DesfireCard.dumpTag(tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.NfcF"))
        return FelicaCard.dumpTag(tagId, tag);
    else if (ArrayUtils.contains(techs, "android.nfc.tech.MifareClassic"))
        return ClassicCard.dumpTag(tagId, tag);
    else//from  w  ww .j  a  va  2 s  .co m
        throw new UnsupportedTagException(techs, Utils.getHexString(tag.getId()));
}

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

private void readTag(Tag t) {
    byte[] id = t.getId();

    // get NDEF tag details
    Ndef ndefTag = Ndef.get(t);/*from ww w . ja v  a2  s. c  o  m*/

    // get NDEF message details
    NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
    if (ndefMesg == null) {
        return;
    }
    NdefRecord[] ndefRecords = ndefMesg.getRecords();
    if (ndefRecords == null) {
        return;
    }
    for (NdefRecord record : ndefRecords) {
        short tnf = record.getTnf();
        String type = new String(record.getType());
        if (tnf == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(type.getBytes(), NdefRecord.RTD_URI)) {
            String url = new String(record.getPayload());
            recordBadge(url);
        }
    }
}

From source file:com.codebutler.farebot.activities.AddKeyActivity.java

@Override
protected void onNewIntent(Intent intent) {
    Tag tag = (Tag) intent.getParcelableExtra("android.nfc.extra.TAG");
    mTagId = Utils.getHexString(tag.getId(), "");

    if (ArrayUtils.contains(tag.getTechList(), "android.nfc.tech.MifareClassic")) {
        mCardType = "MifareClassic";
        ((TextView) findViewById(R.id.card_type)).setText("MIFARE Classic");
        ((TextView) findViewById(R.id.card_id)).setText(mTagId);
        ((TextView) findViewById(R.id.key_data)).setText(Utils.getHexString(mKeyData, "").toUpperCase());

        findViewById(R.id.directions).setVisibility(View.GONE);
        findViewById(R.id.info).setVisibility(View.VISIBLE);
        findViewById(R.id.add).setVisibility(View.VISIBLE);

    } else {/*from   w ww .j  a  v a 2s.  c  om*/
        new AlertDialog.Builder(this).setMessage(R.string.card_keys_not_supported)
                .setPositiveButton(android.R.string.ok, null).show();
    }
}

From source file:de.berlin.magun.nfcmime.core.RfidDAO.java

/** 
 * @param tag Tag//from  ww  w. java2s . co  m
 * @return a hexadecimal String representation of a Tag's ID or UID
 */
public String getTagId(Tag tag) {

    // get the ID byte array
    byte[] id = tag.getId();

    if (NfcV.get(tag) != null) {
        ArrayUtils.reverse(id);
    }

    return new String(Hex.encodeHex(id));
}

From source file:com.codebutler.farebot.activity.AddKeyActivity.java

@Override
protected void onNewIntent(Intent intent) {
    Tag tag = intent.getParcelableExtra("android.nfc.extra.TAG");
    mTagId = Utils.getHexString(tag.getId(), "");

    if (ArrayUtils.contains(tag.getTechList(), "android.nfc.tech.MifareClassic")) {
        mCardType = "MifareClassic";
        ((TextView) findViewById(R.id.card_type)).setText("MIFARE Classic");
        ((TextView) findViewById(R.id.card_id)).setText(mTagId);
        ((TextView) findViewById(R.id.key_data)).setText(Utils.getHexString(mKeyData, "").toUpperCase());

        findViewById(R.id.directions).setVisibility(View.GONE);
        findViewById(R.id.info).setVisibility(View.VISIBLE);
        findViewById(R.id.add).setVisibility(View.VISIBLE);

    } else {/*www .  ja v a  2  s  .  c  o m*/
        new AlertDialog.Builder(this).setMessage(R.string.card_keys_not_supported)
                .setPositiveButton(android.R.string.ok, null).show();
    }
}