安卓Android Studio读写MifareOne M1 IC卡源码

安卓Android Studio读写MifareOne M1 IC卡源码_第1张图片

本示例使用的发卡器:

https://item.taobao.com/item.htm?id=615391857885&spm=a1z10.5-c-s.w4002-21818769070.11.66af789eLeok2R




    

        

        

    

    


    

        

            
package com.usbreadertest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.reader.ourmifare;

import java.util.Timer;
import java.util.TimerTask;

public class IcCardActivity extends AppCompatActivity {
    private TextView tv;

    private static final byte BLOCK0_EN = 0x01;
    private static final byte BLOCK1_EN = 0x02;
    private static final byte BLOCK2_EN = 0x04;
    private static final byte NEEDSERIAL = 0x08;
    private static final byte EXTERNKEY = 0x10;
    private static final byte NEEDHALT = 0x20;

    private long proctimes;

    private Timer timer = null;
    private TimerTask task = null;

    private boolean iscontinue = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ic_card);
        androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        tv = findViewById(R.id.sample_text);
        tv.setText("操作结果");

        Spinner ctrkeymod=findViewById(R.id.SpinnerInOutKey);
        ctrkeymod.setSelection(1);

        Spinner ctrselauth=findViewById(R.id.SpinnerSelABKey);
        ctrselauth.setSelection(1);
    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void retmain(View view)
    {
        finish();
    }


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

        EditText ctruid=findViewById(R.id.TextViewuid);
        ctruid.setText("");
        TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);
        ctr8h10d.setText("");
        tv.setText("");

        status = ourmifare.piccrequest(mypiccserial);
        if(status == 0)
        {
            ourmifare.pcdbeep(38);
            String serialnumber = "";
            for (int i = 0; i < 4; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                serialnumber = serialnumber + bytestr.substring(bytestr.length() - 2, bytestr.length());
            }
            serialnumber=serialnumber.toUpperCase();
            ctruid.setText(serialnumber);

            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
            strls1=strls1.substring(strls1.length()-10,strls1.length());
            ctr8h10d.setText(strls1);

            strls = "读取成功!16进制卡号为:" +serialnumber+"\n转8H10D卡号:"+ strls1;
            tv.setText(strls);
        }
        else
        {
            PrintErrInf(status);
        }
    }

    public void piccwriteserial(View view){
        byte status;//存放返回值
        byte authmode;//密码类型,用A密码或B密码
        byte myctrlword;//控制字
        byte[] mypiccserial = new byte[4];//卡序列号
        byte[] mypicckey = new byte[6];//密码
        byte[] mypiccdata = new byte[48];//卡数据缓冲
        byte[] myblockdata=new byte[16];//第三块数据

        TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);
        ctr8h10d.setText("");
        tv.setText("");

        myctrlword = BLOCK0_EN ;        //控制字指定,控制字的含义请查看本公司网站提供的动态库说明

        Spinner ctrauthmode=findViewById(R.id.SpinnerInOutKey);
        if (ctrauthmode.getSelectedItemId()==1){
            myctrlword=(byte)(myctrlword + EXTERNKEY);        //选用外部密钥认证
        }

        Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);
        authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证

        EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥
        String authkeystr=ctrauthkey.getText().toString().trim();
        if (authkeystr.length()!=12){
            tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");
            return;
        }
        for (int i=0;i<6;i++){
            mypicckey[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
        }

        EditText ctruid=findViewById(R.id.TextViewuid);
        String newuidstr=ctruid.getText().toString().trim();
        if (newuidstr.length()!=8){
            tv.setText("UID为8位16进制数,请输入正确的UID号!");
            return;
        }
        byte edc=0;
        for (int i=0;i<4;i++){
            myblockdata[i]=(byte) Integer.parseInt(newuidstr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
            edc=(byte)(edc ^ myblockdata[i]);   //UID校验字
        }
        myblockdata[4]=edc;

        EditText ctrblock0=findViewById(R.id.Edittextblock0);  //如果第0块显示有数据,将第0块的第5字节以后的数据也写入
        String blockstr0=ctrblock0.getText().toString().trim();
        String[] strArr= blockstr0.split("\\ ");             /*分割接收到的数据后再分析、处理、返回指令 */
        if (strArr.length==16){
            for(int p=5;p<16;p++) {
                myblockdata[p]=(byte)(Integer.parseInt(strArr[p],16));
            }
        }

        status=ourmifare.piccwriteserial(myctrlword,mypiccserial,(byte)0,authmode,mypicckey,myblockdata);
        if(status == 0)
        {
            status = ourmifare.piccrequest(mypiccserial);
            if(status == 0)
            {
                ourmifare.pcdbeep(38);
                String serialnumber = "";
                for (int i = 0; i < 4; i++) {
                    String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                    serialnumber = serialnumber + bytestr.substring(bytestr.length() - 2, bytestr.length());
                }
                serialnumber=serialnumber.toUpperCase();
                ctruid.setText(serialnumber);

                long cardhao;
                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
                strls1=strls1.substring(strls1.length()-10,strls1.length());
                ctr8h10d.setText(strls1);

                String strls = "写UID卡号成功!16进制卡号为:" +serialnumber+"\n转8H10D卡号:"+ strls1;
                tv.setText(strls);
            }
            else
            {
                PrintErrInf(status);
            }
        }
        else
        {
            PrintErrInf(status);
        }
    }

    public void writekeytoe2(View view){
        byte status;//存放返回值
        byte myareano;//区号
        byte authmode;//密码类型,用A密码或B密码
        byte[] mypicckey = new byte[6];//密码

        tv.setText("");

        Spinner spls= findViewById(R.id.SpinnerArea);
        myareano = (byte)(spls.getSelectedItemId());          //指定区号

        String strl="";
        Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);
        authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证
        if(authmode==0){
            strl="B认证密钥";
        }else {strl="A认证密钥";}

        EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥
        String authkeystr=ctrauthkey.getText().toString().trim();
        if (authkeystr.length()!=12){
            tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");
            return;
        }
        for (int i=0;i<6;i++){
            mypicckey[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
        }
        status = ourmifare.pcdwritekeytoe2(myareano,authmode,mypicckey);
        if(status == 0) {
            ourmifare.pcdbeep(38);
            tv.setText("第"+Integer.toString(myareano)+"区内部"+strl+"写入到发卡器芯片内成功!");
        }else{
            PrintErrInf(status);
        }

    }

    //轻松读卡,返回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];//卡数据缓冲
        byte[] myblockdata=new byte[16];//第三块数据

        EditText ctruid=findViewById(R.id.TextViewuid);
        ctruid.setText("");
        TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);
        ctr8h10d.setText("");
        tv.setText("");

        myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN;        //控制字指定,控制字的含义请查看本公司网站提供的动态库说明

        Spinner ctrauthmode=findViewById(R.id.SpinnerInOutKey);
        if (ctrauthmode.getSelectedItemId()==1){
            myctrlword=(byte)(myctrlword + EXTERNKEY);        //选用外部密钥认证
        }

        Spinner spls= findViewById(R.id.SpinnerArea);
        myareano = (byte)(spls.getSelectedItemId());          //指定区号

        Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);
        authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证

        EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥
        String authkeystr=ctrauthkey.getText().toString().trim();
        if (authkeystr.length()!=12){
            tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");
            return;
        }
        for (int i=0;i<6;i++){
            mypicckey[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
        }

        status = ourmifare.piccreadexfm11rf32(myctrlword,mypiccserial,myareano,authmode,mypicckey,mypiccdata);
        if(status == 0)
        {
            ourmifare.pcdbeep(38);
            String serialnumber = "";
            for (int i = 0; i < 4; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                serialnumber = serialnumber + bytestr.substring(bytestr.length() - 2, bytestr.length());
            }
            serialnumber=serialnumber.toUpperCase();
            ctruid.setText(serialnumber);

            long cardhao;
            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
            strls1=strls1.substring(strls1.length()-10,strls1.length());
            ctr8h10d.setText(strls1);

            String strls = "读取扇区数据成功!16进制卡号为:" +serialnumber+"\n转8H10D卡号:"+ strls1;
            tv.setText(strls);

            strls= "";
            for(int i = 0;i < 16;i++)
            {
                strls1 = "00"+Integer.toHexString(mypiccdata[i] & 0xFF);
                strls = strls + strls1.substring(strls1.length()-2) +" ";
            }
            EditText ctrblock0=findViewById(R.id.Edittextblock0);
            ctrblock0.setText(strls.toUpperCase());

            strls= "";
            for(int i = 16;i < 32;i++)
            {
                strls1 = "00"+Integer.toHexString(mypiccdata[i] & 0xFF);
                strls = strls + strls1.substring(strls1.length()-2) +" ";
            }
            EditText ctrblock1=findViewById(R.id.Edittextblock1);
            ctrblock1.setText(strls.toUpperCase());

            strls= "";
            for(int i = 32;i < 48;i++)
            {
                strls1 = "00"+Integer.toHexString(mypiccdata[i] & 0xFF);
                strls = strls + strls1.substring(strls1.length()-2) +" ";
            }
            EditText ctrblock2=findViewById(R.id.Edittextblock2);
            ctrblock2.setText(strls.toUpperCase());

            status= ourmifare.piccread((byte)(myareano*4+3),myblockdata);       //读块第三块密钥块
            if (status==0){
                strls= "";
                for(int i = 0;i < 16;i++)
                {
                    strls1 = "00"+Integer.toHexString(myblockdata[i] & 0xFF);
                    strls = strls + strls1.substring(strls1.length()-2) +" ";
                }
                EditText ctrblock3=findViewById(R.id.Edittextblock3);
                ctrblock3.setText(strls.toUpperCase());
            }else{
                tv.setText( "读取扇区数据成功!16进制卡号为:" +serialnumber+"\n读第3块时错误:"+Integer.toString(status));
            }
        }
        else
        {
            PrintErrInf(status);
        }

        mypiccserial = null;
        mypicckey = null;
        mypiccdata = null;

    }
    //轻松读卡,返回4字节卡号及48字节扇区数据
    public void piccreadexevery200ms(View view)
    {
        if(((Button)findViewById(R.id.btnPiccreadex1)).getText() =="启动连续每隔0.2秒读一次卡")
        {
            ((Button)findViewById(R.id.btnPiccreadex1)).setText("停止连续读卡");
            timer = new Timer();
            task = new TimerTask() {
                @Override
                public void run() {
                    if(iscontinue) {
                        iscontinue = false;
                        piccreadex(view);
                        iscontinue = true;
                        proctimes++;
                        tv.setText(tv.getText() + "\n执行次数:" + Long.toString(proctimes));
                    }
                }
            };
            timer.schedule(task, 0,200);
        }
        else
        {
            ((Button)findViewById(R.id.btnPiccreadex1)).setText("启动连续每隔0.2秒读一次卡");
            if(timer != null) {
                timer.cancel();
                proctimes = 0;
            }
            timer = null;
        }
    }

    //轻松写卡: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];//卡数据缓冲

        EditText ctruid=findViewById(R.id.TextViewuid);
        ctruid.setText("");
        TextView ctr8h10d=findViewById(R.id.TextViewlabel8h10d);
        ctr8h10d.setText("");
        tv.setText("");

        myctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN;        //控制字指定,控制字的含义请查看本公司网站提供的动态库说明

        Spinner ctrauthmode=findViewById(R.id.SpinnerInOutKey);
        if (ctrauthmode.getSelectedItemId()==1){
            myctrlword=(byte)(myctrlword + EXTERNKEY);        //选用外部密钥认证
        }

        Spinner spls= findViewById(R.id.SpinnerArea);
        myareano = (byte)(spls.getSelectedItemId());          //指定区号

        Spinner crtauthabkey=findViewById(R.id.SpinnerSelABKey);
        authmode =(byte)crtauthabkey.getSelectedItemId();    //大于0表示用A密码认证,推荐用A密码认证

        EditText ctrauthkey=findViewById(R.id.EdittextAuthkey); //认证密钥
        String authkeystr=ctrauthkey.getText().toString().trim();
        if (authkeystr.length()!=12){
            tv.setText("认证密钥为12位16进制数,请输入正确的卡认证密钥!");
            return;
        }
        for (int i=0;i<6;i++){
            mypicckey[i]=(byte) Integer.parseInt(authkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
        }
        String[] strArr;
        EditText ctrblock0=findViewById(R.id.Edittextblock0);
        String blockstr0=ctrblock0.getText().toString().trim();
        strArr= blockstr0.split("\\ ");             /*分割接收到的数据后再分析、处理、返回指令 */
        if (strArr.length!=16){
            tv.setText("第0块写入数据不足,请输入32位16进制写入数据!");
            return;
        }

        EditText ctrblock1=findViewById(R.id.Edittextblock1);
        String blockstr1=ctrblock1.getText().toString().trim();
        strArr= blockstr1.split("\\ ");             /*分割接收到的数据后再分析、处理、返回指令 */
        if (strArr.length!=16){
            tv.setText("第1块写入数据不足,请输入32位16进制写入数据!");
            return;
        }

        EditText ctrblock2=findViewById(R.id.Edittextblock2);
        String blockstr2=ctrblock2.getText().toString().trim();
        strArr= blockstr2.split("\\ ");             /*分割接收到的数据后再分析、处理、返回指令 */
        if (strArr.length!=16){
            tv.setText("第2块写入数据不足,请输入32位16进制写入数据!");
            return;
        }

        String writedatastr=blockstr0+" "+blockstr1+" "+blockstr2;
        strArr= writedatastr.split("\\ ");
        for(int p=0;p

你可能感兴趣的:(IC读写器,18002295132,QQ:954486673,android,android,studio,M1,读写,IC卡,智能卡)