In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to use Android to achieve NFC to read campus cards, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
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 android.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="; @ Override 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); writeStudentButton.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.getDefaultAdapter (this); mPendingIntent = PendingIntent.getActivity (this, 0, new Intent (this, getClass ()) .addFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP), 0) IntentFilter ndef = new IntentFilter (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.toByteArray (). 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 written 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:
The above content is how to use Android to achieve NFC to read campus cards, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.