How to read card information with NFC mobile phone

Keywords: Android Mobile SDK

Here are the steps:

1. Get management permission: this development package has been integrated, which can be done with only one code call. This is quite convenient.

        PermissionUtil.grantNeedPermission(this);

2. Open NFC and listen to NFC events. If you don't understand this paragraph, you can directly search Android on the Internet and use NFC to see the introduction

private NfcAdapter      mAdapter = null; // Define NFC adapter
mAdapter = NfcAdapter.getDefaultAdapter(getApplicationContext());
pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);//.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
mTechLists = new String[][]{new String[]{NfcB.class.getName()}, new String[]{NfcA.class.getName()}};
if (mAdapter != null && !mAdapter.isEnabled()) {
    Toast.makeText(this, "NFC Not yet opened", Toast.LENGTH_SHORT).show();
}

3. Because my NFC event is in a sub thread, new has a handle

private static class MyHandler extends Handler {
        private final WeakReference<IDCardScannerActivity> mActivity;

        public MyHandler(IDCardScannerActivity activity) {
            mActivity = new WeakReference<IDCardScannerActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            System.out.println(msg);
            if (mActivity.get() == null) {
                return;
            }
            if (msg.what == 1002)
                throw new RuntimeException();
            else {
                try {
                    mActivity.get().todo(msg);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

4. Initialization class:

private OTGReadCardAPI  ReadCardAPI; // Define read class

ReadCardAPI = new OTGReadCardAPI(getApplicationContext(), this, false); // Initialization class, the third is the callback address, the third is to directly pass false

// Set server address and port
ArrayList<Serverinfo> twoCardServerlist = new ArrayList<Serverinfo>();
twoCardServerlist.add(new Serverinfo("id.yzfuture.cn", 8848));
ReadCardAPI.setServerInfo(twoCardServerlist, null, bTestServer);

5. When the mobile phone detects that the chip is close, it will call back onNewIntent, where it can read directly. But to avoid problems, I send messages directly to Handle.

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        inintent = intent;
        mHandler.sendEmptyMessageDelayed(1, 0);
    }

6. In MyHandler, it will be transferred to todo, which is the specific operation

    public void todo(Message msg) throws FileNotFoundException {
        int tt = 0;
        m_szProcess = "";
        process.setText("");
        m_szAppKey = sharedPreferences.getString("AppKey", "");
        int     nnum = ReadCardAPI.GetAppKeyUseNum(m_szAppKey);
        if (nnum <= 0) nnum = 0;
        numtxt.setText(String.valueOf(nnum));
        if (m_szAppKey.isEmpty() || nnum<=0)
        {
            regeditAppkey(true);
        }
        appkeyTxt.setText(m_szAppKey);
        if (!m_szAppKey.isEmpty() && nnum>0)
        {
            if (msg.what == 1 || msg.what==1005)
            {
                if (msg.what == 1005)
                {
                    inintent = null;
                }
                tt = ReadCardAPI.NfcReadCard(m_szAppKey, null, inintent, eCardType.eTwoGeneralCard, "", m_bAuthon);
            }
            if (m_bshow)
            {
                userInfo.setText(m_szUserInfo);
            }
            if (tt == 41)
            {
                if (m_berror)
                {
                    setdialog("Card reading failed!" + ReadCardAPI.GetErrorInfo());
                }
                else
                {
                    int     nerr = ReadCardAPI.GetErrorCode();
                    if (nerr==-24997)
                    {
                        setdialog("This device does not have decoding permission!");
                    }
                    else setdialog("Card reading failed!");
                }

                edtype.setText("");
                edname.setText("");
                edenname.setText("");
                edid.setText("");

                addTrue.setText("");
                sexTrue.setText("");
                mzTrue.setText("");
                jgTrue.setText("");
                birthTrue.setText("");

                yxqTrue.setText("");
                gjTrue.setText("");
                process.setText("");
            }
            if (tt == 90)
            {
                if (ReadCardAPI.GetTwoCardInfo().arrTwoIdPhoto != null) {
                    idimg.setBackground(new BitmapDrawable(Bytes2Bimap(ReadCardAPI.GetTwoCardInfo().arrTwoIdPhoto)));
                }
                if (ReadCardAPI.GetTwoCardInfo().szTwoType.equals("J")) {
                    edtype.setText("Residence permit for Hong Kong and Macao residents");
                    othernoTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoOtherNO);
                    signTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoSignNum);
                } else {
                    edtype.setText("Second generation certificate of residents");
                    othernoTrue.setText("");
                    signTrue.setText("");
                }
                edid.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdNo.trim());
                edname.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdName.trim());
                addTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdAddress.trim());
                sexTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdSex.trim());
                mzTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdNation.trim() + "family");
                gjTrue.setText("China");
                jgTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdSignedDepartment.trim());
                birthTrue.setText(ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdBirthday.substring(6, 8));

                String startDate = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodBegin.substring(6, 8);
                String endDate = "";
                int nlen = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.indexOf("Long-term");
                if (nlen != -1) {
                    endDate = "Long-term";
                } else {
                    endDate = ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(0, 4) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(4, 6) + "-" + ReadCardAPI.GetTwoCardInfo().szTwoIdValidityPeriodEnd.substring(6, 8);
                }
                yxqTrue.setText(startDate + "~" + endDate);
                //process.setText("100");
                ReadCardAPI.release();
            }
        }
    }

This is the end of reading. Of course, for the first time, you can use the interface that comes with the development package to register. Registration is also simple. You only need to fill in the nickname and mobile number, and then fill in the verification code.

The following is the complete code link of my project. I packed and uploaded it to the online disk. Download it directly and open it with android studio. At the beginning of learning, please forgive me for some bad things.

https://download.csdn.net/download/TygerZH/12066089

For the convenience of students who don't want to compile with source code, I specially compiled an installable APK, which can be directly downloaded and installed on the mobile phone, and then the compiled APK can be directly installed. APK download address: https://download.csdn.net/download/TygerZH/12068701

In addition, if you need the original SDK and documents, you can download them directly from his resources. What the author provides is quite complete. There are both pc and Android SDK and documents. Here is the link to his resources. You can get what you need.

https://download.csdn.net/user/cdyzfuture/uploads Complete resources

https://download.csdn.net/download/cdyzfuture/12050709 Android Platform SDK

https://download.csdn.net/download/cdyzfuture/12050698 Android platform development documents

Published 2 original articles, praised 10 and visited 1516
Private letter follow

Posted by edcaru on Tue, 14 Jan 2020 18:21:13 -0800