Nfc on Android


Published on

The sample app that demonstrates how to work with android and NFC technology is here:

Published in: Technology
1 Comment
  • hello Sir,nice presentation....but still i be Confused about Our(Software developer),what is our task in that type of nfc tasks like....chek this Sir
    ........i am going to implement this in my final year project....reply me plz....
    email id:
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Nfc on Android

  1. 1. A new world is coming… @fernando_cejas
  2. 2. Who am I?• Barcelona GDG Organizer• NFC, Android and Agile Geek•• NFC Actions App• Flojack (@flomio)• @fernando_cejas•
  3. 3. Agenda• What is NFC?• NFC Features and components• Common (and not) use cases• Android and NFC• Android NFC Development• Demo code• Faq
  4. 4. What is Near Field Communication?• Near Field Communication (NFC) is a global standard for short-range wireless connectivity technology• Driven by NFC Forum ( since 2004.• 160 + members, including Google, Intel, Microsoft, Nokia, NEC, Paypal, At&t, Visa, VTT.• Promote N-Mark trademark as a universal symbol for NFC.
  5. 5. Near Field Communication Physical action, virtual reactionIn practice, NFC enables simple and safetwo-way interactions among electronicdevices within a close proximity.
  6. 6. NFC Features• Short range technology – Connection established when devices or tag close enough (5-10 cm) – Simple setup (instant configuration)• 2-way communication – Not only reading like in contactless smart cards, but data exchange• Passive tags – No need for external power – Low-price• Security – Support secure element – Close proximity use brings some basic security
  7. 7. NFC Modes• Read and write tags• Peer-to-peer• Card emulationAndroid support: – Read and write (excellent support) – P2P (limited)
  8. 8. NFC Tags
  9. 9. How can NFC change the world?
  10. 10. NFC and Android• Tag Reading: since Android 2.3 – API level 9• Tag Writing: since Android 2.3.3 – API level 10• Beam (P2P): Android ICS 4.0 – API level 14Core Android classes: – NFC Manager – NFC Adapter – Tag Technology classes
  11. 11. NFC Manager• NFCManager used to get the NFCAdapter• Boring• Shortcut for getSystemService(NFC_SERVICE)
  12. 12. NFC Adapter• The real thing• Control Foreground Dispatch and P2P (Android Beam)• Check if NFC is turned on
  13. 13. Android Manifest• NFC Permission<uses-permission android:name="android.permission.NFC" />• API Level<uses-sdk android:minSdkVersion="10" />• NFC Feature<uses-feature android:name="android.hardware.nfc" android:required="true" />
  14. 14. NDEF Messages• NFC Data Exchange Format (NDEF)• NFC Forum Standard
  15. 15. NFC Android Dyspatch System
  16. 16. Android ManifestIn AndroidManifest.xml (4.0+):<manifest ...> <uses-sdk android:minSdkVersion="10" /> <uses-feature android:name="android.hardware.nfc” android:required="true" /> <uses-permission android:name="android.permission.NFC" /> <application ...> <activity ...> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED"/> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/plain" /> //Could be custom </intent-filter> </activity> </application></manifest>
  17. 17. Activity - Reacting to NFC scansIn the .java file for your Activity:import android.nfc.NfcAdapter;import android.nfc.NfcAdapter.CreateNdefMessageCallback;import android.nfc.NfcEvent;//...NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this);Boolean nfcEnabled = mNfcAdapter.isEnabled();//Give priority to the foreground activity to receive NFC scan intents.mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mReadTagFilters, null);//Enable Android Beam for peer-to-peer mNfcAdapter.setNdefPushMessageCallback(this,this);
  18. 18. Activity - Formatting to NDEF FormatIn the .java file for your Activity:import android.nfc.NdefMessage;import android.nfc.NdefRecord;import;import;//...String data = “Hello NFC”;// create a new NDEF record w/ NDEF message using the apps custom MIME type:String mimeType = "application/root.gast.playground.nfc"; //unique to your appbyte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8"));byte[] dataBytes = data.getBytes(Charset.forName("UTF-8"));byte[] id = new byte[0];NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, dataBytes);NdefMessage m = new NdefMessage(new NdefRecord[] { record });
  19. 19. Activity - Reading NDEF FormatIn the .java file for your Activity: import android.nfc.Tag;//...Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);NdefMessage[] msgs = new NdefMessage[rawMsgs.length];for (int i = 0; i < rawMsgs.length; i++) { msgs[i] = (NdefMessage) rawMsgs[i];}NdefRecord record = msgs[0].getRecords()[0];byte[] payload = record.getPayload(); //the info you wanted
  20. 20. Activity - Android BeamIn the .java file for your Activity://Implement the interfacepublic class BeamActivity extends Activity implements CreateNdefMessageCallback// Register callbackmNfcAdapter.setNdefPushMessageCallback(this, this);public NdefMessage createNdefMessage(NfcEvent event) { String data = "Hello NFC”; // create a new NDEF record and containing NDEF message: String mimeType = "application/”; byte[] mimeBytes = mimeType.getBytes(Charset.forName("UTF-8")); byte[] dataBytes = data.getBytes(Charset.forName("UTF-8")); byte[] id = new byte[0]; NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, dataBytes); NdefMessage message = new NdefMessage(new NdefRecord[] { record }); return message;}
  21. 21. Sample Application
  22. 22. Mobile OS support for NFC
  23. 23. Any questions?
  24. 24. Thanks!When you look at the dark side, careful you must be ... for the dark side looks back. @fernando_cejas