Android mobile APP reading and writing high frequency RFID tag (campus card) NDEF format data design

**

Can RFID tags be read through the NFC function of the mobile phone

**

Some labels can be read

RFID tags have many types: according to different frequencies, they can be divided into low frequency (LF), high frequency (HF), ultra-high frequency (UHF) and microwave (MW) electronic tags.

1. The typical working frequency of high-frequency card is 13.56MHz. It is the most mature and widely used card in China, and there are many kinds of cards.

2. The frequency of low-frequency card is generally below 135K Hz, and the typical ID card of 125Hz is widely used. This card has only one solidified serial number that can be read by the device

3. The typical working frequencies of UHF and microwave tags are 433MHz, 900MHz, 2.45GHz and 5.8GHz, and the maximum card reading distance can be more than 10m. Typical applications of such labels include: logistics and supply management, manufacturing and assembly, air baggage handling, mail, express parcel handling, document tracking, access control, electronic tickets, automatic road charging, etc. Such label technology is now an indispensable part of the Internet of things.

Among them, high-frequency 13.56MHz campus cards such as campus cards can be read by the NFC function of mobile phones. (but not only 13.56MHz, but also other measured frequency bands)

Next, let's analyze how to use Android's NFC function to interact with high-frequency cards!

It's best not to try to scribble your campus card!!! Just read it!

Android code

Experimental environment

Android Studio development tools
Project folder

code

First at Android manifest Apply for the permissions we need in the XML file

<!--NFC Basic authority-->
    <uses-permission android:name="android.permission.NFC" />
    <uses-feature
        android:name="android.hardware.nfc"
        android:required="true" />
    <!--Vibration authority-->
    <uses-permission android:name="android.permission.VIBRATE" />

Design the main interface

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt_write_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="write in NDEF Format data" />
	
	......ellipsis......

</LinearLayout>


Read interface

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Please keep your phone close to NFC label"
        android:textColor="@android:color/darker_gray"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/rv_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

Write interface

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:onClick="onClick"
        android:id="@+id/bt_write_text"
        android:text="write in"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

BaseNFCActivity.java

package com.ljb.nfcreadandwritedemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import utils.NFCHelper;

/**
 * Created by ljb on 2018/8/1.
 */
public class BaseNFCActivity extends AppCompatActivity {
    protected NFCHelper nfcHelper;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        nfcHelper = new NFCHelper(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        //Judge whether the device supports NFC function
        if (nfcHelper.isSupportNFC()) {
            //Judge whether the device turns on the NFC function
            if (nfcHelper.isEnableNFC()) {
                //Register FNC listener
                nfcHelper.registerNFC(this);
            } else {
                nfcHelper.showFNCSetting(this);
            }
        } else {
            showToast("Current device does not support NFC function");
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        nfcHelper.unRegisterNFC(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        log("Action: " + intent.getAction());

    }

    public void start(Class clazz) {
        startActivity(new Intent(this, clazz));
    }

    public void showToast(String content) {
        if (TextUtils.isEmpty(content))
            return;
        Toast.makeText(this, content, Toast.LENGTH_SHORT).show();

    }

    public void log(String content) {
        Log.e(getClass().getSimpleName(), content);
    }
}

ReadMUActivity

package com.ljb.nfcreadandwritedemo.read;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.ljb.nfcreadandwritedemo.BaseNFCActivity;
import com.ljb.nfcreadandwritedemo.R;

/**
 * Created by ljb on 2018/8/3.
 */
public class ReadMUActivity extends BaseNFCActivity {

    private TextView tvRead;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
        tvRead = findViewById(R.id.rv_read);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String ret = nfcHelper.readNFC_MU(intent);
        if (ret != null) {
            nfcHelper.vibrate(this);
            tvRead.setText("The content is: " + ret);
        }
    }
}

According to the above code, we can simply read the card number of the campus card.

If you have too much code, you won't paste them one by one.

Complete source code, please reply to the code in WeChat computer official account store.

Students who are interested in this content or who are interested in this official account can join the official QQ group to discuss and learn from each other, learn from each other and make progress together. The source code and the specific operation process will also be put into the group. If there is no detail, there will be some answers in the group. Join our big family QQ group number: 559369389 welcome new members!

Keywords: Android Design Pattern app

Added by dakkonz on Wed, 09 Feb 2022 09:22:04 +0200