near field communication (nfc) in android

23
Presenter: Jitendra Singh, Mindfire Solutions Date: 12/feb/2015 NEAR FIELD COMMUNICATION

Upload: mindfire-solutions

Post on 15-Jul-2015

294 views

Category:

Software


3 download

TRANSCRIPT

Presenter: Jitendra Singh, Mindfire Solutions Date: 12/feb/2015

NEAR FIELD COMMUNICATION

Presenter: Jitendra Singh, Mindfire Solutions

About Me

Jitendra SinghSenior Android DeveloperMindfire Solutions

Skills: AndroidApplicationDevelopment, Blackberry,PhoneGap, Struts, Hibernate, Servlets, JSP, J2EE, Apache, CoreJava, SQL, SQLite, SQLServer, J2ME, JasperReport,NFC

Skype: mfsi_jitendra.singh

Email:[email protected]

Presenter: Jitendra Singh, Mindfire Solutions

AGENDA1) What is NFC? 2) Classes available in Android to use NFC 3) NDEF Structure

4) Tag Intent Dispatch System

5) NFC Intents

6) Filtering for NFC Intents

7) How to check whether a device supports NFC or not

8) NFC Tag Reading/Writing

9) Android Application Record

Presenter: Jitendra Singh, Mindfire Solutions

1) NFC is a high frequency short-range wireless communication technology that enables the exchange of data between devices over about a 10 cm distance.

2) Near-field communication uses electromagnetic induction between two loop antennas located within each others near field.

3) Android-powered devices with NFC mainly support two main modes of operation:

a) Reader/writer mode, allowing the NFC device to read and/or write passive NFC tags and stickers.

b) P2P mode, allowing the NFC device to exchange data with other NFC peers; this operation mode is used by Android Beam.

Presenter: Jitendra Singh, Mindfire Solutions

1) NFC-related APIs in Android are introduced to users starting from API level 9.

2) Currently, there are two packages for NFC application development in the Android platform.

a) The first is the main package that you will use is android.nfc, which includes necessary classes to enable applications to read and write NDEF messages

in/to NFC tags.

b) The second is the android.nfc.tech package, which includes necessary classes to provide access to different tag technologies such as MIFARE Classic, NfcA , NfcV, and so on.

Continued........

Presenter: Jitendra Singh, Mindfire Solutions

CLASS NAME DESCRIPTION

Tag Represents the discovered NFC tag

NfcAdapter Represents the NFC adapter of the mobile phone

NfcManager Obtains an instance of the NFC adapter

NdefMessage Represents an NDEF message

NdefRecord Represents an NDEF record

Classes available in the android.nfc package are as follows

Presenter: Jitendra Singh, Mindfire Solutions

1) All the messages that are transferred between two android devices or between devices or a NFC tag are mostly transferred in NDEF format.

2) NDEF is a binary format structured in messages, each of which can contain several records.

3) Each record is made up of a header, which contains meta data about the record, such as the record type,length, and so forth, and the payload, which contains the content of the message.

Continued........

Presenter: Jitendra Singh, Mindfire Solutions

4) An NDEF record consists of a type name format (TNF), payload type, payload identifier, and the payload.

5) The TNF tells you how to interpret the payload type. The payload type is an NFC-specific type, MIME mediatype, or URI that tells you how to interpret the payload.

6) Another way to think about this is that the TNF is the metadata about the payload type, and the payload type is the metadata about the payload.

Presenter: Jitendra Singh, Mindfire Solutions

1) The tag intent dispatch system is used to launch applications when the predefined tags or NDEF data are identified in tags.

2) In short, you scan to an NFC tag, and if any application is registered to handle the tag, then the registered application launches.

3) If more than one application is registered to handle the tag, a pop-up to select the application (Activity Chooser) is displayed.

4)When an NFC tag is discovered in proximity, the type and payload data in the tag will be encapsulated to intent, and the tag intent dispatch system in Android will run the corresponding application that can handle the tag.

Continued........

Presenter: Jitendra Singh, Mindfire Solutions

ACTION_NDEF_DISCOVERED

The tag dispatch system uses the TNF and Type fields to try to map a known MIME type or URI to the NDEF message. If successful, it encapsulates that information inside of a ACTION_NDEF_DISCOVERED intent along with the actual payload.

This intent is used to start an Activity when a tag that contains an NDEF payload is scanned and is of a recognized type. This is the highest priority intent, and the tag dispatch system tries to start an Activity with this intent before any other intent, whenever possible.

ACTION_TECH_DISCOVERED

If the tag dispatch system cannot determine the Type of data based on the first NDEF record. This happens when the NDEF data cannot be mapped to a MIME type or URI.

In such cases, a Tag object that has information about the tag's technologies and the payload are encapsulated inside of a ACTION_TECH_DISCOVERED intent.

ACTION_TAG_DISCOVERED

This intent has the lowest priority. If no activities in the device can handle the corresponding above intents, then ACTION_TAG_DISCOVERED is created.

Continued........

Presenter: Jitendra Singh, Mindfire Solutions

1)The tag dispatch system works as follows:

➤ If the payload contains NDEF data:

1. Try to start an activity with the ACTION_NDEF_DISCOVERED intent.

2. If no activities filter for the discovered NDEF data, try to start an activity with the the ACTION_TECH_DISCOVERED intent.

➤ If the payload does not contain NDEF data but is of a known tag technology:

1. Try to start an activity with the ACTION_TECH_DISCOVERED intent.

2. If no activities filter for that intent, try to start an activity with the ACTION_TAG_DISCOVERED intent.

There are 3 types of NFC intents viz :

1) ACTION_NDEF_DISCOVERED2) ACTION_TECH_DISCOVERED3) ACTION_TAG_DISCOVERED

Presenter: Jitendra Singh, Mindfire Solutions

Presenter: Jitendra Singh, Mindfire Solutions

Filtering for ACTION_NDEF_DISCOVERED

This concept would be best explained by examples

Example 1

<intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain" /></intent-filter>

Example 2

<intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="http" android:host="developer.android.com" android:pathPrefix="/index.html" /></intent-filter>

Continued........

Presenter: Jitendra Singh, Mindfire Solutions

Filtering for ACTION_TECH_DISCOVERED

If your activity filters for the ACTION_TECH_DISCOVERED intent, you must create an XML resource file that specifies the technologies that your activity supports within a tech-list set. Your activity is considered a match if a tech-list set is a subset of the technologies that are supported by the tag.

Example<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <tech-list> <tech>android.nfc.tech.IsoDep</tech> <tech>android.nfc.tech.NfcA</tech> <tech>android.nfc.tech.NfcB</tech> <tech>android.nfc.tech.NfcF</tech> <tech>android.nfc.tech.NfcV</tech> <tech>android.nfc.tech.Ndef</tech> <tech>android.nfc.tech.NdefFormatable</tech> <tech>android.nfc.tech.MifareClassic</tech> <tech>android.nfc.tech.MifareUltralight</tech> </tech-list></resources>

In your AndroidManifest.xml file, specify the resource file that you just created in the <meta-data> element inside the <activity> element like in the following example:

<activity><intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED"/></intent-filter><meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" /></activity>

Presenter: Jitendra Singh, Mindfire Solutions

Filtering for ACTION_TECH_DISCOVERED

To filter for ACTION_TAG_DISCOVERED use the following intent filter:

<intent-filter> <action android:name="android.nfc.action.TAG_DISCOVERED"/></intent-filter>

Presenter: Jitendra Singh, Mindfire Solutions

How to check whether a device supports NFC or not

The NFCAdapter class in the android.nfc package represents the NFC adapter for the local machine. Checking if the NFCAdapter is available is the first thing to do in an NFC application. Here is the code to create the necessary NfcAdapter:

private NfcAdapter myNfcAdapter;private TextView myText;

@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);myNfcAdapter = NfcAdapter.getDefaultAdapter(this);if (myNfcAdapter == null){myText.setText("NFC is not available for the device!!!");}else{myText.setText("NFC is available for the device");}}

Presenter: Jitendra Singh, Mindfire Solutions

How to check whether a device supports NFC or not

The NFCAdapter class in the android.nfc package represents the NFC adapter for the device. Checking if the NFCAdapter is available is the first thing to do in an NFC application. Here is the code to create the necessary NfcAdapter:

private NfcAdapter myNfcAdapter;private TextView myText;

@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);myNfcAdapter = NfcAdapter.getDefaultAdapter(this);if (myNfcAdapter == null){myText.setText("NFC is not available for the device!!!");}else{myText.setText("NFC is available for the device");}}

Presenter: Jitendra Singh, Mindfire Solutions

8) NFC Tag Writing

In the following code, the basic write operation to a tag is shown, in which the discovered tag has NDEF payload.

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){ Tag detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG); Ndef ndef = Ndef.get(detectedTag); //If ndef object is null it means that the tag is not NDEF-formatted and you cannot use Ndef class. //If it is already NDEF-formatted, you will use Ndef class; otherwise, you need to use NdefFormatable //class with following code If(ndef == null)

NdefFormatable ndefFormat = NdefFormatable.get(detectedTag);

//If ndefFormat object is also null then it means that the tag does not support the NDEF //and we need to break the operation.

ndef.connect(); ndef.writeNdefMessage(message); ndef.close();}

Presenter: Jitendra Singh, Mindfire Solutions

NFC Tag Reading

Tag detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);

NdefMessage[] messages = getNdefMessages(getIntent());

NdefMessage[] getNdefMessages(Intent intent) { NdefMessage[] message = null; if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

Parcelable[] rawMessages =intent.getParcelableArrayExtraNfcAdapter.EXTRA_NDEF_MESSAGES); if (rawMessages != null) { message = new NdefMessage[rawMessages.length];

for (int i = 0; i < rawMessages.length; i++) { message[i] = (NdefMessage) rawMessages[i]; }

} else { Log.d("", "No messages inside tag"); } } else { Log.d("", "Unknown intent."); finish(); } return message;}

Presenter: Jitendra Singh, Mindfire Solutions

Android Application Record

1) Android Application Record (AAR) is introduced in API level 14 and is a very powerful property of Android NFC.

2) The main objective of AAR is to start the application when an NFC tag is scanned.

3) Despite the fact that the manifest file gives this property to NFC, AAR strengthens the property because more than one application may have been registered for, and may filter, the same intents on a mobile phone.

4) An AAR is the package name of an application. You need to insert the AAR record to an NDEF message, similarly to the way it is done with an NDEF record.

When an NFC tag is scanned, Android searches the entire NDEF message for AAR. If it fi nds an AAR in any of the NDEF records, it starts the corresponding application. If the application is not installed on the device, Google Play is launched automatically to download the corresponding application.

Police Scanner for Android - Locate and listen to Police dispatch frequencies.

An application/tool for the Android, which can locate police dispatch frequencies of various countries along with cities, counties, and channels, was something that one of our clients wanted to develop. Their objective was to help people (users of the app) stay informed about any major disaster (happened/forthcoming) by listening to emergency broadcast streams in real time. Their requirement revealed that they wanted the app to stream the media buffer of a radio channel over internet and play it with native media player. He also required that all users of the app would be automatically updated to the new list whenever there is a new entry to the database that contained the list of countries, cities or station. Their requirements were stringent and needed someone who can understand their vision and meet their expectations.

Click here for details >>

Sample Case Study

www.mindfiresolutions.com

www.mindfiresolutions.com

https://www.facebook.com/MindfireSolutions

http://www.linkedin.com/company/mindfire-solutions

http://twitter.com/mindfires