'Read and write text to NFC Card from android

I currently trying to make Login System using NFC Card. The idea is, Android will write username to the NFC Card, then when the card is scanned on Android, the user on the card will be logged on automatically.

  1. Is this can be done? (please don't judge whether is logical or not)
  2. If it can be done, is there any in depth tutorial to do it?

The tech on my card is:

  • NfcA
  • Mifare Classic 1K
  • NdefFormatable

I already search for few days for this and can't find any solution that work.

EDIT Thank you all for you answer. The problem is resolved.

If anyone who need some code, you can download it at https://drive.google.com/file/d/1fADWUYxCxFIMqLIyeYrv7Q9HDjd99PgJ/view?usp=sharing

This rar contains java file, xml file, and txt file for a little explanation.



Solution 1:[1]

Can this be done? yes

Most tutorials and the Android documents point you to using enableForegroundDispatch but this way of using NFC has a lot of problems, it can produce unreliable reads in real user operations as it pauses your app to do the read. It also very unreliable writes in real user operations as it misdirects users with a beep before the write has completed.

It is better to use enableReaderMode https://developer.android.com/reference/android/nfc/NfcAdapter.html#enableReaderMode(android.app.Activity,%20android.nfc.NfcAdapter.ReaderCallback,%20int,%20android.os.Bundle)

But I've not found a tutorial for this, but some pointers in other answers I've given below.

An Example of enableReaderMode is at https://stackoverflow.com/a/59397667/2373819

While you probably don't want to or need to go to low level data reading shown in this answer (or low level writing) you probably want to use the higher level NDEF data format instead

For a reliable writes don't assume that the card is in range still when you try to write to it and then handle write errors nicely. The best method I found was to store the data to write until you App is notified that a tag has been detected and then immediately write to the card. Logic for this shown in https://stackoverflow.com/a/59423011/2373819

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew