I am trying to (eventually) write a program that reads, writes, and validates nfc tags (for continued use).
I am writing these beginning stages in Kotlin.
I am thusfar running into 2 problem:
If i follow the route as described on the following page:https://developer.android.com/guide/topics/connectivity/nfc/nfc
my app will not open on my android if i include the : ACTION_NDEF_DISCOVERED intent filter. this question might be a good second question where i will post my attempt in the form of code etc.
problem/question 2:
one of the options mentioned on before posted link is aquiring a tag from a intent. now i have succesfully written the following program, that does aquire a tech list through creating a intent and a tag. But my question is this: the website also mentions the tag should contain the payload.. aka all the data on the tag.
now i am doubtfull my code and program do this, as i have read in other peoples code use of things as "Stringbuilder" or otherwise more complicated approaches to eventually display a string of the payload.
Here is the code i have written that does work (partly i suspect):
class MainActivity : AppCompatActivity() {
private var tag : Tag? = null
var data = mutableListOf<String>()
private val nfcAdapter: NfcAdapter? by lazy {
NfcAdapter.getDefaultAdapter(this)
}
private lateinit var pendingIntent: PendingIntent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val intent = Intent(this, javaClass).apply {
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
}
startActivity(intent)
pendingIntent = PendingIntent.getActivity(
this, 0, intent,
0
)
}
public override fun onPause() {
super.onPause()
nfcAdapter?.disableForegroundDispatch(this)
}
public override fun onResume() {
super.onResume()
nfcAdapter?.enableForegroundDispatch(
this, pendingIntent, null,
null
)
}
public override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
data.add(tag.toString())
Toast.makeText(this, "Dit werkt", Toast.LENGTH_SHORT).show()
val edittext = findViewById<EditText>(R.id.edittext_id) as EditText
edittext.setText(data.joinToString("----"))
}
}
Anybody know how to exaxtly do this? or am i getting the payload already (its unclear to me what the tag has on it data wise, its a old work thing i had laying around, it just specifies tech list).
greetings.
I have tried to program all 3 intent filters as mentioned on website, i have written the foreground dispatch system, and i have watched the little things like usespermission nfc, startactivity(intent) etc.
i highly suspect i am missing code to display payload, and am missing some of the finer things implementing intents.
I have looked through some other questions on here, the tech i am getting from the techlist is NfcA.
as i understand now i might have to aquire another tag, and then connect, and then write a construction for showing the payload in string format.
any links, help etc. are appreciated.
UPDATE:
this is from the android developer website, under "advanced nfc"-------
When your application receives the intent, obtain the Tag object from the intent: Kotlin Java
var tagFromIntent: Tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)////this part is succeeding
Obtain an instance of a TagTechnology, by calling one of the get factory methods of the classes in the android.nfc.tech package. You can enumerate the supported technologies of the tag by calling getTechList() before calling a get factory method. For example, to obtain an instance of MifareUltralight from a Tag, do the following: Kotlin Java
```MifareUltralight.get(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG))//this part also works, as it displays a additional '0'.
Read and write to tags
Reading and writing to an NFC tag involves obtaining the tag from the intent and opening communication with the tag. You must define your own protocol stack to read and write data to the tag. Keep in mind, however, that you can still read and write NDEF data when working directly with a tag. It is up to you how you want to structure things. The following example shows how to work with a MIFARE Ultralight tag.
Kotlin
Java
class MifareUltralightTagTester {
fun writeTag(tag: Tag, tagText: String) {
MifareUltralight.get(tag)?.use { ultralight ->
ultralight.connect()
Charset.forName("US-ASCII").also { usAscii ->
ultralight.writePage(4, "abcd".toByteArray(usAscii))
ultralight.writePage(5, "efgh".toByteArray(usAscii))
ultralight.writePage(6, "ijkl".toByteArray(usAscii))
ultralight.writePage(7, "mnop".toByteArray(usAscii))
}
}
}
fun readTag(tag: Tag): String? {///this part does not work for me.
return MifareUltralight.get(tag)?.use { mifare ->
mifare.connect().
val payload = mifare.readPages(4)
String(payload, Charset.forName("US-ASCII"))
}
}
}
**when trying to read the tag to these instructions, when i change mifare to NfcA, the "readpages"part no longer functions..
if anybody knows how to read the tag of a NfcA tag, with according syntax for processing the tag.. this would solve the problem really**
Another question:
Is it mandatory to follow all steps:
so intial tag, the n techinstance, then the read the tag part (that doesnt work for me)???