Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

android - How to Read NFC Tag?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...