在Android上将应用程序记录写入NFC标签

时间:2014-05-16 12:56:43

标签: android text nfc launch ndef

我正在尝试以下方法:

  • 将消息写入NFC标签,该标签包含对我的应用程序的引用以及用于识别标签的短字符串。
  • 阅读该消息。

为了加快测试开始我已经使用应用程序Tagwriter(https://play.google.com/store/apps/details?id=com.nxp.nfc.tagwriter&hl=de)来编写满足我需求的标签:"创建纯文本"和"添加启动应用程序"在下一个窗口中。

在与标签联系后,我的手机将启动我的应用程序,它甚至会正确读取识别字符串。但是我也想让它从我自己的应用程序中编写标记,而不是引用另一个。

我测试了几种方法,但没有一种方法有效。要么我的应用程序根本没有启动,要么它无法读取字符串。任何人都可以帮助我吗?

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;
}

1 个答案:

答案 0 :(得分:1)

对不起,本来可以更详细了。看来我有点自己解决了我的问题。我一直在从这个网站上获取一些示例代码,并从该网站获得一些... 这就是为什么在回答这里之前我必须先做一些清理工作。在那个过程中,我发现了一个错误。写函数现在看起来像这样:

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});
[...]
}

似乎“赞赏”很好,但文本记录不是。