高级NFC

文章译自:Advanced NFC 

本文档介绍了高级NFC,如与各种标签技术协作,NFC标签写入和前台调度,它允许应用程序在前台处理的intent,即使当其他应用程序过滤器相同的。

标签技术支持工作

当NFC标签和Android工作时,使用读取和写入数据的的主要格式是NDEF标签。当设备扫描标签NDEF数据,Android提供了解析NdefMessage消息和其他可能的支持。当扫描标签不包含NDEF数据或NDEF数据不能被映射到一个MIME类型或UR时,在这些情况下,你需要打开tag并直接与其信息交换和使用自己的协议读写数据(在原始字节)。Android为使用的android.nfc.tech包情况提供了通用的支持,android.nfc.tech包如表1中所描述的 。您可以使用的getTechList()方法来确定标签的技术支持,并建立 与之一由android.nfc.tech类相应TagTechnology对象。

Table 1. Supported tag technologies

Class Description
TagTechnology 所有标签技术类必须实现的接口。
NfcA 提供NFC-A(ISO 14443-3A)的性能和I / O操作的访问。
NfcB 提供NFC-B (ISO 14443-3B)的性能和I / O操作的访问。
NfcF 提供 NFC-F (JIS 6319-4)的性能和I / O操作的访问。
NfcV 提供 NFC-V (ISO 15693)的性能和I / O操作的访问。
IsoDep 提供 ISO-DEP (ISO 14443-4)的性能和I / O操作的访问。
Ndef 提供NFC标签已被格式化为NDEF的数据和操作的访问。
NdefFormatable 提供可能被格式化为NDEF的 formattable的标签。

下面的tag技术并不强制Android设备支持(即选择性的支持)。

Table 2. Optional supported tag technologies

Class Description
MifareClassic 如果此Android设备支持MIFARE,提供访问的MIFARE Classic性能和I / O操作。
MifareUltralight 如果此Android设备支持MIFARE,提供访问的MIFARE 超轻性能和I / O操作。

Tag技术与ACTION_TECH_DISCOVERED intent间协作

当设备扫描含有NDEF数据的tag,但不能被映射到一个MIME或URI,标签调度系统试图启动一个携带ACTION_TECH_DISCOVERED intent的activity。 扫描tag不是NDEF数据格式时,ACTION_TECH_DISCOVERED也可使用。有这种东西,如果标签调度系统无法解析tag时,可以直接与tag上数据进行交互。tag技术工作时的基本步骤如下:
1、过滤指定要处理的标签技术的ACTION_TECH_DISCOVERED intent。在一般情况下,标签调度系统尝试启动一个ACTION_TECH_DISCOVERED的intent,当NDEF消息,不能被映射为MIME类型或URI,或者如果标签扫描不包含NDEF数据。如何确定欲了解更多信息,请参阅标签调度系统。
2、当你的应用程序接收的intent,取得标签对象:

Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
3、获得实例的TagTechnology,通过调用android.nfc.tech包 的get方法。调用get方法前,通过调用getTechList()可以列举tag支持的技术。例如,从tag中要获得一个实例MifareUltralight,可以如下做:

MifareUltralight.get(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));

Tags读写

NFC标签读写必须确保获得intent并打开tag间通信。为了读取和写入数据到标签,您必须定义自己的协议栈。请记住,当直接与tag交互时,你仍然可以读取写NDEF格式的数据。它依赖你怎么去构建。下面的例子显示了如何使用一个MIFARE超轻标签。

package com.example.android.nfc;

import android.nfc.Tag;
import android.nfc.tech.MifareUltralight;
import android.util.Log;
import java.io.IOException;
import java.nio.charset.Charset;

public class MifareUltralightTagTester {

    private static final String TAG = MifareUltralightTagTester.class.getSimpleName();

    public void writeTag(Tag tag, String tagText) {
        MifareUltralight ultralight = MifareUltralight.get(tag);
        try {
            ultralight.connect();
            ultralight.writePage(4, "abcd".getBytes(Charset.forName("US-ASCII")));
            ultralight.writePage(5, "efgh".getBytes(Charset.forName("US-ASCII")));
            ultralight.writePage(6, "ijkl".getBytes(Charset.forName("US-ASCII")));
            ultralight.writePage(7, "mnop".getBytes(Charset.forName("US-ASCII")));
        } catch (IOException e) {
            Log.e(TAG, "IOException while closing MifareUltralight...", e);
        } finally {
            try {
                ultralight.close();
            } catch (IOException e) {
                Log.e(TAG, "IOException while closing MifareUltralight...", e);
            }
        }
    }

    public String readTag(Tag tag) {
        MifareUltralight mifare = MifareUltralight.get(tag);
        try {
            mifare.connect();
            byte[] payload = mifare.readPages(4);
            return new String(payload, Charset.forName("US-ASCII"));
        } catch (IOException e) {
            Log.e(TAG, "IOException while writing MifareUltralight
            message...", e);
        } finally {
            if (mifare != null) {
               try {
                   mifare.close();
               }
               catch (IOException e) {
                   Log.e(TAG, "Error closing tag...", e);
               }
            }
        }
        return null;
    }
}

前台调度系统的使用

前台调度系统允许activity截取intent并声明自己比其他处理相同intent的activity的优先级要高。使用这个系统可以为android系统构建一些数据结构,数据结构能够为应用程序发送适当的意图。为了使前景调度系统:
1、在activity的onCreate()方法中添加如下code:
 a、创建一个PendingIntent对象,android系统可以填充在tag被扫描到的内容。
PendingIntent pendingIntent = PendingIntent.getActivity(
 this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
b、声明intent过滤器来处理你想拦截的intent。前台调度系统检查过滤指定的intent,这个intent是扫描标签时收到的。如果匹配,那么你的应用程序处理的意图。如果它不匹配,前台调度系统回到intent调度系统的。指定一个空的数组intent过滤器和技术过滤器,指定要过滤所有的标签,回到TAG_DISCOVERED  intent。下面的代码片段处理所有MIME类型NDEF_DISCOVERED。您应该只处理那些你需要的。
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("*/*");/* Handles all MIME based dispatches.
                             You should specify only the ones that you need. */
    }
    catch (MalformedMimeTypeException e) {
        throw new RuntimeException("fail", e);
    }
   intentFiltersArray = new IntentFilter[] {ndef, };
c、设置您的应用程序要处理tag技术的数组。调用 Object.class.getName()方法来获取支持的所以技术。
techListsArray = new String[][] { new String[] { NfcF.class.getName() } };

2、重写以下activity生命周期回调函数,当activity失去(onPause())和恢复(onResume())焦点时,添加逻辑启用和禁用前台调度。enableForegroundDispatch()必须主线程调用,并只有当activity在前台(调用onResume()保证这一点)。您还需要实现onNewIntent回调函数来处理扫描NFC标签的数据。
public void onPause() {
    super.onPause();
    mAdapter.disableForegroundDispatch(this);
}

public void onResume() {
    super.onResume();
mAdapter.enableForegroundDispatch(this,pendingIntent,intentFiltersArray,techListsArray);
}

public void onNewIntent(Intent intent) {
    Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    //do something with tagFromIntent
}
完整的例子可以查看API demo中的ForegroundDispatch部分。

猜你喜欢

转载自blog.csdn.net/zcmain/article/details/77533214
NFC