Android NFC基础知识汇总

 Near Field Communication,近场通信,简称NFC。现在大多手机都已经支持NFC功能,可用于交通卡充值刷卡、门禁以及大多数据交换和获取的场景。

百度百科中提到NFC是在非接触式射频识别(RFID)技术的基础上,结合无线互联技术研发而成,为我们的日常生活越来越普及的各种电子产品提供了一种十分安全快捷的通信方式。它的中文名称中的“近场”是指临近电磁场的无线电波。实现了电子支付、身份认证、票务、数据交换、防伪、广告等多种功能。建立了一种新型的用户消费和业务模式。

它分为点对点通信、读写器和NFC卡模拟三种工作模式。

点对点:两个NFC设备可以交换数据。

读卡器:NFC设备作为非接触读写器使用

卡模拟:将具有NFC功能的设备模拟成一张标签或者非接触卡。

常见的应用场景:读卡、写卡和分享内容。有三种数据过滤器,分别为:ACTION_NDEF_DISCOVERED,ACTION_TECH_DISCOVERED,ACTION_TAG_DISCOVERED。

ACTION_ NDEF_ DISCOVERED:当系统检测到tag中含有NDEF格式的数据时,且系统中有activity声明可以接受包含NDEF数据的Intent的时候,系统会优先发出这个action的intent。

ACTION_ TECH_ DISCOVERED:当没有任何一个activity声明自己可以响应ACTION_NDEF_DISCOVERED时,系统会尝试发出TECH的intent.即便你的tag中所包含的数据是NDEF的,但是如果这个数据的MIME type或URI不能和任何一个activity所声明的想吻合,系统也一样会尝试发出tech格式的intent,而不是NDEF.

ACTION_TAG_DISCOVERED:当系统发现前两个intent在系统中无人会接受的时候,就只好发这个默认的TAG类型。

NFC常见的数据格式(TechList):

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
    <tech>android.nfc.tech.IsoDep</tech>
    </tech-list>
    <tech-list>
    <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
    <tech>android.nfc.tech.NfcB</tech>
    </tech-list>
    <tech-list>
    <tech>android.nfc.tech.NfcF</tech>
    </tech-list>
    <tech-list>
    <tech>android.nfc.tech.NfcV</tech>
    </tech-list>
    <tech-list>
    <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
    <tech>android.nfc.tech.NdefFormatable</tech>
    </tech-list>
    <tech-list>
    <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
    <tech-list>
    <tech>android.nfc.tech.MifareClassic</tech>
    </tech-list>
</resources>

在res下新建xml,然后新建一个xxx.xml的文件将上方内容copy即可。然后在AndroidManifest.xml中,添加NFC权限:

<uses-permission android:name="android.permission.NFC" />

后将要做NFC功能的界面的launchMode设置为singleTop类型,然后添加meta-data内容,如下:

<meta-data 
                android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter"/>

然后在activity中oncreate和onresume中初始化NFC。

  private void initNfc() {
        defaultAdapter = NfcAdapter.getDefaultAdapter(this);
        if (null == defaultAdapter) {
            Toast.makeText(this, "当前设备不支持NFC功能", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            if (!defaultAdapter.isEnabled()) {
                Toast.makeText(this, "请打开NFC功能", Toast.LENGTH_SHORT).show();
                return;
            }
        } catch (Exception e) {
            return;
        }
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);

    }

然后重写onNewInstance方法中通过

判断当前具备NFC功能的标签属于那种格式协议的。ndef格式举例:

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
                Log.e("===========", "processIntent:2 ");
                Tag tag = intent.getParcelableExtra(defaultAdapter.EXTRA_TAG);
                if (tag == null)
                    return;
                String[] techList = tag.getTechList();
                byte[] id = tag.getId();
                tagIdStr = "ID:" + Utils.bytesToHexString(id) + "\n";
                tagIdStr += "type:" + tag.describeContents() + "\n";
                boolean is15693 = false;
                for (String tecj : techList) {
                    tagIdStr += tecj + "\n";
                    if (tecj.equals("android.nfc.tech.NfcV")) {
                        is15693 = true;
                    }
                }
        
            }
public class Utils {
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    public static byte[] stringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static byte[] hexString2Bytes(String src) {
        if (null == src || 0 == src.length()) {
            return null;
        }
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < (tmp.length / 2); i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }

}

这样基本上就可以了实现数据读取了。本文只针对于富立叶PDA NFC读取功能。代码请关注微信公众号“静之随心手记”,回复NFC获取。如有问题可私信或者微信公众号留言。

创作不易,不喜勿喷!!!

猜你喜欢

转载自blog.csdn.net/lzllzllhl/article/details/107248993
今日推荐