How to Build an NFC-enabled App

  • 4,627 views
Uploaded on

This presentation gives developers a look into how to build NFC-enabled applications. Included is a brief introduction to NFC architecture, NDEF Tags, developing tag readers, using WebWorks to read …

This presentation gives developers a look into how to build NFC-enabled applications. Included is a brief introduction to NFC architecture, NDEF Tags, developing tag readers, using WebWorks to read tags, and how to write a peer-to-peer app.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,627
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
0
Comments
0
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. How to Build an NFC-enabled AppJohn Murray and Martin WoolleyDEV 311February 7-8, 2012
  • 2. Session Overview Introductions Brief introduction to NFC How to read NDEF Tags How to write a Peer-to-Peer App Close @BlackBerryDev – 2
  • 3. NFC Forum Architecture  We will look at examples of:  Reading NDEF Tags NDEF Tags  Peer to Peer using SNEP SNEP 3
  • 4. Reading NDEF tagsMartin Woolley
  • 5. NFC Forum Architecture  We will look at examples of:  Reading NDEF Tags NDEF Tags  Peer to Peer using SNEP 5
  • 6. About NFC Tags An NFC tag is typically a passive device (for example, integrated in a smart poster) that stores data that can be read by an NFC-enabled device. 4 types are defined by the NFC Forum. A card and a tag are technically the same. Think of a tag as being a card with a predefined, relatively simple and standardised application. Data is stored in the NDEF format It’s the standardisation that makes tags so useful! 6
  • 7. The Smart Tags AppYour BlackBerry had a standard tagreader/write app already…..
  • 8. About NFC Tags The standard BlackBerry® application “Smart Tags” was able to read the NFC smart poster at this bus stop in France  The browser opened at the transport company’s web site and the next bus time was displayed  Think about this! No special software from the bus company and extra infrastructure was required at the bus stop. 8
  • 9. A simple NDEF tag readerLet’s take a look at a simple NDEF tag readerin action. We’ll then examine the coderequired to build this kind of application.
  • 10. Developing your own tag readerThree simple steps:1. Implement the NDEFMessageListener interface2. Register your listener with the system, indicating the NDEF record type you are interested in3. Parse and act upon NDEF messages received by your NDEFMessageListener 10
  • 11. NDEFMessageListenerpublic class NfcReadNdefSmartTagListener implements NDEFMessageListener { public void onNDEFMessageDetected(NDEFMessage message) { // do something with the NDEF message }} 11
  • 12. Registering the listener ReaderWriterManager rwManager = ReaderWriterManager.getInstance(); rwManager.addNDEFMessageListener ( listener, // the NDEFMessageListener NDEFRecord.TNF_WELL_KNOWN, // a Type Name Format value "Sp” // “Sp” means smart poster );// 7.1 - our app will auto start if it is not already running// 7.0 - must request this behaviour in alternate method signature 12
  • 13. Parsing the NDEF messagepublic void onNDEFMessageDetected(NDEFMessage message) { NDEFRecord[] records = message.getRecords(); SmartPosterRecord sp = new SmartPosterRecord(records[0]); String uri = sp.getUri(); // do something with the data} 13
  • 14. Parsing the NDEF messageHelper classes exist for Smart Poster, Text and URI recordsFor other record types, implement your own parserpublic void onNDEFMessageDetected(NDEFMessage message) { NDEFRecord[] records = message.getRecords(); for(int j = records.length - 1; j >= 0; j--) { byte[] payloadBytes = records[j].getPayload(); // process the byte arrays manually and extract what // you are interested in }} 14
  • 15. Using WebWorks to readNFC smart postersYou can read smart poster NDEF tags fromyour BlackBerry WebWorks application too
  • 16. WebWorks and NFC tags Later last year, we developed a BlackBerry® WebWorks™ extension which allows you to incorporate NFC smart posters in your WebWorks applications This was described in one of our NFC articles in the developer resource centre:  http://supportforums.blackberry.com/t5/Web-and-WebWorks- Development/NFC-NDEF-Tag-Reading-from-WebWorks- Applications/ta-p/1430431  The code is in our GitHub WebWorks Community APIs repo. 16
  • 17. Register your listenerfunction registerListener(record_type) { var ok = blackberry.nfc.ndef.register_ndef( ndefMessageReceived, onNdefError, record_type ); if (ok) { // }} 17
  • 18. Implement callback functionsfunction ndefMessageReceived(ndef_message) { alert("NDEF message:"+ndef_message);}function onNdefError(error_message) { alert("NDEF error:"+error_message);} 18
  • 19. Message formatMessages are JSON formatted for easy handlingin JavaScript! 19
  • 20. NFC SNEP Responder ExampleDemonstrate how to write an NFC SNEPNDEF Responder application to serve out acontact in vCard format.John Muray
  • 21. NFC Forum Peer-to-Peer  We will look at examples of:  Reading NDEF Tags  Peer to Peer using SNEP SNEP 21
  • 22. NCF Peer-to-Peer (7.1) LLCP (Logical Link Control Protocol)  Establishes a two-way communications channel between two endpoints. Connection Handover  Uses NFC LLCP to exchange sufficient information to allow a Bluetooth® connection to be established and subsequently used. NDEF used as the message format. SNEP (Simple NDEF Exchange Protocol)  SNEP is an application level protocol running over LLCP  It uses NDEF messages as the application level messaging format  A client reads a message as if it were from a tag 22
  • 23. Simple SNEP vCard Server 23
  • 24. Simple SNEP vCard Server 24
  • 25. Developing your own SNEP appThree simple steps:1. Implement the NDEFMessageBuilder interface2. Implement the NDEFPushStatusCallback interface3. Register instances of these with the system via the single instance of the NDEFPushManager 25
  • 26. Simple SNEP vCard Serverpublic static final String VCARD_MIME_TYPE = "text/x-vCard";public static final String VCARD_DATA = For purpose of this example "BEGIN:VCARDn" + the vCard data will be built "VERSION:3.0n" + into the application statically – "N:Murray;John;;;n" + in a real app this could be "FN:John Murrayn" + dynamic. "ADR;TYPE=WORK:;;200, Bath Road;Slough;Berkshire;SL1 3XE;UKn" +... "URL:http://www.rim.comn" + "X-ORG-URL:http://www.rim.comn" + "END:VCARD";} 26
  • 27. Simple SNEP vCard Serverpublic final class NfcSnepResponderScreen extends MainScreen {... private NDEFPushManager _ndefPushManager = null;... Retrieves the system instance public NfcSnepResponderScreen() { of NDEFPushManager which... allows access to the NDEF try { Push features of the API. _ndefPushManager = NDEFPushManager.getInstance(); } catch(NFCException e) { ... }...} 27
  • 28. Simple SNEP vCard Serverprotected void registerForNdefPush() {... _myMsgBuilder = new NfcSnepResponderMsgBuilder(); _myStatusCallBack = new NfcSnepResponderCallBack();... if (_ndefPushManager != null) { _ndefPushId = _ndefPushManager.pushNDEF( _myMsgBuilder, _myStatusCallBack);... Register an instance that will } else { ... } build the correct NDEF} message on demand and one that will be notified of the result. 28
  • 29. Simple SNEP vCard Serverprotected void unRegisterForNdefPush() {... if (_ndefPushManager != null) { _ndefPushManager.cancelNDEFPush( _ndefPushId );... } else { ... }} Cancel the registration using the identifier that we were presented with on registration. 29
  • 30. Simple SNEP vCard Serverpublic class NfcSnepResponderMsgBuilder implements NDEFMessageBuilder {... Build the NDEFMessage that public NDEFMessage[] buildNDEFMessages() { will be served out using SNEP NDEFMessage[] listOfNdefMessages = null; when a NDEFMessage myNdefMessage; try { myNdefMessage = NDEFMessageUtils.createMediaTypeNDEFMessage( Constants.VCARD_MIME_TYPE, Constants.VCARD_DATA.getBytes(“US-ASCII”)); listOfNdefMessages = new NDEFMessage[]{myNdefMessage}; } catch(NFCException e) { ... } return listOfNdefMessages; }} 30
  • 31. Simple SNEP vCard Serverpublic class NfcSnepResponderCallBack implements NDEFPushStatusCallback {... public void onNDEFPushStatusUpdate(NDEFMessage ndefMessage, int stat){... switch (stat) { case NDEFPushStatusCallback.SUCCESS: // do something ... Figure out what happened to break; the request that was served out... to the SNEP client. }} 31
  • 32. Closing RemarksMartin Woolley
  • 33. For More Information… Articles we’ve written:  http://supportforums.blackberry.com/t5/Java-Development/NFC- Primer-for-Developers/ta-p/1334857  http://supportforums.blackberry.com/t5/Java- Development/Reading-and-Writing-NFC-Smart-Tags/ta-p/1379453  http://supportforums.blackberry.com/t5/Web-and-WebWorks- Development/NFC-NDEF-Tag-Reading-from-WebWorks- Applications/ta-p/1430431  http://supportforums.blackberry.com/t5/Java-Development/NFC- Virtual-Target-Emulation/ta-p/1509687 33
  • 34. For More Information… Our Sample Code on GitHub  https://github.com/blackberry/Samples-for-Java  https://github.com/blackberry/WebWorks-Community-APIs Documentation  Javadoc™  Network and Communications Developer Guide 34
  • 35. THANK YOUJohn Murray and Martin WoolleyDEV311February 7-8, 2012