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:
Open Eclipse and create a new Android application project named
NfcBookCh2Example2
with the package namenfcbook.ch2.example2
.Open the
MainActivity.java
file located undernfcbook.ch2.example2
and add the following class member:private NfcAdapter nfcAdapter;.
Instantiate the
nfcAdapter
class field in theonCreate
method:protected void onCreate(Bundle savedInstanceState) { ... nfcAdapter = NfcAdapter.getDefaultAdapter(this); }
Implement the following method and invoke it in the
onResume
method, as shown in the following code:void enableForegroundDispatch...