因項目需要,開發NFC相關的功能,將了解的微末記錄一下。
- 先看官方資料
還是推薦先看官方文檔,Advanced NFC (備好梯子)。
相關的Tag標準
這邊主要看IsoDep和Nedf。
- 基本步驟
首先我們需要在Manifest中添加權限:
<uses-permission android:name="android.permission.NFC" />
在activity中添加intent過濾器。過濾器有4種,我們主要添加:
ACTION_TECH_DISCOVERED
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter"
/>
</activity>
其中nfc_tech_filter是一個篩選tag的過濾文件:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2" >
<tech-list>
<tech>android.nfc.tech.MifareClassic</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.Ndef</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NdefFormatable</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
</tech-list>
</resources>
以上步驟完成,只是實現了NFC的后臺調度過程,NFC設備靠近時,會喚起廣播,并彈出打開應用的請求,但是在界面上,并不能做到數據的截取,并且效果是:打開注冊的activity后,如果NFC設備依然靠近,會再次彈出選擇打開應用的請求框。
如此我們就需要一個前臺調度的過程。
// NFC相關
PendingIntent pendingIntent;
private IntentFilter[] intentFiltersArray;
private NfcAdapter nfcAdapter;
private StringBuilder stringBuilder=new StringBuilder();
private String[][] techLists;
onCreate中:
// NFC 處理
Intent nfcIntent = new Intent(this,getClass());
nfcIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this,0,nfcIntent,0);
IntentFilter pendingFilter = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
techLists = new String[][] { { IsoDep.class.getName() },
{ NfcA.class.getName()}};
pendingFilter.addDataType("*/*");
intentFiltersArray = new IntentFilter[]{pendingFilter};}
catch (IntentFilter.MalformedMimeTypeException e)
{
e.printStackTrace();
}
// 獲取默認的NFC控制器
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null)
{
Toast.makeText(DealRecordQueryActivity.this, "設備不支持NFC!", Toast.LENGTH_LONG).show();
}
else
{
if (!nfcAdapter.isEnabled())
{
Toast.makeText(DealRecordQueryActivity.this,"請在系統設置中先啟用NFC功能!",Toast.LENGTH_LONG).show();
}
}
在onResume中開啟前臺調度:
@Override
protected void onResume()
{
super.onResume();
nfcAdapter.enableForegroundDispatch(this,pendingIntent,intentFiltersArray,techLists);
if(NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction()))
{
onNewIntent(getIntent());
}
}
在onNewIntent中處理由NFC設備傳遞過來的intent:
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
processIntent(intent);
}
接下來就是解析intent中的tag信息了,不同的NFC設備有不同的協議。
private void processIntent(Intent intent) {
//取出封裝在intent中的TAG
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
tagFromIntent.getId();
tagFromIntent.getTechList();
Log.v("aaa", " tagFromIntent.getId()=>" + tagFromIntent.getId());
Log.v("aaa", " tagFromIntent.getTechList()=>" + tagFromIntent.getTechList());
String action = intent.getAction();
Log.v("aaa", "action=>" + action);
IsoDep isodep = IsoDep.get(tagFromIntent);
Log.v("aaa", "IsoDep=>" + isodep);
try{
//Get an instance of the type A card from this TAG
isodep.connect();
}catch(Exception e){
Log.v("aaa", "數據錯了:" + e.getMessage());
Toast.makeText(this,"卡片型號不匹配或讀取失敗,請重試",Toast.LENGTH_SHORT).show();
}
.....
}
- 注意
1.前臺調度再onPause中要取消
//onPause中取消前臺調度
nfcAdapter.disableForegroundDispatch(DealRecordQueryActivity.this);
2.onDestroy中不要做取消操作
@Override
protected void onDestroy()
{
super.onDestroy();
//onDestroy中不能做前臺調度的注銷操作。會引起 Connect not call 的異常.
// if(nfcAdapter.isEnabled()){
// nfcAdapter.disableForegroundNdefPush(this);
//nfcAdapter.disableForegroundDispatch(DealRecordQueryActivity.this);
// }
nfcAdapter = null;
}
3.tag處理過程中要注意處理catch的異常,避免異常崩潰