android中NFC读写功能的实现方法是怎样的
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,今天就跟大家聊聊有关android中NFC读写功能的实现方法是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。首先检查一下设备是否支持NF
千家信息网最后更新 2025年11月08日android中NFC读写功能的实现方法是怎样的
今天就跟大家聊聊有关android中NFC读写功能的实现方法是怎样的,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
首先检查一下设备是否支持NFC功能
private void checkNFCFunction() { // TODO Auto-generated method stub mNfcAdapter = NfcAdapter.getDefaultAdapter(this); // check the NFC adapter first if (mNfcAdapter == null) { // mTextView.setText("NFC apdater is not available"); Dialog dialog = null; AlertDialog.Builder customBuilder = new AlertDialog.Builder( this); customBuilder .setTitle("很遗憾") .setMessage("没发现NFC设备,请确认您的设备支持NFC功能!") .setIcon(R.drawable.ic_banner) .setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }); dialog = customBuilder.create(); dialog.setCancelable(false);// back dialog.setCanceledOnTouchOutside(false); SetDialogWidth(dialog).show(); return; } else { if (!mNfcAdapter.isEnabled()) { Dialog dialog = null; AlertDialog.Builder customBuilder = new AlertDialog.Builder( this); customBuilder .setTitle("提示") .setMessage("请确认NFC功能是否开启!") .setIcon(R.drawable.ic_banner) .setPositiveButton("现在去开启......", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Intent setnfc = new Intent( Settings.ACTION_NFC_SETTINGS); startActivity(setnfc); } }); dialog = customBuilder.create(); dialog.setCancelable(false);// back dialog.setCanceledOnTouchOutside(false); SetDialogWidth(dialog).show(); return; } } }读取NFC信息
package com.xxzhy.shujucaiji.fengxiang;import android.app.Dialog;import android.app.PendingIntent;import android.content.DialogInterface;import android.content.Intent;import android.content.IntentFilter;import android.nfc.FormatException;import android.nfc.NdefMessage;import android.nfc.NdefRecord;import android.nfc.NfcAdapter;import android.nfc.Tag;import android.nfc.tech.MifareClassic;import android.nfc.tech.Ndef;import android.nfc.tech.NfcA;import android.nfc.tech.NfcB;import android.nfc.tech.NfcF;import android.nfc.tech.NfcV;import android.os.Bundle;import android.provider.Settings;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.util.DisplayMetrics;import android.util.Log;import android.view.View;import android.view.WindowManager;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import com.xxzhy.shujucaiji.R;import com.xxzhy.shujucaiji.utils.ByteArrayChange;import com.xxzhy.shujucaiji.utils.ToStringHex;import com.xxzhy.shujucaiji.utils.ToastUtil;import com.xxzhy.shujucaiji.utils.WebServiceClient;import org.json.JSONException;import org.json.JSONObject;import java.io.IOException;import butterknife.BindView;import butterknife.ButterKnife;/** * Created by apple on 2017/10/30. */public class FengxiangThreeActivity extends AppCompatActivity { @BindView(R.id.tv_bianma) TextView tvBianma; @BindView(R.id.ll_input) LinearLayout llInput; @BindView(R.id.noteText) TextView noteText; @BindView(R.id.writeBtn) Button writeBtn; String[][] mTechLists; private Boolean ifWrite; private NfcAdapter mNfcAdapter; private PendingIntent mPendingIntent; private IntentFilter[] mFilters; byte[] code = MifareClassic.KEY_DEFAULT;//读写标签中每个块的密码 private byte[] b0; int block[] = {4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, 20, 21, 22, 24, 25, 26, 28, 29, 30, 32, 33, 34, 36, 37, 38, 40, 41, 42, 44, 45, 46, 48, 49, 50, 52, 53, 54, 56, 57, 58, 60, 61, 62}; private String samCode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fengxiang_three); ButterKnife.bind(this); writeBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ifWrite = true; } }); samCode = getIntent().getStringExtra("samCode"); tvBianma.setText(samCode); checkNFCFunction(); // NFC Check mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); // Setup an intent filter for all MIME based dispatches IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); try { ndef.addDataType("*/*"); } catch (IntentFilter.MalformedMimeTypeException e) { throw new RuntimeException("fail", e); } mFilters = new IntentFilter[]{ndef,}; // 根据标签类型设置 mTechLists = new String[][]{new String[]{NfcA.class.getName()}}; } private void checkNFCFunction() { // TODO Auto-generated method stub mNfcAdapter = NfcAdapter.getDefaultAdapter(this); // check the NFC adapter first if (mNfcAdapter == null) { // mTextView.setText("NFC apdater is not available"); Dialog dialog = null; AlertDialog.Builder customBuilder = new AlertDialog.Builder( this); customBuilder .setTitle("很遗憾") .setMessage("没发现NFC设备,请确认您的设备支持NFC功能!") .setIcon(R.drawable.ic_banner) .setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }); dialog = customBuilder.create(); dialog.setCancelable(false);// back dialog.setCanceledOnTouchOutside(false); SetDialogWidth(dialog).show(); return; } else { if (!mNfcAdapter.isEnabled()) { Dialog dialog = null; AlertDialog.Builder customBuilder = new AlertDialog.Builder( this); customBuilder .setTitle("提示") .setMessage("请确认NFC功能是否开启!") .setIcon(R.drawable.ic_banner) .setPositiveButton("现在去开启......", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Intent setnfc = new Intent( Settings.ACTION_NFC_SETTINGS); startActivity(setnfc); } }); dialog = customBuilder.create(); dialog.setCancelable(false);// back dialog.setCanceledOnTouchOutside(false); SetDialogWidth(dialog).show(); return; } } } private Dialog SetDialogWidth(Dialog dialog) { // TODO 自动生成的方法存根 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenWidth = dm.widthPixels; int screenHeight = dm.heightPixels; WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); if (screenWidth > screenHeight) { params.width = (int) (((float) screenHeight) * 0.875); } else { params.width = (int) (((float) screenWidth) * 0.875); } dialog.getWindow().setAttributes(params); return dialog; } @Override protected void onNewIntent(Intent intent) { // TODO 自动生成的方法存根 super.onNewIntent(intent);// tv1.setText("发现新的 Tag: " + ++mCount + "\n");// mCount 计数 String intentActionStr = intent.getAction();// 获取到本次启动的action if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intentActionStr)// NDEF类型 || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intentActionStr)// 其他类型 || NfcAdapter.ACTION_TAG_DISCOVERED.equals(intentActionStr)) {// 未知类型 // 在intent中读取Tag id Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); byte[] bytesId = tag.getId();// 获取id数组// info += ByteArrayChange.ByteArrayToHexString(bytesId) + "\n";// tv2.setText("标签UID: " + "\n" + info); // 读取存储信息// if (mRead.isChecked()) {// // mChange=false;// tv3.setText("读取成功! " + readTag(tag));// // readNfcVTag(tag);// etSector.setText("");// etBlock.setText("");// etData.setText("");// } // 写数据 if (ifWrite) { if (samCode.length() < 16){ for (int i = samCode.length(); i < 16; i++) { samCode+=" "; } } writeTag(tag, samCode); } // 转换为ASCll// if (mChange.isChecked()) {// tv3.setText(change(tag));// Toast.makeText(getBaseContext(), "转换成功", Toast.LENGTH_SHORT).show();// etSector.setText("");// etBlock.setText("");// etData.setText("");// } } } // 写数据 public void writeTag(Tag tag, String str) { MifareClassic mfc = MifareClassic.get(tag); try { if (mfc != null) { mfc.connect(); } else { Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show(); return; } boolean b = mfc.authenticateSectorWithKeyA(5, code); if (b) { mfc.writeBlock(4 * 5, str.getBytes()); mfc.close(); ToastUtil.showToast("写入成功"); finish(); } } catch (IOException e) { e.printStackTrace(); ToastUtil.showToast("写入失败"); } } @Override protected void onResume() { super.onResume(); enableForegroundDispatch(); } private void enableForegroundDispatch() { if (mNfcAdapter != null) { mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists); } } @Override public void onPause() { super.onPause(); disableForegroundDispatch(); } private void disableForegroundDispatch() { // TODO 自动生成的方法存根 if (mNfcAdapter != null) { mNfcAdapter.disableForegroundDispatch(this); } }}看完上述内容,你们对android中NFC读写功能的实现方法是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
功能
方法
设备
类型
支持
成功
内容
存根
标签
自动生成
生成
遗憾
信息
数据
提示
密码
数组
更多
知识
篇文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
安全接入服务器ip怎么设置
小米电视网络无法连接到服务器
网络服务器配置与管理第2版
qt 数据库保存时间
软件开发分工怎么写
我的世界pc端服务器怎么开
榆树通用网络技术咨询欢迎来电
数据库怎么清楚数据
九杞网络技术公司
检测驱动软件开发
做商城软件开发哪家便宜
数据库添加约束条件为数字字符
租服务器是按年还是按月
南沙网络安全服务有哪些
三个网络安全风险及解决方案
小卖部数据库需求分析
安卓手机数据库入门教程
怎么输入数组的数据库中
非标设计和软件开发
通信软件开发工程师
广东苹果软件开发价钱是多少
网络安全须知有哪五点
四川智慧城管软件开发
互联网科技文献综述
gta服务器广告视频
vrrp服务器是什么意思
海南网络安全技术培训线上学习
c 数据库 事务处理
热创网络技术有限公司
软件开发工具习题详解