简体   繁体   English

如何在 Android 中读取 NFC Mifare Classic 1K 卡中的文本记录

[英]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.在此处输入图像描述我是从事 NFC 阅读器项目的新手 Java 程序员。 I've been trying to read the content of a NFC card in Android but I can't get it to work.我一直在尝试在 Android 中读取 NFC 卡的内容,但无法正常工作。 I can only retrieve the UID of the NFC card.我只能检索 NFC 卡的 UID。 I went through documentation for NFC in Android and also some tutorials but I don't really understand it.我浏览了 Android 中的 NFC 文档以及一些教程,但我不太了解。 I've searched a lot but I didn't a clear solution or article about reading Mifare Classic 1K text records.我搜索了很多,但我没有一个明确的解决方案或关于阅读 Mifare Classic 1K 文本记录的文章。 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.我正在使用 NFC 工具桌面应用程序编写文本记录(下面的屏幕截图),我将不胜感激。 Thanks in advance提前致谢

Here the code snippet I am using to fetch records after getting the intent on android这是我在获得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.您对记录的处理不是在寻找 Ndef 文本记录。

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请注意, toMimeType实际上确实将RTD_TEXT转换为 mime 类型,但为此

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有关处理文本记录,请参阅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.错误,因为当标签中没有 Ndef 数据时,您试图解析 Ndef 数据。

should be应该

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

update2 I checked out the github link to the code you gave update2我查看了您提供的代码的 github 链接

It won't trigger on Ndef records because you are testing for the wrong type of Intent它不会在 Ndef 记录上触发,因为您正在测试错误类型的 Intent

The Intent dispatch system only sends the highest type of NFC Intent possible with the order as:- Intent 调度系统仅发送最高类型的 NFC Intent,其顺序如下:-

ACTION_NDEF_DISCOVERED -> ACTION_TECH_DISCOVERED -> ACTION_TAG_DISCOVERED 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.因此,如果标签包含 NDEF 数据,则仅设置 ACTION_NDEF_DISCOVERED 的操作类型。

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永远不会触发 Intent 的处理

that code should be该代码应该是

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

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

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