左晓为主开发手持机充值管理机
zuoxiao
2023-12-11 526138394ee77759e98764153b851acfe6c929af
app/src/main/java/com/dayu/recharge/activity/ReadCardAcitivy.java
@@ -2,6 +2,7 @@
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
@@ -57,7 +58,8 @@
    volatile UserCard userCard;
    ActivityRedCardBinding redCardBinding;
    Intent intent;
    //    Intent intent;
    Tag mTag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
@@ -73,7 +75,7 @@
        redCardBinding.redRemainderBlance.setText("剩余金额:" + MornyUtil.changeF2Y(blance) + "元");
        redCardBinding.redStatu.setText("卡状态:" + statu);
        if (userCard != null) {
            redCardBinding.redInitCode.setText("用户注册编号:" + userCard.getInitPeasantCode());
            redCardBinding.redInitCode.setText(userCard.getInitPeasantCode());
            Calendar calendar = userCard.getRechargeDate();
            if (calendar != null) {
                int year = calendar.get(Calendar.YEAR);
@@ -91,14 +93,20 @@
        }
    }
    @Override
    public void physicalCardDoing(Tag tag) {
        super.physicalCardDoing(tag);
        mTag = tag;
        ProgressDialog.show(this);
        readAllData(tag);
    }
    @Override
    public void onNewIntent(Intent intent) {
        this.intent = intent;
        ProgressDialog.show(this);
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
            readAllData(intent);
        }
//        this.intent = intent;
        super.onNewIntent(intent);
    }
@@ -106,7 +114,7 @@
     * 读取全部数据
     */
    public void readAllData(Intent intent) {
    public void readAllData(Tag intent) {
        NfcReadHelper.getInstence(intent)
                .getOneSectorData(new NfcReadHelper.NFCCallListback() {
                    @Override
@@ -234,24 +242,30 @@
                    try {
                        stateText = new StringBuffer();
                        //金额
                        byte[] blanceByte = Arrays.copyOfRange(readData.getBodyBytes(), 3, 7);
                        int balance = SocketUtil.get16to10LowHigh(blanceByte);
                        //姓名
                        byte[] nameByte = Arrays.copyOfRange(readData.getBodyBytes(), 7, 19);
                        String name = SocketUtil.fromHexString(SocketUtil.bytesToHexClean0(nameByte));
                        int balance = 0;
                        String name = null;
                        try {
                            //金额
                            byte[] blanceByte = Arrays.copyOfRange(readData.getBodyBytes(), 3, 7);
                            balance = SocketUtil.get16to10LowHigh(blanceByte);
                            //姓名
                            byte[] nameByte = Arrays.copyOfRange(readData.getBodyBytes(), 7, 19);
                            name = SocketUtil.fromHexString(SocketUtil.bytesToHexClean0(nameByte));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        String state = BcdUtil.bcdToStr(readData.getBodyBytes()[2]);  //BCD码00启用 01禁用 02隶属信息不符 03无此卡信息 04其它s
                        if ("00".equals(state)) {
                            userCard.setBalance(balance);
                            userCard.setState("00");
                            WriteCardUtils.setUser(intent, userCard);
                            WriteCardUtils.setUser(mTag, userCard);
                            stateText.append("启用");
                        } else if ("01".equals(state)) {
                            userCard.setBalance(balance);
                            userCard.setState("01");
                            WriteCardUtils.setUser(intent, userCard);
                            WriteCardUtils.setUser(mTag, userCard);
                            stateText.append("禁用");
                        } else if ("02".equals(state)) {
                            stateText.append("隶属信息不符");