左晓为主开发手持机充值管理机
zuoxiao
2023-11-30 8e3719a37a6be0c9e244a0160b5f2f3331ca3122
app/src/main/java/com/dayu/recharge/tools/LoyaltyCardReader.java
@@ -16,9 +16,11 @@
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;
@@ -45,14 +47,21 @@
    // 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;
    }
    /**
@@ -65,12 +74,17 @@
    @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 {
@@ -105,6 +119,11 @@
        }
    }
    }
    /**
     * Build APDU for SELECT AID command. This command indicates which service a reader is
     * interested in communicating with. See ISO 7816-4.