当前位置: 首页 > 知识库问答 >
问题:

如何获取NDEF消息中的记录总数?

谯志诚
2023-03-14

我写了一个应用程序,可以在NFC标签上读写ndef消息。我的应用程序可以在NDEF消息中读取和写入两条NDEF记录。但当我展示一个在NDEF消息中只有一条NDEF记录的标签时,应用程序崩溃了。我知道背后的原因。我也知道如何解决它,但要解决它,我需要知道如何获得NDEF消息中的记录数?

        NdefMessage[] msgs = getNdefMessagesFromIntent(intent);
        NdefRecord ndefRecord1 = msgs[0].getRecords()[0];
        NdefRecord ndefRecord2 = msgs[0].getRecords()[1]; //problem is here
        byte[] payload1 = ndefRecord1.getPayload();
        byte[] payload2 = ndefRecord2.getPayload();
        //Get the text encoding
        String textEncoding1 = ((payload1[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
        String textEncoding2 = ((payload2[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
        //Get the Language Code
        int languageCodeLength1 = payload1[0] & 0077;
        int languageCodeLength2 = payload2[0] & 0077;
        String text1 = null;
        String text2 = null;
        //Get the Text
        try 
        {
            text1 = new String(payload1, languageCodeLength1 + 1, payload1.length - languageCodeLength1 - 1, textEncoding1);
        } 
        catch (UnsupportedEncodingException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //Get the Text
        try 
        {
            text2 = new String(payload2, languageCodeLength2 + 1, payload2.length - languageCodeLength2 - 1, textEncoding2);
        } 
        catch (UnsupportedEncodingException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String payloadString1 = new String(text1);
        String payloadString2 = new String(text2);

        record1.setText(payloadString1);
        record2.setText(payloadString2);

共有1个答案

苏涵润
2023-03-14

经过实验,我自己找到了解决这个问题的方法。希望它也能帮助其他人。。。

        NdefMessage[] msgs = getNdefMessagesFromIntent(intent);
        NdefRecord[] ndefRecords = msgs[0].getRecords();
        int totalRecords = ndefRecords.length;
        if(totalRecords == 2)
        {
            NdefRecord ndefRecord1 = msgs[0].getRecords()[0];
            NdefRecord ndefRecord2 = msgs[0].getRecords()[1];
            byte[] payload1 = ndefRecord1.getPayload();
            byte[] payload2 = ndefRecord2.getPayload();
            //Get the text encoding
            String textEncoding1 = ((payload1[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
            String textEncoding2 = ((payload2[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
            //Get the Language Code
            int languageCodeLength1 = payload1[0] & 0077;
            int languageCodeLength2 = payload2[0] & 0077;
            String text1 = null;
            String text2 = null;
            //Get the Text
            try 
            {
                text1 = new String(payload1, languageCodeLength1 + 1, payload1.length - languageCodeLength1 - 1, textEncoding1);
            }    
            catch (UnsupportedEncodingException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //Get the Text
            try 
            {
                text2 = new String(payload2, languageCodeLength2 + 1, payload2.length - languageCodeLength2 - 1, textEncoding2);
            } 
            catch (UnsupportedEncodingException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String payloadString1 = new String(text1);
            String payloadString2 = new String(text2);

            record1.setText(payloadString1);
            record2.setText(payloadString2);
        }
        else
        {
            NdefRecord ndefRecord1 = msgs[0].getRecords()[0];
            byte[] payload1 = ndefRecord1.getPayload();
            String textEncoding1 = ((payload1[0] & 0200) == 0) ? "UTF-8" : "UTF-16";
            int languageCodeLength1 = payload1[0] & 0077;
            String text1 = null;
            try 
            {
                text1 = new String(payload1, languageCodeLength1 + 1, payload1.length - languageCodeLength1 - 1, textEncoding1);
            }    
            catch (UnsupportedEncodingException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String payloadString1 = new String(text1);
            record1.setText(payloadString1);
            record2.setText("");
        }
 类似资料:
  • 我已经开发了2个Android应用程序。第一个,写入NFC标签,第二个读取我写的内容。 因此,对于*第一个应用程序(WriteNFC):我正在向标记中写入一条NDEF消息,该消息涉及两条NDEF记录:第一条记录是“文本”类型,第二条是“URL”类型。 第二个应用程序(ReadNFC):我扫描标签,以便读取NDEF消息,并显示它,但不是完全显示。我只在屏幕上显示第二条记录(URL)。我想做的是,当用

  • 有人知道为什么会这样吗? //TimerTask类

  • 我使用的是Camel 2.9.2,完成路线定义如下: 中提供https://stackoverflow.com/questions/18877562/how-can-i-log-a-header-value-in-camel-using-spring-dsl解决方案由于某种原因对我不起作用。 我可以看到一些与路由定义相关的驼峰跟踪

  • 类似的问题 - 如何在Android中读取检测到的NFC标签(NDEF内容)详细信息? 我希望我的android应用程序能够读取和解析检测到的NDEF消息。 我已经编辑了AndroidManifest.xml来检测nfc标签,并添加了意图过滤器,如下所示 我相信这很好,因为当我使用SDK附带的NFCDemo示例应用程序创建MockNDEF标签时,当我可以选择处理这些生成的标签的应用程序列表出现时,

  • 我的应用程序中有一个名为posts的实体。帖子可以是其他帖子的子帖子,因此父帖子有hasMany('Posts'),子帖子有hasOne('post')。包含是无限的。 以下是模式: 如何递归地获取“Post_id”设置为null的第一篇帖子的所有子帖子和子帖子的子帖子等? 请不要在这里评论性能,我知道这样的模式很糟糕,我只想知道如何正确编写递归函数来检索无限嵌套的帖子。

  • 问题内容: 我正在开发一个简单的桌面应用程序(不是webapp)。 这是我的: 如你所见,为了消除控制台中的Spring日志消息,我尝试了以下解决方案: 当我从代码中调用log4j记录器时,记录消息是根据上面指定的模式进行的(这很好)。 但是,最糟糕的是-我仍然从Spring进入控制台的DEBUG级别消息…它们看起来像这里: 无法禁用日志记录消息 他们有不同的模式。好像他们忽略了我的设置。 我还尝