Book Image

Near Field Communication with Android Cookbook

By : Subtil
Book Image

Near Field Communication with Android Cookbook

By: Subtil

Overview of this book

An easy-to-follow guide, full of hands-on examples of and real-world applications. Each recipe is explained and placed in context. If you want to learn how to create NFC-enabled Android applications, this is the book for you. Perhaps you already know a bit about Android application developments but have never used NFC, or perhaps you know a little about NFC android development but want some more advanced features and examples. In either case, this book will get you up and running quickly. You are expected to have Android programming knowledge.
Table of Contents (12 chapters)
11
Index

Getting the data out of the message


In the previous recipe, we learn how to get NdefMessage from the tag, but the actual data is in NdefRecords present in the tag. In this recipe, we will see how to get the first NdefRecord of NdefMessage and how to get its content.

How to do it…

The following steps show how to get the data out of the message:

  1. Open Eclipse and create a new Android application project named NfcBookCh5Example2 and package name nfcbook.ch5.example2.

  2. Make sure the AndroidManifest.xml file is correctly configured. Refer to the Requesting NFC permissions recipe in Chapter 1, Getting Started with NFC.

  3. Import the NfcHelper class into the project and enable the foreground dispatch system.

  4. Add the following class member and instantiate it in the onCreate method:

    private NfcHelper nfcHelper;
    
    protected void onCreate(Bundle savedInstanceState) { 
      nfcHelper = new NfcHelper(this); 
      ...
    }
  5. Implement the getFirstNdefRecord method in the NfcHelper class using the following code:

    public NdefRecord...