How to Read NFC Tag?
Asked Answered
C

4

1

Hi I am trying to read from NFC Tag. But I am getting an exception.

I have put this condition to detect the tag?

if(NfcAdapter.ACTION_TAG_DISCOVERED != null)

Whether this condition is correct?

Cambodia answered 7/9, 2012 at 7:24 Comment(2)
difficult to understand here please elaborateOccultation
When i will take the phone to proximity of tag it must detect the tag. So what condition i should use to trigger the tag detection eventCambodia
S
2

To answer you question about the code -

That will always be true - NfcAdapter.ACTION_TAG_DISCOVERED is a constant value - you need to use:

getIntent().getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED) 

to compare it.

But, that probably has nothing to do with your exception -

  1. did you include the NFC permission in your android manifest?
  2. are you sure your phone supports NFC, only two or three support it at this time.
  3. we'd need the stack trace from your logs to know what caused the exception
Supernaturalism answered 7/9, 2012 at 7:30 Comment(2)
I am checking the condition in onStart() method. Whether it has to do with exception or notCambodia
Try this boilerplateFloriated
D
3

First of all you have to initialize the NFC adapter and define Pending Intent in onCreate callback:

NfcAdapter mAdapter;
PendingIntent mPendingIntent;
mAdapter = NfcAdapter.getDefaultAdapter(this);

if (mAdapter == null) {
    //nfc not support your device.
    return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

In onResume() Call back enable the Foreground Dispatch to detect NFC intent.

mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);

In onPause() callback you have to disable the forground dispatch:

if (mAdapter != null) {
    mAdapter.disableForegroundDispatch(this);
}

In onNewIntent() call back method you will get the new Nfc Intent. After getting The Intent , you have to parse the intent to detect the card:

@Override
protected void onNewIntent(Intent intent) {
    getTagInfo(intent)
}

private void getTagInfo(Intent intent) {
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

    String[] techList = tag.getTechList();
    for (int i = 0; i<techList.length; i++) {
        if (techList[i].equals(MifareClassic.class.getName())) {

            MifareClassic mifareClassicTag = MifareClassic.get(tag);
            switch (mifareClassicTag.getType()) {
                case MifareClassic.TYPE_CLASSIC:
                    //Type Clssic
                    break;
                case MifareClassic.TYPE_PLUS:
                    //Type Plus
                    break;
                case MifareClassic.TYPE_PRO:
                    //Type Pro
                    break;
            }
        } else if (techList[i].equals(MifareUltralight.class.getName())) {
            //For Mifare Ultralight
            MifareUltralight mifareUlTag = MifareUltralight.get(tag);
            switch (mifareUlTag.getType()) {
                case MifareUltralight.TYPE_ULTRALIGHT:
                    break;
                case MifareUltralight.TYPE_ULTRALIGHT_C:

                    break;
            }
        } else if (techList[i].equals(IsoDep.class.getName())) {
            // info[1] = "IsoDep";
            IsoDep isoDepTag = IsoDep.get(tag);

        } else if (techList[i].equals(Ndef.class.getName())) {
            Ndef.get(tag);

        } else if (techList[i].equals(NdefFormatable.class.getName())) {

            NdefFormatable ndefFormatableTag = NdefFormatable.get(tag);

        }
    }
}

Full Complete code is here.

Dusk answered 17/2, 2015 at 15:48 Comment(3)
Your repository is not found. please write full example here.Capacitance
@HiteshSarsava Please check the below link. github.com/mesuk/NFC-UTILSDusk
Thanks for the link. I have checked that code but there is no snippet code that tells you how to send data using NFC. If you could help me.Capacitance
S
2

To answer you question about the code -

That will always be true - NfcAdapter.ACTION_TAG_DISCOVERED is a constant value - you need to use:

getIntent().getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED) 

to compare it.

But, that probably has nothing to do with your exception -

  1. did you include the NFC permission in your android manifest?
  2. are you sure your phone supports NFC, only two or three support it at this time.
  3. we'd need the stack trace from your logs to know what caused the exception
Supernaturalism answered 7/9, 2012 at 7:30 Comment(2)
I am checking the condition in onStart() method. Whether it has to do with exception or notCambodia
Try this boilerplateFloriated
F
1

That statement will always be true.

I have created a project which has a boilerplate project for getting on the right track.

Floriated answered 17/9, 2012 at 22:54 Comment(0)
D
0

try out below working code.

 /**
 * this method is used for read nfc data from tag.
 *
 * @param ndef Ndef
 */
private void readFromNFC(Ndef ndef) {

    try {
        ndef.connect();
        NdefMessage ndefMessage = ndef.getNdefMessage();

        NdefRecord[] e = ndefMessage.getRecords();

        for (NdefRecord s : e) {
            String message = new String(s.getPayload());
            if (!message.equals("")) {
                CustomLog.info(TAG, "readFromNFC: " + message);
                mTvMessage.setText(message);
            } else {
                mTvMessage.setText("Tag is empty!");
            }
        }
        ndef.close();
    } catch (IOException | FormatException e) {
        e.printStackTrace();
    }
}
Doth answered 24/2, 2020 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.