左晓为主开发手持机充值管理机
zuoxiao
2023-11-30 8e3719a37a6be0c9e244a0160b5f2f3331ca3122
app/src/main/java/com/dayu/recharge/activity/BaseNfcActivity.java
@@ -9,12 +9,20 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.nfc.Tag;
import android.nfc.cardemulation.CardEmulation;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.dayu.recharge.card.UserCard;
import com.dayu.recharge.tools.LoyaltyCardReader;
import com.dayu.recharge.tools.NFCUtil;
import com.dayu.recharge.tools.NfcReadHelper;
import com.dayu.recharge.utils.LogUtil;
import com.dayu.recharge.utils.TipUtil;
import com.tencent.bugly.crashreport.CrashReport;
@@ -30,7 +38,7 @@
 * 在onNewIntent方法中执行intent传递过来的Tag数据
 * 将NFC标签卡靠近手机后部(NFC标签卡可网上自行购买)
 */
public class BaseNfcActivity extends BaseActivity {
public class BaseNfcActivity extends BaseActivity implements LoyaltyCardReader.AccountCallback {
    /**
     * 该卡已写入用户信息
@@ -47,23 +55,34 @@
    public final static int ERROR = -1;
    protected NfcAdapter mNfcAdapter;
    private PendingIntent mPendingIntent;
//    protected NfcAdapter mNfcAdapter;
//    private PendingIntent mPendingIntent;
    volatile UserCard userCard;
    Handler handler;
    public LoyaltyCardReader mLoyaltyCardReader;
    NFCUtil nfcUtil;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nfcUtil = NFCUtil.getInstance();
        nfcUtil.setAccountCallback(this);
    }
    /**
     * onCreat->onStart->onResume->onPause->onStop->onDestroy
     * 启动Activity,界面可见时.
     */
    @Override
    protected void onStart() {
        super.onStart();
        //此处adapter需要重新获取,否则无法获取message
        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        //一旦截获NFC消息,就会通过PendingIntent调用窗口
        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);
        nfcUtil.onStartNfcAdapter(this);
    }
@@ -73,12 +92,7 @@
    @Override
    public void onResume() {
        super.onResume();
        if (!ifNFCUse(this)) {
            this.finish();
        }
        //设置处理优于所有其他NFC的处理
        if (mNfcAdapter != null)
            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
        nfcUtil.onResumeNfcAdapter(this);
    }
    /**
@@ -88,62 +102,17 @@
    public void onPause() {
        super.onPause();
        //恢复默认状态
        if (mNfcAdapter != null)
            mNfcAdapter.disableForegroundDispatch(this);
        nfcUtil.onPauseNfcAdapter(this);
    }
    /**
     * 检测工作,判断设备的NFC支持情况
     *
     * @return
     */
    protected Boolean ifNFCUse(Activity context) {
        if (mNfcAdapter == null) {
            TipUtil.show(context, "设备不支持NFC!");
            return false;
        }
        if (mNfcAdapter != null && !mNfcAdapter.isEnabled()) {
            TipUtil.show(context, "请在系统设置中先启用NFC功能!");
            return false;
        }
        return true;
    @Override
    public void physicalCardDoing(Tag tag) {
    }
    /**
     * 读取全部数据
     */
    public void readAllData(Intent intent) {
        try {
            NfcReadHelper.getInstence(intent)
                    .getAllData(new NfcReadHelper.NFCCallListback() {
                        @Override
                        public void callBack(Map<String, List<byte[]>> data) {
                            Message message = new Message();
//                           获取1扇区数据
                            List<byte[]> oneSector = data.get("1");
                            if (oneSector != null && oneSector.size() > 0) {
                                userCard = UserCard.getBean(oneSector);
                                message.what = RECHARGE;
                            } else {
                                message.what = ERROR;
                            }
                            if (handler != null) {
                                handler.sendMessage(message);
                            }
                        }
    @Override
    public void virtualCardDoing(Tag tag) {
                        @Override
                        public void error(int code) {
                            if (handler != null) {
                                Message message = new Message();
                                message.what = ERROR;
                                handler.sendMessage(message);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
            CrashReport.postCatchedException(e);
        }
    }
}