Skip to content

Commit fba8b7f

Browse files
Robert LyAndroid Git Automerger
authored andcommitted
am f19e4b4: Merge "docs: nfc ndef helper methods bug 5957772" into jb-dev
* commit 'f19e4b420de65306f09c199829e7e06fe210e21d': docs: nfc ndef helper methods bug 5957772
2 parents fb554fc + f19e4b4 commit fba8b7f

File tree

1 file changed

+63
-40
lines changed
  • docs/html/guide/topics/connectivity/nfc

1 file changed

+63
-40
lines changed

docs/html/guide/topics/connectivity/nfc/nfc.jd

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ other two intents, giving the user a better experience.</p>
318318
</pre>
319319
</li>
320320

321-
<li>The <code>uses-feature</code> element so that your application shows up in Google
322-
Play only for devices that have NFC hardware:
321+
<li>The <code>uses-feature</code> element so that your application shows up in Google Play
322+
only for devices that have NFC hardware:
323323
<pre>
324324
&lt;uses-feature android:name="android.hardware.nfc" android:required="true" /&gt;
325325
</pre>
@@ -511,13 +511,24 @@ contain the payload and allow you to enumerate the tag's technologies:</p>
511511

512512
<h2 id="creating-records">Creating Common Types of NDEF Records</h2>
513513
<p>This section describes how to create common types of NDEF records to help you when writing to
514-
NFC tags or sending data with Android Beam. It also describes how to create the corresponding
514+
NFC tags or sending data with Android Beam. Starting with Android 4.0 (API level 14), the
515+
{@link android.nfc.NdefRecord#createUri createUri()} method is available to help you create
516+
URI records automatically. Starting in Android 4.1 (API level 16), {@link android.nfc.NdefRecord#createExternal createExternal()}
517+
and {@link android.nfc.NdefRecord#createMime createMime()} are available to help you create
518+
MIME and external type NDEF records. Use these helper methods whenever possible to avoid mistakes
519+
when manually creating NDEF records.</p>
520+
521+
<p>
522+
This section also describes how to create the corresponding
515523
intent filter for the record. All of these NDEF record examples should be in the first NDEF
516524
record of the NDEF message that you are writing to a tag or beaming.</p>
517525

518526
<h3 id="abs-uri">TNF_ABSOLUTE_URI</h3>
519-
<p>Given the following {@link android.nfc.NdefRecord#TNF_ABSOLUTE_URI} NDEF record, which is
520-
stored as the first record inside of an {@link android.nfc.NdefMessage}:</p>
527+
<p class="note"><strong>Note:</strong> We recommend that you use the
528+
<a href="#well-known-uri"><code>RTD_URI</code></a> type instead
529+
of {@link android.nfc.NdefRecord#TNF_ABSOLUTE_URI}, because it is more efficient.</p>
530+
531+
<p>You can create a {@link android.nfc.NdefRecord#TNF_ABSOLUTE_URI} NDEF record in the following way:</p>
521532

522533
<pre>
523534
NdefRecord uriRecord = new NdefRecord(
@@ -526,7 +537,7 @@ NdefRecord uriRecord = new NdefRecord(
526537
new byte[0], new byte[0]);
527538
</pre>
528539

529-
<p>the intent filter would look like this:</p>
540+
<p>The intent filter for the previous NDEF record would look like this:</p>
530541
<pre>
531542
&lt;intent-filter&gt;
532543
&lt;action android:name="android.nfc.action.NDEF_DISCOVERED" /&gt;
@@ -537,32 +548,35 @@ NdefRecord uriRecord = new NdefRecord(
537548
&lt;/intent-filter&gt;
538549
</pre>
539550

540-
541551
<h3 id="mime">TNF_MIME_MEDIA</h3>
542-
<p>Given the following {@link android.nfc.NdefRecord#TNF_MIME_MEDIA} NDEF record, which is stored as
543-
the first record inside
544-
of an {@link android.nfc.NdefMessage}:</p>
552+
<p>You can create a {@link android.nfc.NdefRecord#TNF_MIME_MEDIA} NDEF record in the following ways.</p>
553+
554+
<p>Using the {@link android.nfc.NdefRecord#createMime createMime()} method:</p>
555+
<pre>
556+
NdefRecord mimeRecord = NdefRecord.createMime("application/vnd.com.example.android.beam",
557+
"Beam me up, Android".getBytes(Charset.forName("US-ASCII")));
558+
</pre>
559+
560+
<p>Creating the {@link android.nfc.NdefRecord} manually:</p>
545561
<pre>
546562
NdefRecord mimeRecord = new NdefRecord(
547563
NdefRecord.TNF_MIME_MEDIA ,
548-
"application/com.example.android.beam".getBytes(Charset.forName("US-ASCII")),
564+
"application/vnd.com.example.android.beam".getBytes(Charset.forName("US-ASCII")),
549565
new byte[0], "Beam me up, Android!".getBytes(Charset.forName("US-ASCII")));
550566
</pre>
551567

552-
<p>the intent filter would look like this:</p>
568+
<p>The intent filter for the previous NDEF records would look like this:</p>
553569
<pre>
554570
&lt;intent-filter&gt;
555571
&lt;action android:name="android.nfc.action.NDEF_DISCOVERED" /&gt;
556572
&lt;category android:name="android.intent.category.DEFAULT" /&gt;
557-
&lt;data android:mimeType="application/com.example.android.beam" /&gt;
573+
&lt;data android:mimeType="application/vnd.com.example.android.beam" /&gt;
558574
&lt;/intent-filter&gt;
559575
</pre>
560576

561-
562577
<h3 id="well-known-text">TNF_WELL_KNOWN with RTD_TEXT</h3>
563578

564-
<p>Given the following {@link android.nfc.NdefRecord#TNF_WELL_KNOWN} NDEF record, which is stored as
565-
the first record inside of an {@link android.nfc.NdefMessage}:</p>
579+
<p>You can create a {@link android.nfc.NdefRecord#TNF_WELL_KNOWN} NDEF record in the following way:</p>
566580
<pre>
567581
public NdefRecord createTextRecord(String payload, Locale locale, boolean encodeInUtf8) {
568582
byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
@@ -592,9 +606,20 @@ public NdefRecord createTextRecord(String payload, Locale locale, boolean encode
592606

593607
<h3 id="well-known-uri">TNF_WELL_KNOWN with RTD_URI</h3>
594608

595-
<p>Given the following {@link android.nfc.NdefRecord#TNF_WELL_KNOWN} NDEF record, which is stored as
596-
the first record inside of an {@link android.nfc.NdefMessage}:</p>
609+
<p>You can create a {@link android.nfc.NdefRecord#TNF_WELL_KNOWN} NDEF record in the following ways.</p>
610+
611+
<p>Using the {@link android.nfc.NdefRecord#createUri(String)} method:</p>
612+
<pre>
613+
NdefRecord rtdUriRecord1 = NdefRecord.createUri("http://example.com");
614+
</pre>
597615

616+
<p>Using the {@link android.nfc.NdefRecord#createUri(Uri)} method:</p>
617+
<pre>
618+
Uri uri = new Uri("http://example.com");
619+
NdefRecord rtdUriRecord2 = NdefRecord.createUri(uri);
620+
</pre>
621+
622+
<p>Creating the {@link android.nfc.NdefRecord} manually:</p>
598623
<pre>
599624
byte[] uriField = "example.com".getBytes(Charset.forName("US-ASCII"));
600625
byte[] payload = new byte[uriField.length + 1]; //add 1 for the URI Prefix
@@ -604,7 +629,7 @@ NdefRecord rtdUriRecord = new NdefRecord(
604629
NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);
605630
</pre>
606631

607-
<p>the intent filter would look like this:</p>
632+
<p>The intent filter for the previous NDEF records would look like this:</p>
608633

609634
<pre>
610635
&lt;intent-filter&gt;
@@ -617,24 +642,32 @@ NdefRecord rtdUriRecord = new NdefRecord(
617642
</pre>
618643

619644
<h3 id="ext-type">TNF_EXTERNAL_TYPE</h3>
620-
<p>Given the following {@link android.nfc.NdefRecord#TNF_EXTERNAL_TYPE} NDEF record, which is stored
621-
as the first record inside of an {@link android.nfc.NdefMessage}:</p>
645+
<p>You can create a {@link android.nfc.NdefRecord#TNF_EXTERNAL_TYPE} NDEF record in the following ways:</p>
646+
647+
<p>Using the {@link android.nfc.NdefRecord#createExternal createExternal()} method:
648+
<pre>
649+
byte[] payload; //assign to your data
650+
String domain = "com.example"; //usually your app's package name
651+
String type = "externalType";
652+
NdefRecord extRecord = NdefRecord.createExternal(domain, type, payload);
653+
</pre>
622654

655+
<p>Creating the {@link android.nfc.NdefRecord} manually:</p>
623656
<pre>
624657
byte[] payload;
625658
...
626-
NdefRecord mimeRecord = new NdefRecord(
627-
NdefRecord.TNF_EXTERNAL_TYPE, "example.com:externalType", new byte[0], payload);
659+
NdefRecord extRecord = new NdefRecord(
660+
NdefRecord.TNF_EXTERNAL_TYPE, "com.example:externalType", new byte[0], payload);
628661
</pre>
629662

630-
<p>the intent filter would look like this:</p>
663+
<p>The intent filter for the previous NDEF records would look like this:</p>
631664
<pre>
632665
&lt;intent-filter&gt;
633666
&lt;action android:name="android.nfc.action.NDEF_DISCOVERED" /&gt;
634667
&lt;category android:name="android.intent.category.DEFAULT" /&gt;
635668
&lt;data android:scheme="vnd.android.nfc"
636669
android:host="ext"
637-
android:pathPrefix="/example.com:externalType"/&gt;
670+
android:pathPrefix="/com.example:externalType"/&gt;
638671
&lt;/intent-filter&gt;
639672
</pre>
640673

@@ -840,8 +873,8 @@ public class Beam extends Activity implements CreateNdefMessageCallback {
840873
String text = ("Beam me up, Android!\n\n" +
841874
"Beam Time: " + System.currentTimeMillis());
842875
NdefMessage msg = new NdefMessage(
843-
new NdefRecord[] { createMimeRecord(
844-
"application/com.example.android.beam", text.getBytes())
876+
new NdefRecord[] { createMime(
877+
"application/vnd.com.example.android.beam", text.getBytes())
845878
/**
846879
* The Android Application Record (AAR) is commented out. When a device
847880
* receives a push with an AAR in it, the application specified in the AAR
@@ -882,36 +915,26 @@ public class Beam extends Activity implements CreateNdefMessageCallback {
882915
// record 0 contains the MIME type, record 1 is the AAR, if present
883916
textView.setText(new String(msg.getRecords()[0].getPayload()));
884917
}
885-
886-
/**
887-
* Creates a custom MIME type encapsulated in an NDEF record
888-
*/
889-
public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
890-
byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
891-
NdefRecord mimeRecord = new NdefRecord(
892-
NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
893-
return mimeRecord;
894-
}
895918
}
896919
</pre>
897920

898921
<p>Note that this code comments out an AAR, which you can remove. If you enable the AAR, the
899922
application specified in the AAR always receives the Android Beam message. If the application is not
900-
present, Google Play launches to download the application. Therefore, the following intent
923+
present, Google Play is started to download the application. Therefore, the following intent
901924
filter is not technically necessary for Android 4.0 devices or later if the AAR is used:
902925
</p>
903926

904927
<pre>
905928
&lt;intent-filter&gt;
906929
&lt;action android:name="android.nfc.action.NDEF_DISCOVERED"/&gt;
907930
&lt;category android:name="android.intent.category.DEFAULT"/&gt;
908-
&lt;data android:mimeType="application/com.example.android.beam"/&gt;
931+
&lt;data android:mimeType="application/vnd.com.example.android.beam"/&gt;
909932
&lt;/intent-filter&gt;
910933
</pre>
911934
<p>With this intent filter, the <code>com.example.android.beam</code> application now can be started
912935
when it scans an NFC tag or receives an Android Beam with an AAR of
913936
type <code>com.example.android.beam</code>, or when an NDEF formatted message contains a MIME record
914-
of type <code>application/com.example.android.beam</code>.</p>
937+
of type <code>application/vnd.com.example.android.beam</code>.</p>
915938

916939
<p>Even though AARs guarantee an application is started or downloaded, intent filters are
917940
recommended, because they let you start an Activity of your choice in your

0 commit comments

Comments
 (0)