hi all,
I am trying to write vCard data into Mifare STD 1K NFC card as NDEF formated data.
As my created NDEF message is greater than ndef.getMaxSize() i am not able to create tag. If i am reducing the vCard data length it is working fine . let me know how to create an NDEF formated vcard which is having data length greater than 285.
Here is my code..
@Override
protected void onNewIntent(Intent intent) {
// Tag writing mode
if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (writeTag(getPlaceidAsNdef(), detectedTag)) {
Toast.makeText(this, "Success: Wrote placeid to nfc tag", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this, "Write failed", Toast.LENGTH_LONG).show();
}
}
}
/*
* Converts a Long into a NdefMessage in application/vnd.facebook.places MIMEtype.
*
* for writing Places
*/
public static NdefMessage getPlaceidAsNdef() {
String msg = "BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"N:Gump;Forrest\n" +
"FN:Forrest Gump\n" +
"ORG:Bubba Gump Shrimp Co.\n" +
"TITLE:Shrimp Man\n" +
"TEL;WORK;VOICE
111) 555-1212\n" +
"TEL;HOME;VOICE
404) 555-1212\n" +
"ADR;WORK:;;100 Edge;Baytown;United\n" +
"EMAIL;PREF;INTERNET:forrestgump@example.com\n" +
"END:VCARD";
byte[] textBytes = msg.getBytes();
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
"text/x-vCard".getBytes(), new byte[] {}, textBytes);
return new NdefMessage(new NdefRecord[] { textRecord });
}
/*
* Writes an NdefMessage to a NFC tag
*/
public static boolean writeTag(NdefMessage message, Tag tag) {
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
return false;
}
if (ndef.getMaxSize() < size) {
return false;
}
ndef.writeNdefMessage(message);
return true;
} else {
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
return true;
} catch (IOException e) {
return false;
}
} else {
return false;
}
}
} catch (Exception e) {
return false;
}
}
Thanks
Biju.
I am trying to write vCard data into Mifare STD 1K NFC card as NDEF formated data.
As my created NDEF message is greater than ndef.getMaxSize() i am not able to create tag. If i am reducing the vCard data length it is working fine . let me know how to create an NDEF formated vcard which is having data length greater than 285.
Here is my code..
@Override
protected void onNewIntent(Intent intent) {
// Tag writing mode
if (mWriteMode && NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (writeTag(getPlaceidAsNdef(), detectedTag)) {
Toast.makeText(this, "Success: Wrote placeid to nfc tag", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(this, "Write failed", Toast.LENGTH_LONG).show();
}
}
}
/*
* Converts a Long into a NdefMessage in application/vnd.facebook.places MIMEtype.
*
* for writing Places
*/
public static NdefMessage getPlaceidAsNdef() {
String msg = "BEGIN:VCARD\n" +
"VERSION:2.1\n" +
"N:Gump;Forrest\n" +
"FN:Forrest Gump\n" +
"ORG:Bubba Gump Shrimp Co.\n" +
"TITLE:Shrimp Man\n" +
"TEL;WORK;VOICE
111) 555-1212\n" +"TEL;HOME;VOICE
404) 555-1212\n" +"ADR;WORK:;;100 Edge;Baytown;United\n" +
"EMAIL;PREF;INTERNET:forrestgump@example.com\n" +
"END:VCARD";
byte[] textBytes = msg.getBytes();
NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
"text/x-vCard".getBytes(), new byte[] {}, textBytes);
return new NdefMessage(new NdefRecord[] { textRecord });
}
/*
* Writes an NdefMessage to a NFC tag
*/
public static boolean writeTag(NdefMessage message, Tag tag) {
int size = message.toByteArray().length;
try {
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
ndef.connect();
if (!ndef.isWritable()) {
return false;
}
if (ndef.getMaxSize() < size) {
return false;
}
ndef.writeNdefMessage(message);
return true;
} else {
NdefFormatable format = NdefFormatable.get(tag);
if (format != null) {
try {
format.connect();
format.format(message);
return true;
} catch (IOException e) {
return false;
}
} else {
return false;
}
}
} catch (Exception e) {
return false;
}
}
Thanks
Biju.