Table 1. 支持的NFC标签技术
Class | Description |
---|---|
TagTechnology |
The interface that all tag technology classes must implement. |
NfcA |
Provides access to NFC-A (ISO 14443-3A) properties and I/O operations. |
NfcB |
Provides access to NFC-B (ISO 14443-3B) properties and I/O operations. |
NfcF |
Provides access to NFC-F (JIS 6319-4) properties and I/O operations. |
NfcV |
Provides access to NFC-V (ISO 15693) properties and I/O operations. |
IsoDep |
Provides access to ISO-DEP (ISO 14443-4) properties and I/O operations. |
Ndef |
Provides access to NDEF data and operations on NFC tags that have been formatted as NDEF. |
NdefFormatable |
Provides a format operations for tags that may be NDEF formattable. |
下面是不强求android设备支持的技术。
Table 2. 可选技术
Class | Description |
---|---|
MifareClassic |
Provides access to MIFARE Classic properties and I/O operations, if this Android device supports MIFARE. |
MifareUltralight |
Provides access to MIFARE Ultralight properties and I/O operations, if this Android device supports MIFARE. |
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);3.通过包android.nfc.tech中的类的get工厂方法,来获取标签技术类TagTechnology的实例。在这之前你可以通过调用getTechList来获取标签支持的技术的列举。下面是一个用Tag对象获取MifareUltralight 的例子:
MifareUltralight.get(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));
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; } }使用前台派发系统 Using the Foreground Dispatch System
PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);2.为你想拦截intent建立过滤器。出现标签时,系统会用过滤器进行匹配,如果成功则activity获得intent, 如果失败则前台派发系统退化为之前的intent派发系统。声明一个null的Filter的数组,和标签技术个过滤器 的数组,然后填入当回退为TAG_DISVOERED intent的时候你想要捕获的intent的类型。下面的代码片是 拦截所有MIME类型的NDEF_DISCOVERED类型的intent的例子,你应该根据你的需要拦截几种。
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, };3.设置你的app想拦截的标签技术的数组。调用Object.class.getName方法来获得你想支持的技术的类。
techListsArray = new String[][] { new String[] { NfcF.class.getName() } };
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的例子。