当前位置: 首页 > 工具软件 > nfc-talk > 使用案例 >

安卓通讯开发——NFC

西门鹏程
2023-12-01

接上安卓通讯开发——蓝牙

https://blog.csdn.net/nishigesb123/article/details/90401981


NFC(Near field communication)

概述

翻译过来即“近场通讯”

https://developer.android.google.cn/guide/topics/connectivity/nfc

Near Field Communication (NFC) is a set of short-range wireless technologies, typically requiring a distance of 4cm or less to initiate a connection. NFC allows you to share small payloads of data between an NFC tag and an Android-powered device, or between two Android-powered devices.

Tags can range in complexity. Simple tags offer just read and write semantics, sometimes with one-time-programmable areas to make the card read-only. More complex tags offer math operations, and have cryptographic hardware to authenticate access to a sector. The most sophisticated tags contain operating environments, allowing complex interactions with code executing on the tag. The data stored in the tag can also be written in a variety of formats, but many of the Android framework APIs are based around a NFC Forum standard called NDEF (NFC Data Exchange Format).

Android-powered devices with NFC simultaneously support three main modes of operation:

  1. Reader/writer mode, allowing the NFC device to read and/or write passive NFC tags and stickers.
  2. P2P mode, allowing the NFC device to exchange data with other NFC peers; this operation mode is used by Android Beam.
  3. Card emulation mode, allowing the NFC device itself to act as an NFC card. The emulated NFC card can then be accessed by an external NFC reader, such as an NFC point-of-sale terminal.

NFC Basics

This document describes how Android handles discovered NFC tags and how it notifies applications of data that is relevant to the application. It also goes over how to work with the NDEF data in your applications and gives an overview of the framework APIs that support the basic NFC feature set of Android.

Advanced NFC

This document goes over the APIs that enable use of the various tag technologies that Android supports. When you are not working with NDEF data, or when you are working with NDEF data that Android cannot fully understand, you have to manually read or write to the tag in raw bytes using your own protocol stack. In these cases, Android provides support to detect certain tag technologies and to open communication with the tag using your own protocol stack.

Host-based Card Emulation

This document describes how Android devices can perform as NFC cards without using a secure element, allowing any Android application to emulate a card and talk directly to the NFC reader.

近场通信(NFC)是一套短距离无线技术,通常需要4厘米或更短的距离才能启动连接。NFC允许您在nfc标记和android驱动设备之间或在两个android驱动设备之间共享小的有效数据。

标签的复杂性可以很大。简单标签提供的只是读和写语义,有时与一次性可编程的区域,使卡只读。更复杂的标记提供数学操作,并具有加密硬件来验证对扇区的访问。最复杂的标记包含操作环境,允许与在标记上执行的代码进行复杂交互。存储在标记中的数据也可以用多种格式编写,但是许多Android框架api都是基于NFC论坛名为NDEF(NFC数据交换格式)的标准。

带有NFC的Android驱动设备同时支持三种主要的操作模式:

  1. 读写器模式,允许NFC设备读取和/或写入被动的NFC标记和贴纸。
  2. P2P模式,允许NFC设备与其他NFC对等点交换数据;这种操作模式由AndroidBeam使用。
  3. 卡片仿真模式,允许NFC设备本身充当NFC卡。然后,模拟的NFC卡可以被外部NFC读取器访问,例如NFC销售点终端。

NFC基础

本文档描述Android如何处理已发现的NFC标记,以及如何将与应用程序相关的数据通知应用程序。它还介绍了如何在应用程序中处理NDEF数据,并概述了支持Android基本NFC功能集的框架API。

高级试件

本文档介绍了支持使用Android支持的各种标记技术的API。当您不使用NDEF数据时,或者在使用Android无法完全理解的NDEF数据时,您必须使用自己的协议栈以原始字节手动读取或写入标记。在这些情况下,Android支持检测特定的标记技术,并使用自己的协议栈打开与标记的通信。

基于主机的卡仿真

本文档描述了Android设备如何在不使用安全元素的情况下作为NFC卡执行任务,允许任何Android应用程序模拟一张卡并直接与NFC读取器对话。


NFC的目标并非是取代蓝牙等其他无线技术,而是在不同的场合、不同的领域起到相互补充的作用。具体对比如下表:

 NFC蓝牙红外
网络类型点对点单点对多点点对点
使用距离≤0.1m≤10m≤1m
速度106, 212, 424 kbps
规划速率可达868 kbps
721 kbps 115kbps
  
2.1 Mbps~1.0 Mbps
  
建立时间< 0.1s6s0.5s
安全性具备, 硬件实现具备,软件实现不具备, 使用IRFM 时除外
通信模式主动-主动/被动主动-主动主动-主动
成本

 


检查设备

(省略一句private NfcAdapter nfcAdapter)
//检查设备是否支持NFC功能
nfcAdapter=NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter==null){
    Toast.makeText(this, "设备不支持NFC功能", Toast.LENGTH_SHORT).show();
    //finish();
}
//判断设备是否打开NFC功能
if (!nfcAdapter.isEnabled()){
    Intent intent=new Intent(Settings.ACTION_NFC_SETTINGS);
    startActivity(intent);
}

 

emm,由于设备不支持,这篇文章暂时不更新

有需要可以自行参考API文档

https://developer.android.google.cn/guide/topics/connectivity/nfc/nfc

 类似资料: