0

I have the following code

val pendingIntent = PendingIntent.getActivity(activity, 101,
Intent(activity,classType).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), PendingIntent.FLAG_IMMUTABLE)
val nfcIntentFilter = IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED)
val filters = arrayOf(nfcIntentFilter)
val TechLists = arrayOf(arrayOf(Ndef::class.java.name), 
arrayOf(NdefFormatable::class.java.name))
nfcAdapter.enableForegroundDispatch(activity, pendingIntent, filters, TechLists)``

and in the activity I am trying to get the tag , action , message

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)


val type: String? = intent.type
val action: String? = intent.action
 }

However, action is already and tag is also null:

val tag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
it.getParcelableExtra(NfcAdapter.EXTRA_TAG,Tag::class.java)
} else {
it.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
}

In the manifest I have the following inside the activity tag:

 <intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>

I also have the permission enabled:

<uses-permission android:name="android.permission.NFC" />

Tried multiple codes and change the intent filter NDEF_DISCOVERED to TAG_DISCOVERD but everything failed. I have tested it also on three separate phones and also same issue. onNewIntent gets called but the intent does not provide information

Miriana Itani
  • 865
  • 9
  • 25
  • 1
    Especially if you want to reliably write to a Tag then I would not use `enableForegroundDispatch` but use the better `enableReaderMode` API (Java Example of `enableReaderMode` https://stackoverflow.com/a/64921434/2373819 ) – Andrew Jan 31 '23 at 19:46
  • Thank you so much it worked. Please add your comment as an answer to accept it – Miriana Itani Jan 31 '23 at 20:28
  • Really pointing you to another answer to a similar question does not really justify a separate answer, it's more of a "close" because it's a duplicate, but if like the answer I linked just up vote that answer. – Andrew Jan 31 '23 at 21:46
  • If you say so thanks anyways – Miriana Itani Feb 02 '23 at 17:11

0 Answers0