3

I'm trying to write a NdefMessage into a NFC tag in order to have a NFC business card that show the contact information as the tag is tapped with the users phone.

I want to pass the Contact information via my main Application. on EditText fields, but I want the user to open the contact with People (Android's default app).

I'm having trouble to understand how to write the correct payload format.

hope you can help me on this one!, thanks.

Lucky
  • 16,787
  • 19
  • 117
  • 151
maze_mx
  • 103
  • 1
  • 2
  • 7

3 Answers3

2

I got the this working. Its something simple for the advanced users but I think that newcomers like me will be pleased to find something like this. Example code below, please note thaat I hardcoded some data due to Type2 Tag constraints.

private NdefRecord createRecord(String text)
        throws UnsupportedEncodingException {

    //Intent intent = getIntent();
    //EditText editTextWeb = (EditText)
    EditText editText = (EditText) findViewById(R.id.editTextWeblinks);
    String nameVcard = "BEGIN:VCARD" +"\n"+ "VERSION:2.1" +"\n" + "N:;" + editText.getText().toString() + "\n" +"ORG: PlanAyala"+"\n"+ "TEL;HOME:6302421" +"\n"+ "END:VCARD";
    byte[] uriField = nameVcard.getBytes(Charset.forName("US-ASCII"));
    byte[] payload = new byte[uriField.length + 1];              //add 1 for the URI Prefix
    //payload[0] = 0x01;                                      //prefixes http://www. to the URI
    System.arraycopy(uriField, 0, payload, 1, uriField.length);  //appends URI to payload

    NdefRecord nfcRecord = new NdefRecord(
        NdefRecord.TNF_MIME_MEDIA, "text/vcard".getBytes(), new byte[0], payload);


    return nfcRecord;
}
maze_mx
  • 103
  • 1
  • 2
  • 7
  • Thanks for posting this example. Does it work like how you wanted to? That is, you send the contact info from your own app and it opens up on the receiving device with Android's default contacts app? – Dennis Dec 04 '12 at 02:39
  • Yes, I provide the Vcard data trough EditText. As you declare this MIME type as text/vcard, the Android system will open an application which support that kind of information, in this case the People application will add and thus you the contact information contained on the NFC tag. I got this code running on a ndef writer application. Let me know if this was helpful to you :] – maze_mx Dec 05 '12 at 17:10
  • @maze_mx can you please update answer with email format? That will helpful for me – Aristo Michael Oct 08 '14 at 16:06
0

From Wikipedia :

mime-media : text/vcard
format :

BEGIN:VCARD
VERSION:4.0
N:Gump;Forrest;;;
FN: Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=work,voice;VALUE=uri:tel:+1-111-555-1212
TEL;TYPE=home,voice;VALUE=uri:tel:+1-404-555-1212
ADR;TYPE=work;LABEL="42 Plantation St.\nBaytown, LA 30314\nUnited States of America"
 :;;42 Plantation St.;Baytown;LA;30314;United States of America
EMAIL:forrestgump@example.com
REV:20080424T195243Z
END:VCARD

You can use NFC Writer to write a contact and NFC Tag info to see how it's written.
Both application are free on Google Play.

yDelouis
  • 2,094
  • 17
  • 18
0

Probably you might have the answer for now. Though, my answer will be use below string BEGIN:VCARD VERSION:3.0 FN:you name goes here ORG: work place EMAIL: sample@gmail.com ADR:;; Mountain view, CA, USA URL: www.google.com TEL: 123456 END:VCARD

Alternatively, you can try this app to write.

Sam Reyes
  • 329
  • 4
  • 9