Disclaimer: I'm pretty much new in NFC stuff. The official documentation is confusing for me. This is a test app to learn about NFC
So, I have this activity code in an Android app. I waant the activity to read an NFC tag (SDK 32). I don't have the oportunity to test it because I don't know what is a NFC_READ_COMMAND, and therefore, I cannot compile it. I am also unsure on how to properly get the data, I want to show the data in the "text_view_nfc" TextView.
I cannot compile because I don't know what NFC_READ_COMMAND is.
import com.miguel_rp.nfc_gestor_test.ActividadGestoraDeNFCsBase
import android.app.Activity
import android.nfc.NfcAdapter
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.nfc.Tag
import android.nfc.tech.NfcA
import android.widget.TextView
import kotlin.jvm.internal.Intrinsics
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.miguel_rp.nfc_gestor_test.R
class creador_de_cartas : ActividadGestoraDeNFCsBase() {
override var activity = this as Activity
var text_view_nfc: TextView? = null
private var nfcAdapter: NfcAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.setContentView(R.layout.activity_creador_de_cartas)
text_view_nfc = findViewById(R.id.textViewContenido)
this.nfcAdapter = NfcAdapter.getDefaultAdapter(this)
}
private fun enableForegroundDispatch(activity: AppCompatActivity, adapter: NfcAdapter?) {
val intent = Intent(activity.applicationContext, activity.javaClass)
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
val pendingIntent = PendingIntent.getActivity(activity.applicationContext, 0, intent, 0)
val filters = arrayOfNulls<IntentFilter>(1)
val techList = arrayOf<Array<String>>()
filters[0] = IntentFilter()
with(filters[0]) {
this?.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED)
this?.addCategory(Intent.CATEGORY_DEFAULT)
try {
this?.addDataType("text/plain")
} catch (ex: IntentFilter.MalformedMimeTypeException) {
throw RuntimeException("e")
}
}
adapter?.enableForegroundDispatch(activity, pendingIntent, filters, techList)
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
var tagFromIntent: Tag? = intent?.getParcelableExtra(NfcAdapter.EXTRA_TAG)
val nfc = NfcA.get(tagFromIntent)
val atqa: ByteArray = nfc.getAtqa()
val sak: Short = nfc.getSak()
nfc.connect()
val isConnected= nfc.isConnected()
if(isConnected)
{
val receivedData:ByteArray= nfc.transceive(NFC_READ_COMMAND)
//code to handle the received data
}else{
Log.e("ans", "Not connected")
}
}
override fun onResume() {
super.onResume()
enableForegroundDispatch(this, nfcAdapter)
}
}
In case you are wondering, "ActividadGestoraDeNFCsBase" is basically a parent class that makes sure that NFC is always activated while using the app.
package com.miguel_rp.nfc_gestor_test import android.app.Activity import android.content.Context import android.content.Intent import android.nfc.NfcAdapter import android.provider.Settings import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import java.util.* open class ActividadGestoraDeNFCsBase() : AppCompatActivity() { open lateinit var activity: Activity companion object { lateinit var nfcAdapter: NfcAdapter; var timer = Timer() fun isNFCEnabled(context: Context, actividad: Activity){ if (!nfcAdapter.isEnabled()) { actividad.runOnUiThread{ Toast.makeText(context, "Esta aplicación necesita tener el NFC activado", Toast.LENGTH_SHORT).show() } val intent = Intent(Settings.ACTION_NFC_SETTINGS) actividad.startActivity(intent) } } } override fun onResume() { super.onResume() val task = object : TimerTask() { override fun run() { isNFCEnabled(applicationContext, activity) } } timer.scheduleAtFixedRate(task, 0, 1000) } override fun onPause() { super.onPause() timer.cancel() timer = Timer() } }
Just in case, I'm using this NFC cards: Spanish Amazon. Sorry for that