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

在Android上向NFC标签写入和读取应用程序记录,android,text,nfc,launch,ndef,Android,Text,Nfc,Launch,Ndef,我正在尝试以下方法: 向NFC标记写入一条消息,该标记包含对我的应用程序的引用以及我可以识别标记的短字符串 读那条消息 为了在开始时加快测试速度,我使用了app Tagwriter()编写了一个带有我需要的标记:在下一个窗口中“创建纯文本”和“添加启动应用程序” 一旦接触到标签,我的手机将启动我的应用程序,它甚至可以正确读取识别字符串。但是,我也希望它从我自己的应用程序中编写标记,而不是引用另一个 我已经测试了几种方法,但没有一种有效。要么我的应用程序根本没有启动,要么它无法读取字符串。有人

我正在尝试以下方法:

  • 向NFC标记写入一条消息,该标记包含对我的应用程序的引用以及我可以识别标记的短字符串
  • 读那条消息
为了在开始时加快测试速度,我使用了app Tagwriter()编写了一个带有我需要的标记:在下一个窗口中“创建纯文本”和“添加启动应用程序”

一旦接触到标签,我的手机将启动我的应用程序,它甚至可以正确读取识别字符串。但是,我也希望它从我自己的应用程序中编写标记,而不是引用另一个

我已经测试了几种方法,但没有一种有效。要么我的应用程序根本没有启动,要么它无法读取字符串。有人能帮我吗

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

对不起,本可以更详细一点。看来我自己解决了我的问题。我已经从这个网站和那个网站上取了一些示例代码,然后。。。 这就是为什么我在回答这个问题之前必须先做些清理。在这个过程中,我发现了一些错误。写入函数现在如下所示:

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});
[...]
}
“apprecord”似乎还可以,但文本记录却不行。

“我已经测试了几种方法,但都不起作用。”:什么不起作用?标签是否未写入?你的应用程序没有启动吗?您的应用程序是否启动但未收到文本?你会犯什么错误?