gooeyDuck
gooeyDuck

Reputation: 1

NFC tag intent filtering

My implementation goal: Nt3h2211 tag (configured over I2C from a PIC-mcu) then read by my Android App.

How to launch app by scanning NDEF NFC tag? This thread seems very close to the problem I am experiencing. Editing the manifest intent filters per those suggestions did not resolve my issue. Any assistance is greatly appreciated.

I made the hardware implementation and wrote the PIC firmware; so, I have access to change the tag configuration if needed. Tag is formatted as NDEF and the payload is only bit fields… at this point only 16 bytes are populated. See pic of tag memory state as read from manufactures NFC Tag reading utility.

Tag configuration

Manifest file nfc intent filters:

    <intent-filter>
    <action android:name=”android.nfc.action.NDEF.DISCOVERED”/>
    <category android:name=”android.intent.category.DEFAULT”/>
    <data android:scheme=”vnd.android.nfc”
              android:host=”ext”
              android:pathPrefix=”/blackfeathersystems.com:pr”/>
</intent-filter>
<intent-filter>
    <action android:name=”android.nfc.action.TECH.DISCOVERED”/>
    <category android:name=”android.intent.category.DEFAULT”/>
    <data android:scheme=”vnd.android.nfc”
              android:host=”ext”
              android:pathPrefix=”/blackfeathersystems.com:pr”/>
</intent-filter>
<meta-data android:name=”android.nfc.action.TECH_DISCOVERED”
                   android:resource=”@xml/nfc_tech_list” />

The nfc_tech_list xml file content:

<?xml version=”1.0” endoding=”utf-8”?>
<resources xmlns:xliff=”urn:oasis:names:tc:xliff:document:1.2”>
    <tech-list>
    <tech> android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
    <tech> android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
    <tech> android.nfc.tech.MifareClassic</tech>
    </tech-list>
    <tech-list>
    <tech> android.nfc.tech.MifareUltralight</tech>
    </tech-list>
</resources>

The main activity code. Please note the onNewIntent() function has been stubbed out with Toast’s for brevity – but it only ever resolves to “action: NOT DISCOVERED”

package com.example.nfc2211comms2

class MainActivity : AppCompatActivity() {

lateinit var nfcAdapter : NfcAdapter
lateinit var pendingIntent: PendingIntent
lateinit var nfcA: NfcA

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_fragment)

    nfcAdapter = NfcAdapter.getDefaultAdapter(this)
    
} // end onCreate

    override fun onResume() {
        Val intent = Intent(this, javaClass).apply {
        addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
}

pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null)

onNewIntent(intent)

} // end onResume


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

    if (NfcAdapter.ACTION_NDEF_DISCOVERED == nintent.action) {
Toast.makeText(this, “ action: NDEF ”, LENGTH_SHORT).show()
} else if (NfcAdapter.ACTION_TECH_DISCOVERED == nintent.action) {
Toast.makeText(this, “ action: TECH ”, LENGTH_SHORT).show()
} else {
Toast.makeText(this, “ Not Discovered ”, LENGTH_SHORT).show()
}
} // end onNewIntent

override fun onPause() {
    super.onPause()
    nfcA.close()
    nfcAdapter.disableForegroundDispatch(this)
}

override fun onDestroy() {
    super.onDestroy()
    finish()
}

} // end mainActivity

Upvotes: 0

Views: 439

Answers (1)

gooeyDuck
gooeyDuck

Reputation: 1

I think I may have, at least partially, answered my own question. After rewriting the code to be less verbose I found out NDEF formatted tags need one of three first records:

  1. SMART POSTER, 2)WELL KNOWN MIME or 3) URI

I have EXTERNAL, so the NDEF_DISCOVERED filter will never get hit. Consequently the TECH_DISCOVERED intent does work.

Thanks!

Upvotes: 0

Related Questions