I found two approaches to let an Android app detect and process NFC tags:
NfcAdapter.enableReaderMode(activity, callback, flags, extras)
and then receive the tag info in the callback.
NfcAdapter.enableForegroundDispatch(activity, intent, filters, techLists)
and then receive the tag info in the onNewIntent(intent)
activity method.
I currently use the second approach, however, I recently discovered the enableReaderMode
approach and wonder if it's better to use that to process NFC tags.
So what is the difference between enableReaderMode
and enableForegroundDispatch
?
Near Field Communication (NFC) is a set of short-range wireless technologies, typically requiring a distance of 4cm or less to initiate a connection. NFC allows you to share small payloads of data between an NFC tag and an Android-powered device, or between two Android-powered devices. Tags can range in complexity.
To get access to the NFC hardware, you have to apply for permission in the manifest. If the app won't work without NFC, you can specify the condition with the uses-feature tag. If NFC is required, the app can't be installed on devices without it and Google Play will only display your app to users who own a NFC device.
If you have a Samsung Android phone, check under settings > connections > tap NFC and contactless payments > tap the switch to turn NFC on. Once this is turned on for your device, you can adjust your settings for contactless payments and select your preferred mobile payment service, such as Google Pay or Samsung Pay.
NFC readers are the active components in NFC transactions. They can read and write cards and tags, interact with NFC phones and enable communication from device to device. NXP has a broad portfolio of high-performance NFC readers, fully supporting the MIFARE® ICs and standards such as EMV.
The foreground dispatch system (NfcAdapter.enableForegroundDispatch()
) exists since Android 2.3.3 (which is basically the beginning of Android NFC). Hence, this method is supported on all Android devices with NFC capabilities.
The foreground dispatch system is used to give an activity that is currently in the foreground precedence in handling NFC discovery events (i.e. discovered NFC tags and NDEF messages received from peer-to-peer devices). This means that even if another app is registered (by means of an intent filter in the AndroidManifest.xml
) for a specific tag type or NDEF data, the NFC event will still be passed to the foreground activity instead of that other activity. Consequently, the method does not change the way Android listens for NFC devices (NFC tags, P2P devices), it only changes the priority for handling discovered devices.
The reader-mode API (NfcAdapter.enableReaderMode()
) was introduced in Android 4.4. Consequently, not all Android devices with NFC capabilities support this method.
As opposed to the foreground dispatch system, the reader-mode API does change the way Android listens for NFC devices. The reader-mode API disables peer-to-peer mode. This, for instance, permits you to discover card emulation mode of other devices that have peer-to-peer mode and card emulation mode enabled simultaneously (as is the case with Android HCE). (Normally, such a device would be discovered as a peer-to-peer device and an Android app would not be able to access the card emulation functionality.)
Moreover, you can change specific parameters of the NFC reader mode, e.g. you can:
According to a comment by Adam Johns, the above may no longer be true on Android 10 (he tested on a Pixel 2). When using only enableReaderMode()
(without an additional enableForegroundDispatch()
), the devices seems to show a toast "No supported application for this NFC tag" eventhough tags are correctly dispatched to the registered reader-mode callback method (onTagDiscovered()
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With