Question

I wrote an app to read and write ndef message on NFC tag. My app can read and write two NDEF records in a NDEF message fine. But when I presented a tag which is having only one NDEF record inside the NDEF message then app got crashed. I know the reason behind it. And I also know how to solve it but to resolve it I need to know how to get the no.of records in an NDEF message?

        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);
Was it helpful?

Solution

After experimenting I self found the solution to this problem. Hope it will help others also...

        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("");
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top