nfc读卡本来在android4.1读写很正常,在4.4就出现莫名其妙的错误,搞了好久,终于有点眉目。
这里说到,android版本对nfc的影响,这里就不重复了,总结一下。
对于android4.1-4.3以下nfc对卡片的读取就是以下代码:
<span style="font-size:18px;">NfcAdapter mAdapter = NfcAdapter.getDefaultAdapter(this);
public void registerReceiver() {
if (mAdapter != null) {
mFilters = new IntentFilter[]{
new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED),
new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED),
new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED)};
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
try {
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, null);
} catch (Exception e) {
}
}
}
public void unregisterReceiver() {
if (mAdapter != null) {
try {
mAdapter.disableForegroundDispatch(this);
} catch (Exception e) {
}
}
}</span>
然后触发的代码在
<span style="font-size:18px;">protected void onNewIntent(Intent intent) {
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
IsoDep isoDep = IsoDep.get(tagFromIntent);
}</span>
在 AndroidManifest.xml 的相应的activity加上
<span style="font-size:18px;">android:launchMode="singleTask"</span>
而上面适用于android4.4以下的方法在android4.4也是可以运行,
不过会出现有些卡能读写,有些卡无限重连,有些卡两条指令之间不能超过300ms
对于android4.4以上nfc对卡片的读取就是以下代码:api19
<span style="font-weight: normal;"><span style="font-size:18px;">public static int READER_FLAGS = NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK;
private void enableReaderMode() {
Activity activity = getActivity();
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
Bundle options = new Bundle();
<span style="color:#ff0000;">options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 5000);</span>//延迟对卡片的检测
if (nfc != null) {
nfc.enableReaderMode(activity, mCardReader, READER_FLAGS, options);
}
}
private void disableReaderMode() {
Activity activity = getActivity();
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
if (nfc != null) {
nfc.disableReaderMode(activity);
}
}</span></span>
<span style="font-weight: normal;"><span style="font-size:18px;">public static int READER_FLAGS = NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK;
private void enableReaderMode() {
Activity activity = getActivity();
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
Bundle options = new Bundle();
<span style="color:#ff0000;">options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 5000);</span>//延迟对卡片的检测
if (nfc != null) {
nfc.enableReaderMode(activity, mCardReader, READER_FLAGS, options);
}
}
private void disableReaderMode() {
Activity activity = getActivity();
NfcAdapter nfc = NfcAdapter.getDefaultAdapter(activity);
if (nfc != null) {
nfc.disableReaderMode(activity);
}
}</span></span>
在onTagDiscovered编写具体的卡片操作
<span style="font-size:18px;">@Override
public void onTagDiscovered(Tag tag) {
IsoDep isoDep = IsoDep.get(tag);
}</span>
在 AndroidManifest.xml 的相应的activity加上
<span style="font-size:18px;">android:launchMode="singleTask"</span>
如果不加红色的那句代码,会导致一些卡操作出现异常,猜测是因为nfc本身有一定间隔的时间去检测卡片在不在,导致两个命令之间不能超过300ms,而加上红色那句,又会导致没法检测卡片是否离开。暂时只能通过业务逻辑控制。