如何使用Android实现NFC读取校园卡
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,本篇文章为大家展示了如何使用Android实现NFC读取校园卡,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。主程序:package com.nfclab.st
千家信息网最后更新 2026年10月31日如何使用Android实现NFC读取校园卡
本篇文章为大家展示了如何使用Android实现NFC读取校园卡,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
主程序:
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; }}布局文件:
配置文件:
上述内容就是如何使用Android实现NFC读取校园卡,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
校园
内容
技能
文件
知识
简明
简明扼要
主程序
就是
布局
文章
更多
篇文章
行业
资讯
资讯频道
频道
一亮
储备
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中国 科技 互联网
西安互联网科技
汇久创客互联网科技
伴你行互联网科技有限公司
天下捷融互联网科技
上海电发互联网科技有限公司
安控科技rtu产品工业互联网
新华互联网科技2019
互联网金融科技公司干啥的
栖莱科技互联网股份有限公司
上海宁丞科技互联网
昊天互联网科技有限公司
哈尔滨屹滨互联网科技有限公司
迪庆互联网科技招生
中科明德北京互联网科技
广州富喜互联网络科技
互联网风格科技公司名称
云南互联网科技学费
互联网科技对化妆品行业
优雇互联网科技有限公司
保险科技 互联网保险
深圳齐家互联网科技
互联网女皇 腾讯科技
互联网科技有限公司 翻译
中正互联网科技招聘
中国科技互联网公司收入
互联网大会黑科技排名
消费医药和互联网科技
电子科技大学互联网创业大赛
中国科技和美国互联网