简体   繁体   中英

How to read text record in a NFC Mifare Classic 1K card in android

enter image description here I am newbie java programmer working on a NFC reader project. I've been trying to read the content of a NFC card in Android but I can't get it to work. I can only retrieve the UID of the NFC card. I went through documentation for NFC in Android and also some tutorials but I don't really understand it. I've searched a lot but I didn't a clear solution or article about reading Mifare Classic 1K text records. How can I achieve that? Really I don't know anything so please excuse me if the question is a little bit unclear. I'm using the NFC tools desktop app to write text records(screenshot below) I will appreciate any help. Thanks in advance

Here the code snippet I am using to fetch records after getting the intent on android


    private void readFromIntent(Intent intent) {
        System.out.println("Came huered ");
        String action = intent.getAction();
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
                || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
                || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
           
            Parcelable[] rawMessages = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
            NdefMessage[] messages ;
            
            if (rawMessages != null) {
                messages = new NdefMessage[rawMessages.length];
                for (int i = 0; i < rawMessages.length; i++) {
                    messages[i] = (NdefMessage) rawMessages[i];
                    NdefRecord [] records = messages[i].getRecords();
                    System.out.println("RECORDS "+records);
                    //if you are sure you have text then you don't need to test TNF
                    for(NdefRecord record: records){
                        processRecord(record);
                    }
                }
            }
        }
    }
public void processRecord(NdefRecord record) {

        short tnf = record.getTnf();
        switch (tnf) {


            case NdefRecord.TNF_MIME_MEDIA: {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    if (record.toMimeType().equals("MIME/Type")) {
                        // handle this as you want
                        System.out.println("HEREEEE");
                    } else {
                        //Record is not our MIME
                    }
                }
            }
            // you can write more cases
            default: {
                //unsupported NDEF Record
            }
        }
    }

Your Processing of the record is not looking for a Ndef Text Record.

case NdefRecord.TNF_MIME_MEDIA: {

is not right for Text records

use

short tnf = record.getTnf();
byte[] type = record.getType();

if (tnf == NdefRecord.TNF_WELL_KNOWN &&
        Arrays.equals(type, NdefRecord.RTD_TEXT) {
  // Correct TNF and Type for Text record
  // Now process the Text Record encoding
}

Note as toMimeType does actually convert RTD_TEXT to a mime Type but for that

if (record.toMimeType().equals("MIME/Type")) {

would need to be

if (record.toMimeType().equals("text/plain")) {

For processing the Text record see https://stackoverflow.com/a/59515909/2373819

update

Should have also said that

if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
                || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
                || NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

is wrong as your are trying to parse for Ndef data when the Tag does not have Ndef data in it.

should be

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {

update2 I checked out the github link to the code you gave

It won't trigger on Ndef records because you are testing for the wrong type of Intent

The Intent dispatch system only sends the highest type of NFC Intent possible with the order as:-

ACTION_NDEF_DISCOVERED -> ACTION_TECH_DISCOVERED -> ACTION_TAG_DISCOVERED

Therefore if the Tag contains NDEF data only the action type of ACTION_NDEF_DISCOVERED is set.

Thus

public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
...

will never trigger the processing of the Intent

that code should be

public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM