简体   繁体   English

Android NFC 读取记录得到 null Ndef 记录

[英]Android NFC read Records getting a null Ndef Record

I am a junior android developper working on a Nfc Reader project.我是从事 Nfc Reader 项目的初级 android 开发人员。 And I use Mifare Cards.我使用 Mifare 卡。 I can only read the UID for the card but unable to read records that I wrote using Nfc Tools with my app but it gives me a null when logging the Ndef value in my console.我只能读取卡的 UID,但无法读取我使用 Nfc 工具和我的应用程序编写的记录,但在我的控制台中记录 Ndef 值时,它给了我一个 null。 Any help?有什么帮助吗? Thanks in advance Here's my code提前致谢 这是我的代码

public class MainActivity extends AppCompatActivity {
private TextView mNfcText;
private String mTagText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNfcText = (TextView) findViewById(R.id.textME);
}

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
   
    Ndef ndef = Ndef.get(detectedTag);
    System.out.println("NDEF >>>"+ndef); //gives null
    readNfcTag(intent);
    mNfcText.setText(mTagText);
}

/**
 * NFC READ
 */
private void readNfcTag(Intent intent) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        NdefMessage msgs[] = null;
        int contentSize = 0;
        if (rawMsgs != null) {
            msgs = new NdefMessage[rawMsgs.length];
            for (int i = 0; i < rawMsgs.length; i++) {
                msgs[i] = (NdefMessage) rawMsgs[i];
                contentSize += msgs[i].toByteArray().length;
            }
        }
        try {
            if (msgs != null) {
                NdefRecord record = msgs[0].getRecords()[0];
                String textRecord = parseTextRecord(record);
                mTagText += textRecord + "\n\ntext\n" + contentSize + " bytes";
            }
        } catch (Exception e) {
        }
    }
}

/**
 * PArser
 */
public static String parseTextRecord(NdefRecord ndefRecord) {
    
    if (ndefRecord.getTnf() != NdefRecord.TNF_WELL_KNOWN) {
        return null;
    }
    if (!Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
        return null;
    }
    try {
       
        byte[] payload = ndefRecord.getPayload();
        String textEncoding = ((payload[0] & 0x80) == 0) ? "UTF-8" : "UTF-16";
        int languageCodeLength = payload[0] & 0x3f;
        String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII");
        String textRecord = new String(payload, languageCodeLength + 1,
                payload.length - languageCodeLength - 1, textEncoding);
        System.out.println("Text "+textRecord);
        return textRecord;
    } catch (Exception e) {
        throw new IllegalArgumentException();
    }
}

You don't specify exact which type of card you are using (Model type of the card) but Most cards described as "Mifare" are not actual fully NFC standards compliant.您没有具体说明您使用的是哪种类型的卡(卡的型号类型),但大多数描述为“Mifare”的卡实际上并不完全符合 NFC 标准。

This especially is the case if it is a Mifare Classic Type card.如果它是 Mifare Classic Type 卡,情况尤其如此。

So there are 2 possible reason for your problem.所以你的问题有两个可能的原因。

1)Mifare classic cards have a proprietary method of storing Ndef Data on them and not every phone hardware supports either reading Ndef data from them or supports reading them at anything other than the NfcA low level protocol. 1)Mifare 经典卡具有在其上存储 Ndef 数据的专有方法,并非每个手机硬件都支持从中读取 Ndef 数据或支持以 NfcA 低级协议以外的任何方式读取它们。 And you would have to implement reading/writing Ndef data yourself using the proprietary method.而且您必须使用专有方法自己实现读取/写入 Ndef 数据。

It would be better to use a NFC standards compliant Card like one of the NTAG21x series instead.最好使用符合 NFC 标准的卡,例如 NTAG21x 系列之一。

2)You don't seemed to have configured your code to actually ask the system to send you data when it sees a card via Manifest entries when your app is not running or via one of the 2 foreground detection methods when your app is already running (See https://stackoverflow.com/a/72152878/2373819 for more details) 2)您似乎没有将代码配置为在您的应用程序未运行时通过清单条目或在您的应用程序已运行时通过 2 种前台检测方法之一看到卡片时实际要求系统向您发送数据(有关详细信息,请参阅https://stackoverflow.com/a/72152878/2373819

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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