Chapter 5. Listening for NDEF Messages
Every well-designed application relies on good listening for relevant events, whether they’re user input events, network events, or some other form. NFC applications are no different. NFC applications are always listening for NDEF messages, parsing and filtering the message type and contents, and taking action accordingly. In Chapter 4, you got an introduction on how to write NDEF messages using the PhoneGap-NFC plug-in, but we didn’t dive deeply into the listening part.
There are two main ways to listen for NDEF messages: you can program your application to listen for them when it’s the app in the foreground, or you can let the operating system of your device do the listening, and call your application when it sees a message that’s relevant to your app. The PhoneGap-NFC plug-in offers several NFC events that you can program your app to listen for, and Android offers a tag dispatch system that allows you to tell the operating system which tags you’re most interested in. NFC apps can take advantage of the tag dispatch system by writing intent filters in the Android Manifest that tell the operating system which tags to route to the app. They can also explicitly listen for some or all tags, overriding the intent filters of other apps using the foreground dispatch system. In this chapter, you’ll learn a bit more about how to listen for and filter NDEF messages using PhoneGap-NFC’s event listeners and Android’s tag dispatch system.