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

技术文档

安卓(Android)下如何开发USB NFC读写器app

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

      荣士第二代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读、写M1卡源码:

package com.ic.ic02test;
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 BLOCK0_EN = 0x01;        //允许读写第0块
    private static final byte BLOCK1_EN = 0x02;        //允许读写第1块
    private static final byte BLOCK2_EN = 0x04;        //允许读写第2块
    private static final byte NEEDSERIAL = 0x08;       //仅读指定序列号的卡
    private static final byte EXTERNKEY = 0x10;        //需要指定密码
    private static final byte NEEDHALT = 0x20;         //休眠卡

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        tv = findViewById(R.id.sample_text);
        tv.setText("操作结果");
    }

    //读取IC卡卡号---------------------------------------------------------------------------------------------
    public void piccrequest(View view)
    {
        byte status;//存放返回值
        byte[] mypiccserial = new byte[4];
        long cardhao;

        String strls = "";
        status = ourmifare.piccrequest(this,mypiccserial);

        if(status == 0)
        {
            strls = "读取成功!卡号为";
            cardhao = mypiccserial[3] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[2] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[1] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[0] & 0xff;
            String strls1 = "000000000"+Long.toString(cardhao);//0305887634  123B7992
            strls = strls + strls1.substring(strls1.length()-10);
        }
        else
        {
            if(status == 8)
            {
                strls = "请将卡放在IC卡读卡器感应区";
            }
            else if(status == 23)
            {
                strls = "错误提示:读卡器未插入";
            }
            else
            {
                strls = "错误代码:" + Integer.toString(status);
            }
        }
        tv.setText(strls);
    }

    //轻松读卡,返回4字节卡号及48字节扇区数据--------------------------------------------------------------------
    public void piccreadex(View view)
    {
        byte status;//存放返回值
        byte myareano;//区号
        byte authmode;//密码类型,用A密码或B密码
        byte myctrlword;//控制字
        byte[] mypiccserial = new byte[4];//卡序列号
        byte[] mypicckey = new byte[6];//密码
        byte[] mypiccdata = new byte[48];//卡数据缓冲
         
        myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;  //控制字指定
        myareano = 8;//指定为第8区         
        authmode = 1;  //批定密码模式,大于0表示用A密码认证,推荐用A密码认证

        //指定密码
        mypicckey[0] = (byte)0xFF;
        mypicckey[1] = (byte)0xFF;
        mypicckey[2] = (byte)0xFF;
        mypicckey[3] = (byte)0xFF;
        mypicckey[4] = (byte)0xFF;
        mypicckey[5] = (byte)0xFF;

        long cardhao;

        String strls = "";
        status = ourmifare.piccreadex(this,myctrlword,mypiccserial,myareano,authmode,mypicckey,mypiccdata);

        if(status == 0)
        {
            strls = "读取成功!卡号为";
            cardhao = mypiccserial[3] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[2] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[1] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[0] & 0xff;

            String strls1 = "000000000"+Long.toString(cardhao);//0305887634
            strls += strls1.substring(strls1.length()-10);

            strls += ",扇区数据为";

            for(int i = 0;i < 47;i++)
            {
                strls1 = "0"+Integer.toHexString(mypiccdata[i] & 0xff);
                strls = strls + strls1.substring(strls1.length()-2) +"-";
            }
            String CardShopId=new String(mypiccdata);
            String CardId=CardShopId.substring(1,16) ;
            String ShopId=CardShopId.substring(17,35) ;
            strls1 = "0"+Integer.toHexString(mypiccdata[47] & 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);
    }

    //轻松写卡:8区,密码12个F,数据1-48,返回4字节卡号-------------------------------------------------------
    public void piccwriteex(View view)
    {
        byte status;//存放返回值
        byte myareano;//区号
        byte authmode;//密码类型,用A密码或B密码
        byte myctrlword;//控制字
        byte[] mypiccserial = new byte[4];//卡序列号
        byte[] mypicckey = new byte[6];//密码
        byte[] mypiccdata = new byte[48];//卡数据缓冲

        myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY;    //控制字指定,
        myareano = 8;//指定区号为第8区
        authmode = 1;//批定密码模式,大于0表示用A密码认证,推荐用A密码认证

        //指定密码
        mypicckey[0] = (byte)0xFF;
        mypicckey[1] = (byte)0xFF;
        mypicckey[2] = (byte)0xFF;
        mypicckey[3] = (byte)0xFF;
        mypicckey[4] = (byte)0xFF;
        mypicckey[5] = (byte)0xFF;

        String CardId="0000000000123456";
        String ShopId="0000000000987654321";
        String WriteStr=CardId + ShopId +"                       ";//写入信息补足不小于48字节
        byte[] WriteBuf=WriteStr.getBytes() ;

        for(int i = 0;i < 48;i++)
        {
            mypiccdata[i] = WriteBuf[i];
        }

        long cardhao;

        String strls = "";
        status = ourmifare.piccwriteex(this,myctrlword,mypiccserial,myareano,authmode,mypicckey,mypiccdata);

        if(status == 0)
        {
            strls = "写卡成功!卡号为";
            cardhao = mypiccserial[3] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[2] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[1] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[0] & 0xff;

            String strls1 = "000000000"+Long.toString(cardhao);//0305887634
            strls += strls1.substring(strls1.length()-10);

            strls += ",写入扇区数据为";

            for(int i = 0;i < 47;i++)
            {
                strls1 = "0"+Integer.toHexString(mypiccdata[i] & 0xff);
                strls = strls + strls1.substring(strls1.length()-2) +"-";
            }

            strls1 = "0"+Integer.toHexString(mypiccdata[47] & 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);
    }

    //改单区密码:8区,密码12个F,改成12个F,返回4字节卡号"--------------------------------------------------
    public void piccchangesinglekeyex(View view)
    {
        byte status;//存放返回值

        byte myareano;//区号
        byte authmode;//密码类型,用A密码或B密码
        byte myctrlword;//控制字
        byte[] mypiccserial = new byte[4];//卡序列号
        byte[] mypicckeyold = new byte[6];//密码
        byte[] mypicckeynew = new byte[17];//卡数据缓冲

        myctrlword = EXTERNKEY;    //指定本次操作的控制字
        myareano = 8;//指定区号为第8区
        authmode = 1;//指定密码认证模式,大于0表示用A密码认证,推荐用A密码认证

        //旧密码
        mypicckeyold[0] = (byte)0xFF;
        mypicckeyold[1] = (byte)0xFF;
        mypicckeyold[2] = (byte)0xFF;
        mypicckeyold[3] = (byte)0xFF;
        mypicckeyold[4] = (byte)0xFF;
        mypicckeyold[5] = (byte)0xFF;

        //新A密码
        mypicckeynew[0] = (byte)0xFF;
        mypicckeynew[1] = (byte)0xFF;
        mypicckeynew[2] = (byte)0xFF;
        mypicckeynew[3] = (byte)0xFF;
        mypicckeynew[4] = (byte)0xFF;
        mypicckeynew[5] = (byte)0xFF;

        //访问位,不要轻易改,改错卡将做废  FF078069
        mypicckeynew[6] = (byte)0xFF;
        mypicckeynew[7] = (byte)0x07;
        mypicckeynew[8] = (byte)0x80;
        mypicckeynew[9] = (byte)0x69;

        //新B密码
        mypicckeynew[10] = (byte)0x00;
        mypicckeynew[11] = (byte)0x00;
        mypicckeynew[12] = (byte)0x00;
        mypicckeynew[13] = (byte)0x00;
        mypicckeynew[14] = (byte)0x00;
        mypicckeynew[15] = (byte)0x00;

        //选项
        mypicckeynew[16] = (byte)0x00;//为0表示只改A官密码 为1表示改A密码同时也改B密码,为3表示改AB密码及访问位,警示:不要轻易改访问位

        long cardhao;

        String strls = "";
        status = ourmifare.piccchangesinglekeyex(this,myctrlword,mypiccserial,myareano,authmode,mypicckeyold,mypicckeynew);

        if(status == 0)
        {
            strls = "修改密码成功!卡号为";
            cardhao = mypiccserial[3] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[2] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[1] & 0xff;
            cardhao *= 256;
            cardhao += mypiccserial[0] & 0xff;
            String strls1 = "000000000"+Long.toString(cardhao);//0305887634
            strls += strls1.substring(strls1.length()-10);
        }
        else
        {
            if(status == 8)
            {
                strls = "请将卡放在IC卡读卡器感应区";
            }
            else if(status == 23)
            {
                strls = "错误提示:读卡器未插入";
            }
            else
            {
                strls = "错误代码:" + Integer.toString(status);
            }
        }
        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运行


 
上一篇:Android安卓系统如何开发USB RFID读卡器APP 下一篇:T5557发卡器动态库函数使用说明二
     
Guangzhou Rong Shi Electronics Co., Ltd., China 广州荣士电子有限公司 备案/许可证编号:粤ICP备11063836号
TEL  020-22307058    020-82301718
消费机
隐私政策

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

网站地图 xml