如何开发Android安卓APP读写NFC Ntag

      第二代USB免驱动IC读写器IC-02支持Windows、安卓(Android)、Linux系统,为方便IC-02读写器能快速的接入安卓(Android)系统,我们提供了各种安卓版本的So库及示例源码,安卓工程师可直接拷贝以下代码使用到项目中。

      按以下4部操作,可快速将So库加载到您的Android工程项目内:

      1、将我们提供的开发包里的文件夹libs及OURMIFARE_V1.aar,一起复制你的项目APP-->libs目录下;
      2、修改APP-->src下的build.gradle文件,在buildTypes{}的后面,加入
           sourceSets {
                main {
                         jniLibs.srcDirs = ['libs']
                }
           }
           同时在dependencies {}中加入
           implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')
      3、加入NDK工具包。
      4、java的源文件中加入import com.reader.ourmifare;,就可以调用我们在AAR包里的函数了。

读写器介绍:RFID读写器NFC发卡器WEB可编程NDEF文本/网址/智能海报/电话/启动-淘宝网 (taobao.com)https://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-17663462238.11.d4b5789ej0RWYB&id=615391857885

Android Studio源码: 

package com.ic.usbic02ntag;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.reader.ourmifare;//引入我们的读卡器类

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private static final byte NEEDSERIAL = 0x08;
    private static final byte NEEDKEY = 0x10;
    private static final byte NEEDHALT = 0x20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.sample_text);
        tv.setText("操作结果");
    }

    //NFC NTAG卡:寻卡-------------------------------------------------------------------------
    public void piccrequestul(View view)
    {
        byte status;//存放返回值
        byte[] mypiccserial = new byte[7];//卡序列号
        status = ourmifare.piccrequestul(this,mypiccserial);

        String strls = "";
        if(status == 0)
        {
            strls = "寻卡成功!卡号为";
            String strls1;
            for(int i = 0;i < 6;i++)
            {
                strls1 = "0"+Integer.toHexString(mypiccserial[i] & 0xff);
                strls = strls + strls1.substring(strls1.length()-2) +"-";
            }
            strls1 = "0"+Integer.toHexString(mypiccserial[6] & 0xff);
            strls = strls + strls1.substring(strls1.length()-2);
        }
        else
        {
            if(status == 8)
            {
                strls = "请将卡放在IC卡读卡器感应区";
            }
            else if(status == 23)
            {
                strls = "错误提示:读卡器未插入";
            }
            else
            {
                strls = "错误代码:" + Integer.toString(status);
            }
        }
        tv.setText(strls);
    }

    //NFC NTAG卡:轻松读卡--------------------------------------------------------------
    public void piccreadexntag(View view)
    {
        byte status;//存放返回值
        byte myctrlword;//控制字
        byte[] mypiccserial = new byte[7];//卡序列号
        byte[] mypicckey = new byte[4];//卡认证密码
        byte myblockaddr;//起始块地址
        byte myblocksize;//总块数
        byte[] mypiccdata = new byte[48];//读卡的数据缓冲,最大可以读出12个块共48个字节

        myctrlword = 0;
        //myctrlword = myctrlword + NEEDKEY;//如果需要用到密码

        myblockaddr = 4;
        myblocksize = 10;

        status = ourmifare.piccreadexntag(this,myctrlword,mypiccserial,mypicckey,myblockaddr,myblocksize,mypiccdata);
        String strls = "";
        if(status == 0)
        {
            strls = "读取成功!卡号为";
            String strls1;
            for(int i = 0;i < 6;i++)
            {
                strls1 = "0"+Integer.toHexString(mypiccserial[i] & 0xff);
                strls = strls + strls1.substring(strls1.length()-2) +"-";
            }
            strls1 = "0"+Integer.toHexString(mypiccserial[6] & 0xff);
            strls = strls + strls1.substring(strls1.length()-2);
            strls += ",块数据为";
            for(int i = 0;i < (myblocksize*4-1);i++)
            {
                //从databuf[i+1]开始才是数据,databuf[0]为数据长度
                strls1 = "0"+Integer.toHexString(mypiccdata[i+1] & 0xff);
                strls = strls + strls1.substring(strls1.length()-2) +"-";
            }
            strls1 = "0"+Integer.toHexString(mypiccdata[myblocksize*4-1] & 0xff);
            strls = strls + strls1.substring(strls1.length()-2);
        }
        else
        {
            if(status == 8)
            {
                strls = "请将卡放在IC卡读卡器感应区";
            }
            else if(status == 23)
            {
                strls = "错误提示:读卡器未插入";
            }
            else
            {
                strls = "错误代码:" + Integer.toString(status);
            }
        }

        tv.setText(strls);

    }

    //NFC NTAG卡:轻松写卡---------------------------------------------------
    public void piccwriteexntag(View view)
    {
        byte status;//存放返回值
        byte myctrlword;//控制字
        byte[] mypiccserial = new byte[7];//卡序列号
        byte[] mypicckey = new byte[4];//卡认证密码
        byte myblockaddr;//起始块地址
        byte myblocksize;//总块数
        byte[] mypiccdata = new byte[44];//写卡的数据缓冲,最大可以写入11个块共44个字节

        //控制字指定,控制字的含义请查看本公司网站提供的动态库说明
        myctrlword = 0;
        //myctrlword = myctrlword + NEEDKEY;//如果需要用到密码
        myblockaddr = 4;
        myblocksize = 1;
        mypiccdata[0] = 0x11;
        mypiccdata[1] = 0x22;
        mypiccdata[2] = 0x33;
        mypiccdata[3] = 0x44;

        status = ourmifare.piccwriteexntag(this,myctrlword,mypiccserial,mypicckey,myblockaddr,myblocksize,mypiccdata);
        String strls = "";
        if(status == 0)
        {
            strls = "写卡成功!卡号为";
            String strls1;
            for(int i = 0;i < 6;i++)
            {
                strls1 = "0"+Integer.toHexString(mypiccserial[i] & 0xff);
                strls = strls + strls1.substring(strls1.length()-2) +"-";
            }
            strls1 = "0"+Integer.toHexString(mypiccserial[6] & 0xff);
            strls = strls + strls1.substring(strls1.length()-2);
            strls += ",写入块中的数据为";
            for(int i = 0;i < (myblocksize * 4-1);i++)
            {
                //从databuf[i+1]开始才是数据,databuf[0]为数据长度
                strls1 = "0"+Integer.toHexString(mypiccdata[i+1] & 0xff);
                strls = strls + strls1.substring(strls1.length()-2) +"-";
            }
            strls1 = "0"+Integer.toHexString(mypiccdata[myblocksize * 4-1] & 0xff);
            strls = strls + strls1.substring(strls1.length()-2);
        }
        else
        {
            if(status == 8)
            {
                strls = "请将卡放在IC卡读卡器感应区";
            }
            else if(status == 23)
            {
                strls = "错误提示:读卡器未插入";
            }
            else
            {
                strls = "错误代码:" + Integer.toString(status);
            }
        }
        tv.setText(strls);
    }

    //NFC NTAG卡:更改卡密码---------------------------------------------------------
    public void piccinitntag(View view)
    {
        String strls = "NFC NTAG卡:更改卡密码 功能用到piccinitntag函数,已在动态库文件中,请参考VB例子代码";
        tv.setText(strls);
    }


    //读出设备全球唯一的设备编号,作为加密狗用----------------------------------------
    public void pcdgetdevicenumber(View view)
    {
        byte status;//存放返回值
        byte[] devicenumber = new byte[4];
        String strls = "";
        status = ourmifare.pcdgetdevicenumber(this,devicenumber);
        if(status == 0)
        {
            strls = "读取成功!设备编号为";
            String strls1 = "0"+Integer.toHexString(devicenumber[0]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(devicenumber[1]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(devicenumber[2]);
            strls = strls + strls1.substring(strls1.length()-2) +"-";
            strls1 = "0"+Integer.toHexString(devicenumber[3]);
            strls = strls + strls1.substring(strls1.length()-2);
        }
        else
        {
            if(status == 23)
            {
                strls = "错误提示:读卡器未插入";
            }
            else
            {
                strls = "错误代码:" + Integer.toString(status);
            }
        }
        tv.setText(strls);
    }

    //让设备发出声响-----------------------------------------------------
    public void beep(View view)
    {
        byte status;//存放返回值
        String strls = "";
        status = ourmifare.pcdbeep(this,50);

        if(status == 0)
        {
            strls = "读卡器嘀一声成功!!!";
        }
        else
        {
            if(status == 23)
            {
                strls = "错误提示:读卡器未插入";
            }
            else
            {
                strls = "错误代码:" + Integer.toString(status);
            }
        }
        tv.setText(strls);
    }
}

Android APP源码下载:Ntag21x芯片读写Android示例源码.rar-其它文档类资源-CSDN文库https://download.csdn.net/download/zhangjin7422/21411605

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值