Android near Field COMMUNICATION---NFC Basics (iv)
Source: Internet
Author: User
<span id="Label3"></p><p><p>Transfer from http://blog.csdn.net/think_soft/article/details/8184539</p></p><p><p></p></p><p><p><strong>Get information from intent</strong></p></p><p><p>If you start an activity because of the intent of nfc, you will be able to get information about the scanned NFC tag from the Intent. Depending on the label being scanned, the intent object is able to provide the following additional information:</p></p><p><p>1. Extra_tag (required): It is a tag object that represents the tag being scanned;</p></p><p><p>2. Extra_ndef_messages (optional): It is an array that parses the NDEF message from the Tag. This additional information is enforced on the intent object;</p></p><p><p>3. {@link android.nfc.nfcadapter#extra_id (optional): The low-level ID of the label.</p></p><p><p>To get these additional information, make sure that your activity is initiated by the intent object of the NFC that was scanned, before you get additional information outside of Intent. The following example checks the intent of the action_ndef_discovered type and gets the NDEF message from the intent Object's additional Information.</p></p><p align="left"><p align="left">public void Onresume () {</p></p><p align="left"><p align="left">Super.onresume ();</p></p><p align="left"><p align="left">...</p></p><p align="left"><p align="left">If (NfcAdapter.ACTION_NDEF_DISCOVERED.equals (getintent (). getaction ())) {</p></p><p align="left"><p align="left">parcelable[] rawmsgs = Intent.getparcelablearrayextra (nfcadapter.extra_ndef_messages);</p></p><p align="left"><p align="left">If (rawmsgs! = Null) {</p></p><p align="left"><p align="left">msgs = new ndefmessage[rawmsgs.length];</p></p><p align="left"><p align="left">for (int i = 0; i < rawmsgs.length; i++) {</p></p><p align="left"><p align="left">msgs[i] = (ndefmessage) rawmsgs[i];</p></p><p align="left"><p align="left">}</p></p><p align="left"><p align="left">}</p></p><p align="left"><p align="left">}</p></p><p align="left"><p align="left">Process the msgs array</p></p><p><p>}</p></p><p><p>In addition, you can get a tag object from the intent object that contains the data payload and allows you to enumerate the techniques of the Tag:</p></p><p><p>Tag tag = Intent.getparcelableextra (nfcadapter.extra_tag);</p></p><p><p><strong>Create a common ndef record type</strong></p></p><p><p>This section describes how to create a common ndef record type to help you write to an NFC tag or send data with an Android Beam. Starting with Android4.0 (API Level14), You can use the Createuri () method to help you automatically create URI records. Starting with Android4.1 (API level 16), You can use the createexternal () and Createmime () methods to help you create MIME and external types of ndef RECORDS. Using these helper methods avoids the error of manually creating Ndef records as much as Possible.</p></p><p><p>This section also describes how to create a ndef record corresponding to the intent Filter. All of these ndef record examples that are written to or sent to an NFC tag should be the first record of the Ndef Message.</p></p><p><p><strong>Tnf_absolute_uri</strong></p></p><p><p>Note: We recommend that you use the Rtd_uri type instead of Tnf_absolute_uri because it is more efficient.</p></p><p><p>Create a ndef record of the Tnf_absolute_uri type in the following ways:</p></p><p align="left"><p align="left">Ndefrecord Urirecord = new Ndefrecord (</p></p><p align="left"><p align="left">ndefrecord.tnf_absolute_uri,</p></p><p align="left"><p align="left">"http://developer.android.com/index.html". getBytes (charset.forname ("us-ascii")),</p></p><p><p>New byte[0], New byte[0]);</p></p><p><p><strong>The corresponding intent filters are as Follows:</strong></p></p><p><p><intent-filter></p></p><p align="left"><p align="left"><action android:name= "android.nfc.action.NDEF_DISCOVERED"/></p></p><p align="left"><p align="left"><category android:name= "android.intent.category.DEFAULT"/></p></p><p align="left"><p align="left"><data android:scheme= "http"</p></p><p align="left"><p align="left">android:host= "developer.android.com"</p></p><p align="left"><p align="left">android:pathprefix= "/index.html"/></p></p><p><p></intent-filter></p></p><p><p><strong>Tnf_mime_media</strong></p></p><p><p>Use the following methods to create a ndef record of the Tnf_mime_media Type.</p></p><p><p><strong>Use the Createmime () method:</strong></p></p><p align="left"><p align="left">Ndefrecord Mimerecord = Ndefrecord.createmime ("application/vnd.com.example.android.beam",</p></p><p><p>"Beam me up, Android". getBytes (charset.forname ("us-ascii"));</p></p><p><p><strong>To create a Ndefrecord manually:</strong></p></p><p align="left"><p align="left">Ndefrecord Mimerecord = new Ndefrecord (</p></p><p align="left"><p align="left">ndefrecord.tnf_mime_media,</p></p><p align="left"><p align="left">"application/vnd.com.example.android.beam". getBytes (charset.forname ("us-ascii")),</p></p><p><p>New byte[0], "Beam me up, android!". GetBytes (charset.forname ("us-ascii"));</p></p><p><p><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-size: medium;"><span style="color: #222222;"><strong>The corresponding intent filters are as Follows:</strong></span></span></span></p></p><p><p><intent-filter></p></p><p align="left"><p align="left"><action android:name= "android.nfc.action.NDEF_DISCOVERED"/></p></p><p align="left"><p align="left"><category android:name= "android.intent.category.DEFAULT"/></p></p><p align="left"><p align="left"><data android:mimetype= "application/vnd.com.example.android.beam"/></p></p><p><p></intent-filter></p></p><p><p></p></p><p><p><strong>Tnf_well_known and Rtd_text</strong></p></p><p><p>Create a ndef record of the Tnf_well_known type in the following ways:</p></p><p align="left"><p align="left">Public Ndefrecord createtextrecord (String payload, locale locale, boolean encodeInUtf8) {</p></p><p align="left"><p align="left">byte[] langbytes = locale.getlanguage (). getBytes (charset.forname ("us-ascii"));</p></p><p align="left"><p align="left">Charset utfencoding = encodeInUtf8? Charset.forname ("UTF-8"): Charset.forname ("UTF-16");</p></p><p align="left"><p align="left">byte[] textbytes = payload.getbytes (utfencoding);</p></p><p align="left"><p align="left">int utfbit = encodeInUtf8? 0: (1 << 7);</p></p><p align="left"><p align="left">Char status = (char) (utfbit + langbytes.length);</p></p><p align="left"><p align="left">byte[] data = new Byte[1 + langbytes.length + textbytes.length];</p></p><p align="left"><p align="left">data[0] = (byte) status;</p></p><p align="left"><p align="left">System.arraycopy (langbytes, 0, data, 1, langbytes.length);</p></p><p align="left"><p align="left">System.arraycopy (textbytes, 0, data, 1 + langbytes.length, textbytes.length);</p></p><p align="left"><p align="left">Ndefrecord record = new Ndefrecord (ndefrecord.tnf_well_known,</p></p><p align="left"><p align="left">ndefrecord.rtd_text, New byte[0], data);</p></p><p align="left"><p align="left">Return record;</p></p><p><p>}</p></p><p><p><strong>The corresponding intent filters are as Follows:</strong></p></p><p align="left"><p align="left"><intent-filter></p></p><p align="left"><p align="left"><action android:name= "android.nfc.action.NDEF_DISCOVERED"/></p></p><p align="left"><p align="left"><category android:name= "android.intent.category.DEFAULT"/></p></p><p align="left"><p align="left"><data android:mimetype= "text/plain"/></p></p><p><p></intent-filter></p></p><p><p></p></p><p><p><strong>Tnf_well_know and Rtd_uri</strong></p></p><p><p>Create a ndef record of the Tnf_well_known type in the following ways.</p></p><p><p><strong>Use the Createuri (String) method:</strong></p></p><p><p>Ndefrecord rtdUriRecord1 =ndefrecord.createuri ("http://example.com");</p></p><p><p><strong>Use the Createuri (Uri) method:</strong></p></p><p align="left"><p align="left">Uri uri = new Uri ("http://example.com");</p></p><p><p>Ndefrecord RtdUriRecord2 = Ndefrecord.createuri (uri);</p></p><p><p><strong>To create a Ndefrecord manually:</strong></p></p><p align="left"><p align="left">byte[] Urifield = "example.com". getBytes (charset.forname ("us-ascii"));</p></p><p align="left"><p align="left">byte[] payload = new Byte[urifield.length + 1]; Add 1 for the URI Prefix</p></p><p align="left"><p align="left">byte payload[0] = 0x01; Prefixes HTTP://WWW. to the URI</p></p><p align="left"><p align="left">System.arraycopy (urifield, 0, payload, 1, urifield.length); Appends URI to payload</p></p><p align="left"><p align="left">Ndefrecord Rtdurirecord = new Ndefrecord (</p></p><p><p>ndefrecord.tnf_well_known, ndefrecord.rtd_uri, New byte[0], payload);</p></p><p><p><strong>The corresponding intent filters are as Follows:</strong></p></p><p align="left"><p align="left"><intent-filter></p></p><p align="left"><p align="left"><action android:name= "android.nfc.action.NDEF_DISCOVERED"/></p></p><p align="left"><p align="left"><category android:name= "android.intent.category.DEFAULT"/></p></p><p align="left"><p align="left"><data android:scheme= "http"</p></p><p align="left"><p align="left">android:host= "example.com"</p></p><p align="left"><p align="left">android:pathprefix= ""/></p></p><p><p></intent-filter></p></p><p><p><strong>Tnf_external_type</strong></p></p><p><p>Use the following methods to create a record of type Tnf_external_type.</p></p><p><p><strong>Use the createexternal () method:</strong></p></p><p align="left"><p align="left">byte[] payload; Assign to your data</p></p><p align="left"><p align="left">String domain = "com.example"; Usually your app ' s package name</p></p><p align="left"><p align="left">String type = "externaltype";</p></p><p><p>Ndefrecord Extrecord = ndefrecord.createexternal (domain, type, payload);</p></p><p><p><strong>To create a Ndefrecord manually:</strong></p></p><p align="left"><p align="left">byte[] payload;</p></p><p align="left"><p align="left">...</p></p><p align="left"><p align="left">Ndefrecord Extrecord = new Ndefrecord (</p></p><p><p>ndefrecord.tnf_external_type, "com.example:externalType", New byte[0], payload);</p></p><p><p><strong>The corresponding intent filters are as Follows:</strong></p></p><p align="left"><p align="left"><intent-filter></p></p><p align="left"><p align="left"><action android:name= "android.nfc.action.NDEF_DISCOVERED"/></p></p><p align="left"><p align="left"><category android:name= "android.intent.category.DEFAULT"/></p></p><p align="left"><p align="left"><data android:scheme= "vnd.android.nfc"</p></p><p align="left"><p align="left">android:host= "ext"</p></p><p align="left"><p align="left">android:pathprefix= "/com.example:externaltype"/></p></p><p><p></intent-filter></p></p><p><p>Use a more generalized tnf_external_type-type NFC deployment to better support Android devices and Non-android Devices.</p></p><p><p>Note: a URN of type tnf_external_type contains the following format:</p></p><p><p>urn:nfc:ext:example.com.externalType, however, The RTD specification declaration of the NFC forum, the Urn's urn:nfc:ext: section must be ignored in the Ndef Record. So all the information you need to provide is to use the ":" sign to leave the domain name (example.com in the Example) and the type (externaltype in the example). When scheduling tnf_external_type types of records, Android converts urn:nfc:ext:example.com:externalType urns into vnd.android.nfc://ext/ The URI of the example.com:externalType, which is the intent filter declared in the Example.</p></p><p><p>Android near Field COMMUNICATION---NFC basis (d) (rpm)</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service