因项目需要,开发NFC相关的功能,将了解的微末记录一下。
- 先看官方资料
还是推荐先看官方文档,Advanced NFC (备好梯子)。
这边主要看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的异常,避免异常崩溃