Beginning NFC by Tom Igoe, Don Coleman, and Brian Jepson Copyright © 2014 Tom Igoe, Don Coleman, and Brian Jepson. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or [email protected]. Editors: Rachel Roumeliotis and Allyson MacDonald Indexer: WordCo Indexing Services Production Editor: Nicole Shelby Cover Designer: Randy Comer Copyeditor: Jasmine Kwityn Interior Designer: David Futato Proofreader: Kiel Van Horn Illustrator: Rebecca Demarest January 2014: First Edition Revision History for the First Edition: 2013-01-13: First release See http://oreilly.com/catalog/errata.csp?isbn=9781449372064 for release details. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Beginning NFC, the image of a Central American squirrel monkey, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐ mark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN: 978-1-449-37206-4 [LSI] This book is dedicated to Red Burns. Table of Contents 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Who This Book Is For 2 Recommended Reading 2 What’s Covered in This Book 3 What You’ll Need 4 Hardware 4 Software 6 Other Useful NFC Apps 7 Conventions Used in This Book 7 Using Code Examples 8 Safari® Books Online 8 How to Contact Us 9 Acknowledgments 9 2. NFC and RFID. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 What’s RFID? 11 What’s NFC? 12 How RFID Operates 13 RFID Standards 13 How NFC Operates 14 NFC Data Exchange Format (NDEF) 14 The Architecture of NFC 15 NFC Tag Types 17 Where to Get Tags 18 Device-to-Tag Type Matching 19 What You Can Do with NFC 20 Conclusion 21 3. Getting Started with PhoneGap and the PhoneGap-NFC Library. . . . . . . . . . . . . . . . . . . 23 v Why Android? 23 Hello, World! Your First PhoneGap App 24 Setting Up the Development Environment 24 Creating a PhoneGap Project 29 The Important Files 33 A Simple Locator App 34 PhoneGap Meets NFC: NFC Reader 40 Installing the NFC Plug-In 41 Writing the NFC Reader App 42 Troubleshooting 46 Conclusion 46 4. Introducing NDEF. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 NDEF Structure 49 Type Name Format 51 Payload Type 51 Payload Identifier 53 Payload 53 Record Layout 53 Record Header 54 How Big Can an NDEF Message Be? 55 Record Chunking 56 Additional Info 56 NDEF in Practice 56 A Tag Writer Application: Foursquare Check-In 61 Writing an NDEF Record to a Tag 62 Writing Different Record Types 66 PhoneGap-NFC NDEF Helper Functions, Summarized 71 Conclusion 72 5. Listening for NDEF Messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 PhoneGap-NFC’s Event Listeners 75 An NDEF Reader Application 77 Listening for Multiple Events 78 Reading the NDEF Messages 81 Filtering Tags Using Record Types 84 Filtering Using MIME-Types 86 Android’s Tag Dispatch System 89 Types of Intents 89 Android Application Records 93 vi | Table of Contents Conclusion 93 6. An NFC Application in Practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 The User Interaction 97 Getting to Know Hue, Getting to Know All About Hue 98 The Hue Data Format 101 The Hue’s REST API 104 The Android Shell 104 The PhoneGap Media API 105 The User Interface 106 The Application Code 109 Housekeeping Functions 110 Global Event Handlers 112 Hub Communication Functions 114 Lighting User Interface Event Handlers 118 Music User Interface Event Handlers 121 NFC Event Handlers 126 Enabling Background Dispatch 128 Conclusion 129 7. Introduction to Arduino and NFC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Digital Meets Physical: Arduino 131 The Hardware Heart of NFC 133 The Arduino Development Environment 133 Serial Communication 137 Installing Arduino Libraries 138 The Arduino NDEF Library 140 Reading NDEF in Arduino 142 Writing NDEF in Arduino 145 A Microcontroller NFC Application: Hotel Key Cards 146 The Interaction and Data Format 148 The Arduino NDEF Writer Device 150 The Arduino NDEF Reader and Door Lock Device 156 A Browser Interface for the Arduino NDEF Writer Device 164 Node.js Application Specification: package.json 164 The Client-Side Code 165 The Server-Side Code 166 Conclusion 171 8. Peer-to-Peer Exchange. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Sending Peer-to-Peer Messages in PhoneGap 175 Receiving Peer-to-Peer Messages in PhoneGap 183 Table of Contents | vii Handover 185 Static Handover 186 Sending Handover Messages in PhoneGap 186 Peer-to-Peer Using Arduino 192 Card Emulation 193 Conclusion 194 9. NFC on Embedded Systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Introduction to Embedded Linux Devices and Package Managers 196 Network, USB, and NFC 196 NFC on Embedded Linux: The Overview 199 Housekeeping Details 200 Get Comfortable with Your Editing Workflow 200 Know Your Package Manager 200 Set the Date and Time 201 Make a Downloads Directory 202 BeagleBone Peculiarities 202 Raspberry Pi Peculiarities 202 Installing the Tools for NFC 203 Installing the Libusb Compatibility Library 204 Installing libnfc 204 Testing the Installation on Raspberry Pi or BeagleBone 205 Installing libfreefare 205 Libnfc and Libfreefare Command Line Tools 206 NDEF Reading and Writing in Node.js 208 Web Interface for Tag Writer 212 Tags Controlling Physical Output 214 BeagleBone Version 214 Raspberry Pi Version 217 Conclusion 221 A. NFC Specification Codes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 viii | Table of Contents
Description: