Tap & Pay functionality with NFC Tags stopped working after migrating Android version from 11 to 12.
With following code, on tapping the NFC tag in NFC enabled area the device is vibrating but nothing is happening after that. Previously this functionality was working end to end when target SDK was 30 i.e. for Android version 11. Sharing my code below, please help me finding what is missed in the code and what can be the possible solution, thanks!
NFCBaseActivity
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
if (NfcAdapter.ACTION_NDEF_DISCOVERED == intent.action) {
val rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)
if (rawMsgs != null) {
onTagTapped(NfcUtil.getSuperTagId(intent), NfcUtil.getSuperTagData(rawMsgs))
} else{
onTagTapped(NfcUtil.getSuperTagId(intent), null)
}
}
}
calling below method from onResume()
protected fun enableNfcForegroundDispatch(environment: String) {
try {
val intentFiltersArray = NfcUtil.getIntentFilters(environment)
val intent = Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
val nfcPendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
adapter?.enableForegroundDispatch(this, nfcPendingIntent, intentFiltersArray, null)
} catch (ex: IllegalStateException) {
Timber.e(ex, "Error enabling NFC foreground dispatch")
}
}
AndroidManifest
<!--Need this feature to use NFC functionality -->
<uses-feature android:name="android.hardware.nfc" android:required="true" />
<!-- Need this permission to read nfc tags in foreground-->
<uses-permission
android:name="android.permission.NFC"
android:required="false" />
.
.
.
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
PendingIntent.FLAG_IMMUTABLE
should be PendingIntent.FLAG_MUTABLE
as the NFC service needs to be able change the Pending intent to add the NFC Tag data.
When it is returned to onNewIntent
intent.action
is null because the Intent could not be changed. See the documentation for more detail