我写了一个应用来读写NFC标签上的ndef消息。我的应用程序可以在NDEF消息中读取和写入两条NDEF记录。但是当我提交一个在NDEF消息中只有一条NDEF记录的标签时,应用程序崩溃了。我知道背后的原因。而且我也知道如何解决它但要解决它我需要知道如何在NDEF消息中获取no.of记录?
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);
答案 0 :(得分: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("");
}