Android: NFC reader card
Main Program:
Package com. nfclab. stuCard; import java. io. IOException; import android. app. activity; import android. app. pendingIntent; import android. content. intent; import android. content. intentFilter; import android. nfc. ndefMessage; import android. nfc. ndefRecord; import android. nfc. nfcAdapter; import android. nfc. tag; import android. nfc. tech. ndef; import android. nfc. tech. ndefFormatable; import android. OS. bundle; import androi D. util. log; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. textView; import android. widget. toast; public class stuCardActivity extends Activity {private NfcAdapter mNfcAdapter; private PendingIntent mPendingIntent; private IntentFilter [] mFilters; private String [] [] mTechLists; private String studentId = ""; private String studentName = ""; @ O Verride public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); final EditText studentIdEditText = (EditText) this. findViewById (R. id. studentIdEditText); final EditText studentNameEditText = (EditText) this. findViewById (R. id. studentNameEditText); Button writeStudentButton = (Button) this. findViewById (R. id. writeStudentButton); writeStudentBut Ton. setOnClickListener (new android. view. view. onClickListener () {public void onClick (View view) {studentId = studentIdEditText. getText (). toString (); studentName = studentNameEditText. getText (). toString (); TextView messageText = (TextView) findViewById (R. id. messageText); messageText. setText ("Touch NFC Tag to write \ n"); messageText. append ("Student id:" + studentId + "\ nStudent Name:" + studentName) ;}}); Button exitButton = (Button) findViewById (R. id. exitButton); exitButton. setOnClickListener (new android. view. view. onClickListener () {public void onClick (View v) {finish () ;}}); mNfcAdapter = NfcAdapter. getdefaadapter adapter (this); mPendingIntent = PendingIntent. getActivity (this, 0, new Intent (this, getClass ()). addFlags (Intent. FLAG_ACTIVITY_SINGLE_TOP), 0); IntentFilter ndef = new IntentFilte R (NfcAdapter. ACTION_NDEF_DISCOVERED); // ndef. addDataScheme ("http"); mFilters = new IntentFilter [] {ndef,}; mTechLists = new String [] [] {new String [] {Ndef. class. getName ()}, new String [] {NdefFormatable. class. getName () };}@ Override public void onResume () {super. onResume (); if (mNfcAdapter! = Null) mNfcAdapter. enableForegroundDispatch (this, mPendingIntent, mFilters, mTechLists);} @ Override public void onNewIntent (Intent intent) {Log. I ("Foreground dispatch", "Discovered tag with intent:" + intent); Tag tag = intent. getParcelableExtra (NfcAdapter. EXTRA_TAG); String externalType = "nfclab.com: transport"; String payload = studentId + ":" + studentName; NdefRecord extRecord1 = new NdefRecord (NdefRecord. TNF_EXTERNAL_TYPE, externalType. getBytes (), new byte [0], payload. getBytes (); NdefMessage newMessage = new NdefMessage (new NdefRecord [] {extRecord1}); writeNdefMessageToTag (newMessage, tag) ;}@ Override public void onPause () {super. onPause (); mNfcAdapter. disableForegroundDispatch (this);} boolean writeNdefMessageToTag (NdefMessage message, Tag detectedTag) {int size = message. toByteA Rray (). length; try {Ndef ndef = Ndef. get (detectedTag); if (ndef! = Null) {ndef. connect (); if (! Ndef. isWritable () {Toast. makeText (this, "Tag is read-only. ", Toast. LENGTH_SHORT ). show (); return false;} if (ndef. getMaxSize () <size) {Toast. makeText (this, "The data cannot written to tag, Tag capacity is" + ndef. getMaxSize () + "bytes, message is" + size + "bytes. ", Toast. LENGTH_SHORT ). show (); return false;} ndef. writeNdefMessage (message); ndef. close (); Toast. makeText (this, "Message is wri Tten tag. ", Toast. LENGTH_SHORT). show (); return true;} else {NdefFormatable ndefFormat = NdefFormatable. get (detectedTag); if (ndefFormat! = Null) {try {ndefFormat. connect (); ndefFormat. format (message); ndefFormat. close (); Toast. makeText (this, "The data is written to the tag", Toast. LENGTH_SHORT ). show (); return true;} catch (IOException e) {Toast. makeText (this, "Failed to format tag", Toast. LENGTH_SHORT ). show (); return false ;}} else {Toast. makeText (this, "NDEF is not supported", Toast. LENGTH_SHORT ). show (); return false ;}} catch (Exception e) {Toast. makeText (this, "Write opreation is failed", Toast. LENGTH_SHORT ). show () ;}return false ;}}
Layout file:
Configuration file: