simplified NFC reading

This commit is contained in:
Faraphel 2023-11-01 14:30:48 +01:00
parent 0afd73baf6
commit 412c584237
3 changed files with 11 additions and 65 deletions

View file

@ -21,17 +21,9 @@
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity> </activity>
</application> </application>

View file

@ -5,6 +5,9 @@ import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.nfc.NfcAdapter import android.nfc.NfcAdapter
import android.nfc.Tag import android.nfc.Tag
import android.nfc.tech.IsoDep
import android.nfc.tech.NdefFormatable
import android.nfc.tech.NfcA
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@ -32,70 +35,28 @@ class MainActivity : AppCompatActivity() {
if (!(this.nfcAdapter!!.isEnabled)) { if (!(this.nfcAdapter!!.isEnabled)) {
Log.w("NFC", "NFC is not enabled") Log.w("NFC", "NFC is not enabled")
} }
// check if the application was started by an intent
if (this.intent != null) this.processIntent(intent)
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
val intent = Intent( nfcAdapter!!.enableReaderMode(
this, this,
this::class.java this::processTag,
NfcAdapter.FLAG_READER_NFC_A or NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
null
) )
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val intentFilter = arrayOf<IntentFilter>()
// enable the NFC discovery
nfcAdapter?.enableForegroundDispatch(this, pendingIntent, intentFilter, null)
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
// disable the NFC discovery // disable the NFC discovery
this.nfcAdapter!!.disableForegroundDispatch(this) this.nfcAdapter!!.disableReaderMode(this)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
// check the new intent
if (intent != null) {
this.processIntent(intent)
}
}
private fun processIntent(intent: Intent) {
Log.d("NFC", "New : ${intent.action}")
var text: TextView = this.findViewById(R.id.test_text)
text.text = intent.action
if (intent.action == NfcAdapter.ACTION_TAG_DISCOVERED) {
val tag: Tag? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(NfcAdapter.EXTRA_TAG, Tag::class.java)
} else {
intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
}
Log.d("NFC", "Tag : ${tag?.id.contentToString()}")
}
if (intent.action == NfcAdapter.ACTION_TECH_DISCOVERED) { // TODO(Faraphel): switch ?
val tag: Tag? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(NfcAdapter.EXTRA_TAG, Tag::class.java)
} else {
intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
}
Log.d("NFC", "Tag : $tag")
} }
@OptIn(ExperimentalStdlibApi::class)
fun processTag(tag: Tag) {
Log.d("NFC", "Tag ID : ${tag.id.toHexString()}")
} }
} }

View file

@ -1,7 +0,0 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
</tech-list>
</resources>