消费机 水控机 售饭机 电梯门禁 门禁机
快速寻找产品(请输入产品型号或名称的关键词):
首页> 技术文档

技术文档

Android安卓读写NFC Ntag标签

发布者:广州荣士电子有限公司         发布时间: 2022-6-14 

      荣士第二代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包里的函数了。


Android Studio读写Ntagx标签源码:

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);
    }
}

APP运行



 
上一篇:win11记事本不小心卸载了怎么恢复 下一篇:荣士485协议工位机读卡器开发使用说明
     
Guangzhou Rong Shi Electronics Co., Ltd., China 广州荣士电子有限公司 备案/许可证编号:粤ICP备11063836号
TEL  020-22307058    020-82301718
消费机
隐私政策

消费机 水控机 售饭机 电梯门禁 门禁机

网站地图 xml