Book Image

Near Field Communication with Android Cookbook

By : Vitor Subtil
Book Image

Near Field Communication with Android Cookbook

By: Vitor Subtil

Overview of this book

Table of Contents (17 chapters)
Near Field Communication with Android Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Filtering tags by their content type programmatically


NFC tags can hold several types of data commonly used in several applications. In our applications, we may only want to handle tags in which the content is recognized. That way, we avoid unexpected exceptions and messing up with other developers' applications.

How to do it...

By performing the following steps, we will programmatically define the tag-content filter in which the application gets executed:

  1. Open Eclipse and create a new Android application project named NfcBookCh2Example2 with the package name nfcbook.ch2.example2.

  2. Open the MainActivity.java file located under nfcbook.ch2.example2 and add the following class member:

    private NfcAdapter nfcAdapter;.
  3. Instantiate the nfcAdapter class field in the onCreate method:

    protected void onCreate(Bundle savedInstanceState) { 
    ... 
    nfcAdapter = NfcAdapter.getDefaultAdapter(this); 
    }
  4. Implement the following method and invoke it in the onResume method, as shown in the following code:

    void enableForegroundDispatch...