参考网址:https://blog.csdn.net/sky2016_w/article/details/79026596

https://www.jianshu.com/p/61f90708bb02

1、首先在manifests里面声明NFC和添加相应的权限;
<uses-feature  android:name="android.hardware.nfc"  android:required="true" />
<uses-permission android:name="android.permission.NFC" />2、在Activity标签中声明识别NFC标签;
<activity android:name=".Activity.Main.NFCActivity">  <intent-filter>  <action android:name="android.nfc.action.TAG_DISCOVERED" />  <category android:name="android.intent.category.DEFAULT" />  <data android:mimeType="*/*" />  </intent-filter>
</activity>
3、封装NFC的读写,方便调用
public class NfcUtils {  //nfc  public static NfcAdapter mNfcAdapter;  public static IntentFilter[] mIntentFilter = null;  public static PendingIntent mPendingIntent = null;  public static String[][] mTechList = null;  /** * 构造函数,用于初始化nfc*/  public NfcUtils(Activity activity) {  mNfcAdapter = NfcCheck(activity);  NfcInit(activity);  }  /** * 检查NFC是否打开 */  public static NfcAdapter NfcCheck(Activity activity) {  NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);  if (mNfcAdapter == null) {  return null;  } else {  if (!mNfcAdapter.isEnabled()) {  Intent setNfc = new Intent(Settings.ACTION_NFC_SETTINGS);  activity.startActivity(setNfc);  }  }  return mNfcAdapter;  }  /** * 初始化nfc设置 */  public static void NfcInit(Activity activity) {  mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);  IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);  IntentFilter filter2 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);  try {  filter.addDataType("*/*");  } catch (IntentFilter.MalformedMimeTypeException e) {  e.printStackTrace();  }  mIntentFilter = new IntentFilter[]{filter, filter2};  mTechList = null;  }  /**  * 读取NFC的数据  */  public static String readNFCFromTag(Intent intent) throws UnsupportedEncodingException {  Parcelable[] rawArray = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);  if (rawArray != null) {  NdefMessage mNdefMsg = (NdefMessage) rawArray[0];  NdefRecord mNdefRecord = mNdefMsg.getRecords()[0];  if (mNdefRecord != null) {  String readResult = new String(mNdefRecord.getPayload(), "UTF-8");  return readResult;  }  }  return "";  }  /** * 往nfc写入数据 */  public static void writeNFCToTag(String data, Intent intent) throws IOException, FormatException {  Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  Ndef ndef = Ndef.get(tag);  ndef.connect();  NdefRecord ndefRecord = NdefRecord.createTextRecord(null, data);  NdefRecord[] records = {ndefRecord};  NdefMessage ndefMessage = new NdefMessage(records);  ndef.writeNdefMessage(ndefMessage);  }  /** * 读取nfcID */  public static String readNFCId(Intent intent) throws UnsupportedEncodingException {  Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  String id = ByteArrayToHexString(tag.getId());  return id;  }  /** * 将字节数组转换为字符串 */  private static String ByteArrayToHexString(byte[] inarray) {  int i, j, in;  String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};  String out = "";  for (j = 0; j < inarray.length; ++j) {  in = (int) inarray[j] & 0xff;  i = (in >> 4) & 0x0f;  out += hex[i];  i = in & 0x0f;  out += hex[i];  }  return out;  }
}  
4、在NFCActivity代码中的使用、使用标签的前台调度系统
@Override
public void initData() {  //nfc初始化设置  NfcUtils nfcUtils = new NfcUtils(this);
} @Override
protected void onResume() {  super.onResume();  //开启前台调度系统  NfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList);
}
@Override
protected void onPause() {  super.onPause();  //关闭前台调度系统  NfcUtils.mNfcAdapter.disableForegroundDispatch(this);
}@Override
protected void onNewIntent(Intent intent) {  super.onNewIntent(intent);  //当该Activity接收到NFC标签时,运行该方法  //调用工具方法,读取NFC数据  String str = NfcUtils.rendFromTag(intent);
}5、判断手机是否支持NFC
PackageManager packageManager = this.getPackageManager();
boolean b1 = packageManager.hasSystemFeature(PackageManager.FEATURE_NFC);
Toast.makeText(context, "是否支持nfc:" + b1, 1).show();

Android中NFC读写相关推荐

  1. android中NFC读写功能的实现方法

    这篇文章主要为大家详细介绍了android中NFC读写功能的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了android中NFC读写功能的 ...

  2. Android之NFC读写操作

    上次记录NFC知识时,还处在研究状态,现在项目的第一阶段开发已经完成.上篇Android之NFC开发,简单介绍了一些知识,也是对未知信息的研究,总要了解一点来龙去脉,省的心发慌.这篇文章总结自己的项目 ...

  3. Android开发NFC读写数据

    1.权限<uses-permission android:name="android.permission.NFC"/><uses-feature android ...

  4. Android实现NFC读写

    一.NFC是什么? 近距离无线通讯技术,这个技术由非接触式射频识别(RFID)演变而来,由飞利浦半导体(现恩智浦半导体公司).诺基亚和索尼共同研制开发,其基础是RFID及互连技术.近场通信(Near ...

  5. Android中NFC编程

    Android NFC的相关资源,需求和设置 你可以在Android的NFC支持页面找到相关的API文档和NFC的示例代码: http://developer.android.com/referenc ...

  6. Android中NFC的使用

    NFC开发也是我们开发中会遇到的,所以我们也有必要了解一下. 1.权限配置 <!-- NFC --> <uses-permission android:name="andr ...

  7. android开发板功能,Android中NFC模块PN532开发板应用/原理图/PCB等全部资料

    PN532是NXP最近推出的一款NFC芯片,支持读卡器模式和卡模式(需要卡芯片Smart MX),支持TypeA丶TypeB丶TypeC三个标准.该NFC模块PN532开发板特点: @ 内部带一个MC ...

  8. Android基于nfc的读写(一)

    这里写自定义目录标题 Android的NFC读写(一) NFC简介 代码解析 源码(Demo) Android的NFC读写(一) 近来,因需求需要开发nfc读写功能,网上查阅了许多资料,发现相关方面资 ...

  9. Android中文件的读写

    首先得获取文件读写的权限可以在AndroidManifest中进行获取文件读写实验 <uses-permission android:name="android.permission. ...

最新文章

  1. Libgdx学习笔记:Simple text input
  2. 人工智能可能成为我们检测COVID-19最有效的方法吗?
  3. 也说 ASP.NET MVC的 Script 管理
  4. MIMO系统ML检测(最大似然检测)
  5. Android开发笔记之:Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
  6. Android Wifi开发之WifiConfiguration
  7. 【三维深度学习】基于片元的渐进式三维点云上采样模型
  8. 其他测试用例设计方法-错误推测法与正交实验法
  9. lucene bug的报告经历
  10. 态调用Excel避免因为版本不同而使用程序无法编辑或调试
  11. Egret入门学习日记 --- 第十六篇(书中 6.10~7.3节 内容)
  12. 全球与中国心脏临床信息系统(CIS)市场深度研究分析报告
  13. 按键精灵 手机 oracle,按键精灵Android版:软件使用
  14. 借助Hugo和Academic主题在github.io建立个人网站
  15. 通过函数seaborn.cubehelix_palette生成顺序调色板
  16. cannot find -l****问题的解决办法
  17. mac下chrome插件安装位置
  18. Azure学习笔记2.——六种虚拟网络连接
  19. 时隔一年才发现嵌入式到底指的是什么
  20. 神秘美丽的陨石:蜂窝黄金发光晶体

热门文章

  1. 计算机教师幽默介绍,数学老师幽默自我介绍关于数字
  2. 参与360私有化的公司股价暴涨 多家借壳公司却遭遇政策难题
  3. 系统学习车载仿真测试HiL,成为HiL测试工程师
  4. You may need to add 'xxxx.cn' to ALLOWED_HOSTS.
  5. C/C++ 和 Java 命令行绘制心形图案
  6. Centos 7 一键安装Redash (Centos7 + Docker)
  7. 海康摄像头视频调用出错,Jni Error(app bug): accessed stale local reference解决办法
  8. Netty 多线程模型
  9. 《转》mac 蓝牙不可用解决
  10. 去除List集合中的重复对象