【问题标题】:How to get total no.of records in a NDEF message?如何获取 NDEF 消息中的记录总数?
【发布时间】:2013-02-27 19:15:16
【问题描述】:

我编写了一个应用程序来在 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);

【问题讨论】:

    标签: android nfc ndef


    【解决方案1】:

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

            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("");
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多