0

我写了一个应用程序来读取和写入 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);
4

1 回答 1

0

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

        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("");
        }
于 2013-02-27T11:15:16.677 回答