如何构建将联系人(vcard格式)写入NdefMessage?

时间:2012-11-09 20:59:18

标签: android nfc

我正在尝试将NdefMessage写入NFC标签,以便拥有一张NFC名片,以便在用户手机上点击标签时显示联系信息。

我想通过我的主应用程序传递联系信息。在EditText字段上,但我希望用户打开与People(Android的默认应用程序)的联系。

我无法理解如何编写正确的有效负载格式。

希望你能帮我这个!谢谢。

3 个答案:

答案 0 :(得分:2)

我得到了这个工作。它对于高级用户来说很简单,但我认为像我这样的新手很乐意找到这样的东西。下面的示例代码,请注意,由于Type2标记限制,我硬编码了一些数据。

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;
}

答案 1 :(得分:0)

来自Wikipedia

mime-media:text/vcard
格式:

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

您可以使用NFC Writer撰写联系人,并使用NFC Tag info查看其编写方式。
这两个应用程序都在Google Play上免费提供。

答案 2 :(得分:0)

可能您现在已经有了答案。虽然,我的答案是 在字符串下面使用 开始:VCARD 版本:3.0 FN:你的名字在这里 ORG:工作地点 电子邮件:sample@gmail.com ADR:;;美国加利福尼亚州山景城 网址:www.google.com 电话:123456 END:VCARD

或者,您可以尝试this app进行书写。