我正在嘗試將NdefMessage寫入NFC標籤,以便在用戶手機上輕敲標籤時使NFC名片顯示聯繫信息。如何構建將聯繫人(電子名片格式)寫入NdefMessage?
我想通過我的主應用程序傳遞聯繫人信息。在EditText字段上,但我希望用戶打開與人聯繫(Android的默認應用程序)。
我很難理解如何編寫正確的有效負載格式。
希望你能幫助我這個!,謝謝。
我正在嘗試將NdefMessage寫入NFC標籤,以便在用戶手機上輕敲標籤時使NFC名片顯示聯繫信息。如何構建將聯繫人(電子名片格式)寫入NdefMessage?
我想通過我的主應用程序傳遞聯繫人信息。在EditText字段上,但我希望用戶打開與人聯繫(Android的默認應用程序)。
我很難理解如何編寫正確的有效負載格式。
希望你能幫助我這個!,謝謝。
MIME媒體: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:[email protected]
REV:20080424T195243Z
END:VCARD
您可以使用NFC Writer寫一個接觸和NFC Tag info,看看它是怎麼寫的。
這兩個應用程序在Google Play上都是免費的。
我得到了這個工作。它對於高級用戶來說很簡單,但我認爲像我這樣的新手很樂意找到這樣的東西。下面的代碼示例請注意,由於Type2 Tag約束,我硬編碼了一些數據。
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;
}
感謝您的參考。 –