简体   繁体   English

在Android上将应用程序记录写入和读取到NFC标签

[英]Write and read application record to NFC tag on Android

I'm trying the following: 我正在尝试以下方法:

  • Write a message to an NFC tag that contains a reference to my application as well as a short string with which I can identify the tag. 将消息写到NFC标签,其中包含对我的应用程序的引用以及可以用来识别标签的短字符串。
  • Read that message. 阅读该消息。

To speed up testing a bit at the beginning I've used the app Tagwriter ( https://play.google.com/store/apps/details?id=com.nxp.nfc.tagwriter&hl=de ) to write a tag with my needs: "Create plain text" and "Add launch application" in the next window. 为了在一开始就加快测试速度,我使用了应用Tagwriter( https://play.google.com/store/apps/details?id=com.nxp.nfc.tagwriter&hl=de )使用我的需求:在下一个窗口中“创建纯文本”和“添加启动应用程序”。

Upon contact with the tag my phone will start up my application, it'll even read the identifying string correctly. 接触标签后,我的手机将启动我的应用程序,它甚至会正确读取识别字符串。 However I also want it to write the tag from my own application instead of referring to another one. 但是我也希望它从我自己的应用程序中编写标记,而不是引用另一个。

I've tested several approaches, none of them worked. 我已经测试了几种方法,但没有一种有效。 Either my application isn't started at all or it can't read the string. 我的应用程序根本没有启动,或者无法读取字符串。 Can anyone help me? 谁能帮我?

public static boolean writeTag(String textToWrite, Tag tag)
{
     Miscellaneous.logEvent("i", "NFC", "Attempting to write tag...", 2);

     String packageName = Miscellaneous.getAnyContext().getPackageName();       
     NdefRecord appRecord = NdefRecord.createApplicationRecord(packageName);
     // Record with actual data we care about
     NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                                            new String("application/" + packageName)
                                            .getBytes(Charset.forName("US-ASCII")),
                                            null, textToWrite.getBytes());

     // Complete NDEF message with both records
     NdefMessage completeMessageToWrite = new NdefMessage(new NdefRecord[] {textRecord, appRecord});

     int size = completeMessageToWrite.toByteArray().length;
     try
     {
           Ndef ndef = Ndef.get(tag);
           if (ndef != null)
           {
                ndef.connect();
                if (ndef.isWritable() && ndef.getMaxSize() > size)
            {
                ndef.writeNdefMessage(completeMessageToWrite);
                Miscellaneous.logEvent("i", "NFC", "Done writing tag.", 2);
                return true;
            }
        }
        else
        {
            NdefFormatable format = NdefFormatable.get(tag);
            if (format != null)
            {
                try
                {
                    format.connect();
                    format.format(completeMessageToWrite);
                    Miscellaneous.logEvent("i", "NFC", "Done writing tag.", 2);
                    return true;
                }
                catch(IOException e)
                {
                    Miscellaneous.logEvent("e", "NFC", "Error writing tag: " + Log.getStackTraceString(e), 2);
                }
            }
        }
    }
    catch(Exception e)
    {
        Miscellaneous.logEvent("e", "NFC", "Error writing tag: " + Log.getStackTraceString(e), 2);
    }

    return false;
}

I'm sorry, could have been a bit more detailed. 抱歉,本来可以更详细一些。 It appears I kind of solved my problem myself. 看来我自己解决了我的问题。 I've been taking a bit of example code from this website and a bit from that website and... That's why I had to first do some cleaning up before answering here. 我一直在从该网站上获取一些示例代码,并从该网站上获取一些代码...这就是为什么我必须首先清理一下然后再回答这里的原因。 In that process I kind of found the mistake. 在此过程中,我发现了错误。 The write-function now looks like this: 现在,写入功能如下所示:

public static boolean writeTag(String textToWrite, Tag tag)
{
    Miscellaneous.logEvent("i", "NFC", "Attempting to write tag...", 2);

    String packageName = Miscellaneous.getAnyContext().getPackageName();        
    NdefRecord appRecord = NdefRecord.createApplicationRecord(packageName);
    // Record with actual data we care about
    byte[] textBytes = textToWrite.getBytes();
    byte[] textPayload = new byte[textBytes.length + 3];
    textPayload[0] = 0x02; // 0x02 = UTF8
    textPayload[1] = 'e'; // Language = en
    textPayload[2] = 'n';
    System.arraycopy(textBytes, 0, textPayload, 3, textBytes.length);
    NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], textPayload);

    // Complete NDEF message with both records
    NdefMessage completeMessageToWrite = new NdefMessage(new NdefRecord[] {textRecord, appRecord});
[...]
}

It appears the "apprecord" was fine, but the text record was not. 看来“ apprecord”很好,但文本记录却不好。

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

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