如何将联系人(vcard格式)写入NdefMessage中?[英] How to build write a Contact (vcard format) into a NdefMessage?

本文是小编为大家收集整理的关于如何将联系人(vcard格式)写入NdefMessage中?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

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

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

希望你能帮我解决这个问题!,谢谢.

推荐答案

我得到了这个工作.对于高级用户来说,这很简单,但我认为像我这样的新手会很高兴找到这样的东西.下面的示例代码,请注意,由于 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;
}

其他推荐答案

来自维基百科:

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标签信息以查看怎么写的.
这两个应用程序在 Google Play 上都是免费的.

其他推荐答案

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

或者,您可以试试这个应用 写.

本文地址:https://www.itbaoku.cn/post/102612.html

问题描述

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.

推荐答案

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

其他推荐答案

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.

其他推荐答案

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.