Android Studio读写低频RFID T5557卡源码

Android Studio读写低频RFID T5557卡源码_第1张图片

本示例使用的发卡器:https://item.taobao.com/item.htm?id=675212889085&spm=a1z10.5-c.w4002-21818769070.13.21166f89nKgnJ7 



    

        

        

    

    


    

        

            

            

            



            

            



            

            

            
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.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.reader.ouridr;
import com.reader.ourmifare;
public class T5557Activity extends AppCompatActivity {
    private TextView tv;
    private byte NEEDSERIAL = 0x01;   //需要只对指定系列号的卡操作
    private byte NEEDKEY = 0x02;      //需要用密码认证
    private byte LOCKBIT = 0x04;      //锁定配置块或数据块,仅对   t5557_init,t5557_write ,t5557_changekey函数有效
    private byte KEYENABLE = 0x08;    //启用本卡的密码功能
    private byte RESETCARD = 0x10;    //操作成功后重启卡片

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

        tv = findViewById(R.id.sample_text);
        tv.setText("操作结果");
    }
    @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();
    }

    public void textclear5557(View view)
    {
        TextView tvls;
        tvls = findViewById(R.id.TextViewRetDataT5557);
        tvls.setText("");
    }

    public void default5557(View view)
    {
        EditText etls;
        CheckBox cbls;
        etls = findViewById(R.id.editTextConfigWordT5557);
        etls.setText("00088028");

        cbls = findViewById(R.id.chkAOR);//AOR请求应答模式
        cbls.setChecked(false);
        cbls = findViewById(R.id.chkFrameEnd);//帧终结符
        cbls.setChecked(true);
        cbls = findViewById(R.id.chkPwdSetT5557);//启用密码读写功能,并设密码为
        cbls.setChecked(false);
    }

    public void updateset5557(View view)
    {
        CheckBox cbls;
        CheckBox cbAOR;
        CheckBox cbPWD;

        EditText etls;
        byte[] Configdata = new byte[4];//配置值

        etls = findViewById(R.id.editTextConfigWordT5557);
        String strls = etls.getText().toString().trim();
        if(strls.length() < 8){
            strls = "00088028";
            Configdata[0] = 0x00;
            Configdata[1] = 0x08;
            Configdata[2] = (byte)0x80;
            Configdata[3] = 0x28;
        }
        else {
            for (byte i = 0; i < 4; i++) {
                Configdata[i] = (byte)Integer.parseInt(strls.substring(i * 2, i * 2 + 2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
        }
        
        cbAOR = findViewById(R.id.chkAOR);//AOR请求应答模式
        cbPWD = findViewById(R.id.chkPwdSetT5557);//启用密码读写功能,并设密码为
        if(cbAOR.isChecked()) {
            if(!cbPWD.isChecked()) {//密码没启用,AOR无效
                cbAOR.setChecked(false);
                Configdata[2] &= 0xfd;
                tv.setText("密码没启用,AOR无效");
            }
            else {
                Configdata[2] |= 0x02;
            }
        }
        else {
            Configdata[2] &= 0xfd;
        }

        cbls = findViewById(R.id.chkFrameEnd);//帧终结符
        if(cbls.isChecked()) {
            Configdata[3] |= 0x08;
        }
        else {
            Configdata[3] &= 0xf7;
        }

        if(cbPWD.isChecked()) {
            if ((Configdata[3] & 0xe0) > (6 * 32)) {//启用密码功能后,最大块不能为7
                Configdata[3] &= 0x1f;
                Configdata[3] |=(6 * 32);
            }
            Configdata[3] |= 0x10;//启用密码功能
        }
        else{
            Configdata[3] &= 0xef;//取消密码功能
            Configdata[2] &= 0xfd;
            cbAOR.setChecked(false);
        }

        etls.setText(String.format("%02X%02X%02X%02X",Configdata[0],Configdata[1],Configdata[2],Configdata[3]));

    }
    public void changepwd5557(View view)
    {
        byte i;
        byte j;
        byte status;//存放返回值

        byte myctrlword;//控制字

        byte[] oldpicckey = new byte[4];//密码
        byte[] mypiccserial = new byte[6];//卡序列号
        byte[] newpicckey = new byte[4];//指定写哪一块

        myctrlword = 0; //NEEDSERIAL:需要只对指定系列号的卡操作,NEEDKEY:需要用密码认证,LOCKBIT:锁定块,KEYENABLE:启用本卡的密码功能

        String strls;

        CheckBox cbls;
        EditText etls;

        cbls = findViewById(R.id.chkPwdT5557);
        if(cbls.isChecked()) {//本次操作需要密码验证
            etls = findViewById(R.id.editTextPwdT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 8)
            {
                tv.setText("旧密码长度不足8位");
                return;
            }
            for (i = 0; i < 4; i++) {
                oldpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.NEEDKEY;

        }

        cbls = findViewById(R.id.chkUidNeedT5557);
        if(cbls.isChecked()) {//仅操作指定卡号的卡
            etls = findViewById(R.id.editTextUidT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 12)
            {
                tv.setText("卡号长度不足12位");
                return;
            }

            for (i = 0; i < 6; i++) {
                mypiccserial[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.NEEDSERIAL;

        }

        etls = findViewById(R.id.editTextPwdNewT5557);
        strls = etls.getText().toString().trim();
        if(strls.length() < 8)
        {
            tv.setText("新密码长度不足8位!");
            return;
        }

        for (i = 0; i < 4; i++) {
            newpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
        }

        status = ouridr.t5557changekey(myctrlword,mypiccserial,oldpicckey,newpicckey);

        if(status == 0)
        {
            strls = "卡号[";
            strls += String.format("%02X%02X%02X%02X%02X%02X",mypiccserial[0],mypiccserial[1],mypiccserial[2],mypiccserial[3],mypiccserial[4],mypiccserial[5]);
            strls += "]";
            TextView tvls = findViewById(R.id.TextViewRetDataT5557);
            tvls.setText(strls);
            strls = "更改T5557卡密码成功";
            ouridr.beep(38);
        }
        else {
            strls=GetErrInf(status);
        }
        tv.setText(strls);

    }
    public void read5557(View view)
    {
        byte i;
        byte status;//存放返回值

        byte myctrlword;//控制字

        byte[] oldpicckey = new byte[4];//密码
        byte[] mypiccserial = new byte[6];//卡序列号
        byte[] mypiccdata = new byte[64];//读卡数据缓冲:卡无线转输分频比、卡内容长度(字节数),及最多返回12块的数据
        byte[] mypiccblockflag = new byte[2];//指定读哪一块

        myctrlword = 0; //NEEDSERIAL:需要只对指定系列号的卡操作,NEEDKEY:需要用密码认证,LOCKBIT:锁定块,KEYENABLE:启用本卡的密码功能

        String strls;

        CheckBox cbls;
        EditText etls;

        cbls = findViewById(R.id.chkPwdT5557);
        if(cbls.isChecked()) {//本次操作需要密码验证
            etls = findViewById(R.id.editTextPwdT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 8)
            {
                tv.setText("密码长度不足8位");
                return;
            }
            for (i = 0; i < 4; i++) {
                oldpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.NEEDKEY;

        }

        cbls = findViewById(R.id.chkUidNeedT5557);
        if(cbls.isChecked()) {//仅操作指定卡号的卡
            etls = findViewById(R.id.editTextUidT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 12)
            {
                tv.setText("卡号长度不足12位");
                return;
            }

            for (i = 0; i < 6; i++) {
                mypiccserial[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.NEEDSERIAL;

        }

        //操作块标志
        //第0页的块
        mypiccblockflag[0] = 0;

        cbls = findViewById(R.id.chkB0T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 1;
        }

        cbls = findViewById(R.id.chkB1T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 2;
        }

        cbls = findViewById(R.id.chkB2T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 4;
        }

        cbls = findViewById(R.id.chkB3T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 8;
        }

        cbls = findViewById(R.id.chkB4T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 16;
        }

        cbls = findViewById(R.id.chkB5T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 32;
        }

        cbls = findViewById(R.id.chkB6T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 64;
        }

        cbls = findViewById(R.id.chkB7T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 128;
        }

        //第1页
        mypiccblockflag[1] = 0;

        cbls = findViewById(R.id.chkB11T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[1] += 2;
        }

        cbls = findViewById(R.id.chkB12T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[1] += 4;
        }

        cbls = findViewById(R.id.chkB13T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[1] += 8;
        }

        cbls = findViewById(R.id.chkB14T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[1] += 16;
        }

        status = ouridr.t5557read(myctrlword,mypiccserial,oldpicckey,mypiccblockflag,mypiccdata);

        if(status == 0)
        {
            strls = "卡无线转输分频比[" + Integer.toString(mypiccdata[0]) + "],卡号[";
            strls += String.format("%02X%02X%02X%02X%02X%02X",mypiccserial[0],mypiccserial[1],mypiccserial[2],mypiccserial[3],mypiccserial[4],mypiccserial[5]);
            strls += "],卡数据[";
            for (i = 0; i < mypiccdata[1]; i++) {
                strls += String.format("%02X",mypiccdata[i+2]);
            }
            strls += "]";

            TextView tvls = findViewById(R.id.TextViewRetDataT5557);
            tvls.setText(strls);
            strls = "读T5557卡成功";
            ouridr.beep(38);
        }
        else {
            strls=GetErrInf(status);
        }
        tv.setText(strls);
    }
    public void write5557(View view)
    {
        byte i;
        byte j;
        byte status;//存放返回值

        byte myctrlword;//控制字

        byte[] oldpicckey = new byte[4];//密码
        byte[] mypiccserial = new byte[6];//卡序列号
        byte[] mypiccdata = new byte[64];//写入数据缓冲:最多12块的数据
        byte[] mypiccblockflag = new byte[2];//指定写哪一块

        myctrlword = 0; //NEEDSERIAL:需要只对指定系列号的卡操作,NEEDKEY:需要用密码认证,LOCKBIT:锁定块,KEYENABLE:启用本卡的密码功能

        String strls;

        CheckBox cbls;
        EditText etls;

        cbls = findViewById(R.id.chkPwdT5557);
        if(cbls.isChecked()) {//本次操作需要密码验证
            etls = findViewById(R.id.editTextPwdT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 8)
            {
                tv.setText("密码长度不足8位");
                return;
            }
            for (i = 0; i < 4; i++) {
                oldpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.NEEDKEY;

        }

        cbls = findViewById(R.id.chkUidNeedT5557);
        if(cbls.isChecked()) {//仅操作指定卡号的卡
            etls = findViewById(R.id.editTextUidT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 12)
            {
                tv.setText("卡号长度不足12位");
                return;
            }

            for (i = 0; i < 6; i++) {
                mypiccserial[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.NEEDSERIAL;

        }

        //操作块标志
        j = 0;
        //第0页的块
        mypiccblockflag[0] = 0;

        cbls = findViewById(R.id.chkB0T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 1;
            j++;
        }

        cbls = findViewById(R.id.chkB1T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 2;
            j++;
        }

        cbls = findViewById(R.id.chkB2T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 4;
            j++;
        }

        cbls = findViewById(R.id.chkB3T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 8;
            j++;
        }

        cbls = findViewById(R.id.chkB4T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 16;
            j++;
        }

        cbls = findViewById(R.id.chkB5T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 32;
            j++;
        }

        cbls = findViewById(R.id.chkB6T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 64;
            j++;
        }

        cbls = findViewById(R.id.chkB7T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[0] += 128;
            j++;
        }

        //第1页
        mypiccblockflag[1] = 0;

        cbls = findViewById(R.id.chkB11T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[1] += 2;
            j++;
        }

        cbls = findViewById(R.id.chkB12T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[1] += 4;
            j++;
        }

        cbls = findViewById(R.id.chkB13T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[1] += 8;
            j++;
        }

        cbls = findViewById(R.id.chkB14T5557);
        if(cbls.isChecked()) {
            mypiccblockflag[1] += 16;
            j++;
        }

        //写卡数据准备
        if(j == 0)
        {
            tv.setText("请先选择需要写入的块");
            return;
        }
        etls = findViewById(R.id.editTextDataT5557);
        strls = etls.getText().toString().trim();
        if(strls.length() < (j*8))
        {
            tv.setText("写入数据长度不足,请补足数据!");
            return;
        }

        for (i = 0; i < (j*4); i++) {
            mypiccdata[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
        }

        status = ouridr.t5557write(myctrlword,mypiccserial,oldpicckey,mypiccblockflag,mypiccdata);
        if(status == 0) {
            strls = "卡号[";
            strls += String.format("%02X%02X%02X%02X%02X%02X", mypiccserial[0], mypiccserial[1], mypiccserial[2], mypiccserial[3], mypiccserial[4], mypiccserial[5]);
            strls += "]";
            TextView tvls = findViewById(R.id.TextViewRetDataT5557);
            tvls.setText(strls);
            strls = "写T5557卡成功";
            ouridr.beep(38);
        }else {
            strls=GetErrInf(status);
        }
            tv.setText(strls);
    }


    public void init5557(View view)
    {
        byte i;
        byte status;//存放返回值

        byte myctrlword;//控制字

        byte[] oldpicckey = new byte[4];//密码
        byte[] mypiccserial = new byte[6];//卡序列号
        byte[] mypiccdata = new byte[4];//写入数据缓冲
        byte[] newpicckey = new byte[4];//指定写哪一块

        myctrlword = 0; //NEEDSERIAL:需要只对指定系列号的卡操作,NEEDKEY:需要用密码认证,LOCKBIT:锁定块,KEYENABLE:启用本卡的密码功能

        String strls;

        CheckBox cbls;
        EditText etls;

        cbls = findViewById(R.id.chkPwdT5557);
        if(cbls.isChecked()) {//本次操作需要密码验证
            etls = findViewById(R.id.editTextPwdT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 8)
            {
                tv.setText("密码长度不足8位");
                return;
            }
            for (i = 0; i < 4; i++) {
                oldpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.NEEDKEY;

        }

        cbls = findViewById(R.id.chkPwdSetT5557);
        if(cbls.isChecked()) {//启用密码读写功能
            etls = findViewById(R.id.editTextPwdSetT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 8)
            {
                tv.setText("新密码长度不足8位");
                return;
            }
            for (i = 0; i < 4; i++) {
                newpicckey[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.KEYENABLE;

        }
        else {
            newpicckey[0] = 0x00;
            newpicckey[1] = 0x00;
            newpicckey[2] = 0x00;
            newpicckey[3] = 0x00;
        }

        cbls = findViewById(R.id.chkUidNeedT5557);
        if(cbls.isChecked()) {//仅操作指定卡号的卡
            etls = findViewById(R.id.editTextUidT5557);
            strls = etls.getText().toString().trim();
            if(strls.length() < 12)
            {
                tv.setText("卡号长度不足12位");
                return;
            }

            for (i = 0; i < 6; i++) {
                mypiccserial[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
            }
            myctrlword += ouridr.NEEDSERIAL;

        }

        //配置值:
        etls = findViewById(R.id.editTextConfigWordT5557);
        strls = etls.getText().toString().trim();
        if(strls.length() < 8)
        {
            tv.setText("配置值输入长度不足8位");
            return;
        }
        for (i = 0; i < 4; i++) {
            mypiccdata[i] = (byte)Integer.parseInt(strls.substring(i*2,i*2+2),16);//只有用Integer.parseInt才能杜绝大于128时的错误
        }

        status = ouridr.t5557init(myctrlword,mypiccserial,oldpicckey,mypiccdata,newpicckey);
        if(status == 0) {
            strls = "卡号[";
            strls += String.format("%02X%02X%02X%02X%02X%02X",mypiccserial[0],mypiccserial[1],mypiccserial[2],mypiccserial[3],mypiccserial[4],mypiccserial[5]);
            strls += "]";
            TextView tvls = findViewById(R.id.TextViewRetDataT5557);
            tvls.setText(strls);
            strls = "配置T5557卡成功";
            ouridr.beep(38);
        }
        else {
            strls=GetErrInf(status);
        }
        tv.setText(strls);
    }

    public void t5557to4100(View view){
        byte[] myuidbuf=new byte[7];
        byte[] oldpicckey=new byte[4];
        byte[] newpicckey=new byte[4];
        byte[] mypiccserial=new byte[6];
        byte myctrlword=RESETCARD;

        myuidbuf[0]=47;
        myuidbuf[1]=1;
        myuidbuf[2]=2;
        myuidbuf[3]=3;
        myuidbuf[4]=4;

        byte status=ouridr.t5557to4100(myctrlword,mypiccserial,oldpicckey,newpicckey,myuidbuf);
        if (status==0){
            ouridr.beep(38);

            tv.setText("T5557卡配置成ID卡成功!");
        }else{
            tv.setText("错误代码:"+Integer.toString(status));
        }
    }

    public String GetErrInf(byte errcode) {
        String dispstr="";
        switch(errcode){
            case 1:
                dispstr="错误代码:1,写入配置的值不正确,配置区或密码区可能已被锁定,请重新写入!";
                break;
            case 2:
                dispstr="错误代码:2,本卡尚未开启密码功能,函数myctrlword中无需加入NEEDKEY!";
                break;
            case 3:
                dispstr="错误代码:3,需要指定密码,函数myctrlword要加入NEEDKEY!";
                break;
            case 5:
                dispstr="错误代码:5,密码错误!";
                break;
            case 8:
                dispstr="错误代码:8,卡不在感应区 或 密码不正确,请重新拿开卡后再放到感应区!";
                break;
            case 23:
                dispstr="错误代码:23,发卡器未连接!";
                break;
            default:
                dispstr="未知错误,错误代码:"+Integer.toString(errcode);
                break;
        }
       return dispstr;
    }

}

 

 

你可能感兴趣的:(18002295132,QQ:954486673,android,安卓,低频,RFID,t5557,读写,java)