| | |
| | | package com.dayu.recharge.tools; |
| | | |
| | | import android.annotation.TargetApi; |
| | | import android.app.Activity; |
| | | import android.nfc.NfcAdapter; |
| | | import android.nfc.Tag; |
| | | import android.nfc.tech.IsoDep; |
| | | import android.nfc.tech.MifareClassic; |
| | | import android.os.Build; |
| | | import android.util.Log; |
| | | |
| | |
| | | |
| | | // Weak reference to prevent retain loop. mAccountCallback is responsible for exiting |
| | | // foreground mode before it becomes invalid (e.g. during onPause() or onStop()). |
| | | private WeakReference<AccountCallback> mAccountCallback; |
| | | private AccountCallback mAccountCallback; |
| | | |
| | | Activity mActivity; |
| | | |
| | | public interface AccountCallback { |
| | | public void onAccountReceived(Tag tag); |
| | | //实体卡回调 |
| | | public void physicalCardDoing(Tag tag); |
| | | |
| | | //虚拟卡回调 |
| | | public void virtualCardDoing(Tag tag); |
| | | } |
| | | |
| | | public LoyaltyCardReader(AccountCallback accountCallback) { |
| | | mAccountCallback = new WeakReference<AccountCallback>(accountCallback); |
| | | public LoyaltyCardReader(Activity activity, AccountCallback accountCallback) { |
| | | mActivity = activity; |
| | | mAccountCallback = accountCallback; |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public void onTagDiscovered(Tag tag) { |
| | | Log.i(TAG, "New tag discovered"); |
| | | mAccountCallback.get().onAccountReceived(tag); |
| | | // Android's Host-based Card Emulation (HCE) feature implements the ISO-DEP (ISO 14443-4) |
| | | // protocol. |
| | | // |
| | | // In order to communicate with a device using HCE, the discovered tag should be processed |
| | | // using the IsoDep class. |
| | | MifareClassic mifareClassic= MifareClassic.get(tag); |
| | | //普通M1卡 |
| | | if (mifareClassic!=null){ |
| | | mActivity.runOnUiThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | mAccountCallback.physicalCardDoing(tag); |
| | | } |
| | | }); |
| | | }else { |
| | | // Android 的基于主机的卡仿真 (HCE) 功能实现了 ISO-DEP (ISO 14443-4) 协议。为了使用 HCE 与设备通信,应使用 IsoDep 类处理发现的标记。 |
| | | IsoDep isoDep = IsoDep.get(tag); |
| | | if (isoDep != null) { |
| | | try { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * Build APDU for SELECT AID command. This command indicates which service a reader is |
| | | * interested in communicating with. See ISO 7816-4. |