This document will describe how to enable NFC on an Android device and how to create an Android application using NFC. The first step is to enable NFC on the Android phone’s Settings menu to allow for use of the Tag Dispatch System. This system analyzes scanned NFC tags, parses them, and attempts to locate relevant applications.
Tag Dispatch and Intent Filtering
In order for an Android application to have access to NFC on a device, you must grant access to the application by adding the following line to the AndroidManifest.xml file:
The application must also be using a minimum SDK version of 10. If NFC is used for a critical part of the application, an element must be included in AndroidManifest.xml so that on Google Play, it only shows up for devices that have NFC:
To automatically start an application when a specific NFC tag is scanned, you must specify a specific intent for the Android manifest to filter for. Usually, the ACTION_NDEF_DISCOVERED intent is filtered and to do this, that intent filter must be declared along with the type of data that you want to filter for. An example of how to do this is shown below:
You could also, however, filter for the ACTION_TECH_DISCOVERED intent, in which case you must create an XML resource file that specifies the technologies that your activity supports within a tech-list set within any file in the
/res/xml folder. An example of this could be done by:
android.nfc.tech.example
...
A similar intent-filter tag must also be created in AndroidManifest.xml.
If an activity start because of an NFC intent, you would use the ACTION_TAG_DISCOVERED intent to obtain information about the scanned tag (a similar intent-filter tag as to above would be created as well). If you are expecting any extras on a receiving intent, you must also retrieve the data from them within the Java code.
NDEF Records
In order to create NDEF records to help when writing to NFC tags in Android, it is recommended to have a minimum API level of at least 14 in order to have access to the createUri() method to help create URI records automatically. These automated helper methods can decrease the opportunity to make mistakes when creating NDEF records (manual or automated).
The most commonly used NDEF record is TNF_ABSOLUTE_URI. An example of its usage within Java is:
NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI,
"http://developer.android.com/index.html".getBytes(Charset.forName("US-ASCII")),new byte[0], new byte[0]);
An intent filter would also be created within AndroidManifest.xml as such:
android:host="developer.android.com"
android:pathPrefix="/index.html" />
Note that RTD_URI is typically used instead of TNF_ABSOLUTE_URI as it is more efficient.
A TNF_MIME_MEDIA NDEF record could be created manually similar as to above, however, it could also be created in a more automated manner as in the following example:
NdefRecord mimeRecord = NdefRecord.createMime("application/vnd.com.example.android.beam", "Beam me up, Android".getBytes(Charset.forName("US-ASCII")));
There are various other NDEF records such as TNF_WELL_KNOWN, TNF_EXTERNAL_TYPE, and RTD_TEXT which can be created in a similar fashion as above.
Android Application Records
Android Application Records can be created in API level 14 and higher to provide a stronger certainty that the application is started when an NFC tag is scanned. This is useful to prevent other applications from filtering for the same intent and potentially handling specific tags that you have deployed. If a tag contains an AAR, it will be dispatched by the tag dispatch system. The necessary code to implement an AAR is:
NdefMessage msg = new NdefMessage(
new NdefRecord[] {
...,
NdefRecord.createApplicationRecord("com.example.android.beam")}
Beaming NDEF Messages to Other Devices
One useful implantation of NFC is called Android Beam. Android Beam allows simple peer-to-peer data exchange between two Android-powered devices. The application that wants to beam data to another device must be in the foreground and the device receiving the data must not be locked. In order to enable Android Beam for your application, you must either call the setNdefPushMessage() method or the setNdefPushMessageCallback() method. The former accepts an NdefMessage to set as the message to beam while the latter accepts a callback that contains a createNdefMessage() method call for when a device is in range to beam data to.
Since an activity can only push one NDEF message at a time, setNdefPushMessageCallback() will take precedence over setNdefPushMessage() if both have been set. In order to actually use and enable Android Beam, you must create an NdefMessage that contains the NdefRecords that you want to push onto the other device. Then you must make a call to setNdefPushMessage() with a NdefMessage or make a to call setNdefPushMessageCallback() with an NfcAdapter.CreateNdefMessageCallback object (from the onCreate() method) an argument.
Share with your friends: |