Android Studio安卓读写NFC Ntag标签源码

Android Studio安卓读写NFC Ntag标签源码_第1张图片

本示例使用的发卡器: https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-21818769070.11.3513789erHXVGx&id=615391857885



    

        

        

    

    


    

        

            
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.ourmifare;

public class NtagActivity extends AppCompatActivity {
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ntag);

        androidx.appcompat.widget.Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

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

        Spinner spls = findViewById(R.id.spin_seleopencloskey);
        spls.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();
    }


    public void RequestNtag(View view){
        byte status;                          //存放返回值
        byte[] mypiccserial = new byte[7];    //7字节卡号

        tv.setText("");
        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

        status = ourmifare.piccrequestul(mypiccserial);//& 0xff用于转为无符号行数据

        if (status == 0) {
            ourmifare.pcdbeep(38);
            String uidstr = "";
            for (int i = 0; i < 7; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) ;
            }
            ctruid.setText(uidstr);
            tv.setText("获取7字节卡序号:" + uidstr);
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void getversiontype(View view){
        byte status;                          //存放返回值
        byte[] mypiccserial = new byte[7];    //7字节卡号
        byte[] mypiccversiondata=new byte[8];

        tv.setText("");
        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

        status = ourmifare.piccrequestul(mypiccserial);

        if (status == 0) {
            String uidstr = "";
            for (int i = 0; i < 7; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) ;
            }
            ctruid.setText(uidstr);

            status = ourmifare.piccgetversionntag(mypiccversiondata);
            if (status == 0) {
                ourmifare.pcdbeep(38);
                String verdstr = "";
                for (int i = 0; i < 8; i++) {
                    String bytestr = "00" + Integer.toHexString(mypiccversiondata[i] & 0xff);
                    verdstr = verdstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";
                }
                tv.setText("读取卡片版本型号成功!"+"\n卡片序号:" + uidstr+"\n"+"版本型号:"+verdstr);
            }else {
                PrintErrInf(status);   //返回代码提示
            }
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void getcardsign(View view){
        byte status;                          //存放返回值
        byte[] mypiccserial = new byte[7];    //7字节卡号
        byte[] piccsigdata=new byte[32];

        tv.setText("");
        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

        status = ourmifare.piccrequestul(mypiccserial);

        if (status == 0) {
            String uidstr = "";
            for (int i = 0; i < 7; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) ;
            }
            ctruid.setText(uidstr);

            status = ourmifare.piccgetversionntag(piccsigdata);
            if (status == 0) {
                ourmifare.pcdbeep(38);
                String verdstr = "";
                for (int i = 0; i < 32; i++) {
                    String bytestr = "00" + Integer.toHexString(piccsigdata[i] & 0xff);
                    verdstr = verdstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";
                }
                tv.setText("读取卡片签名信息成功!"+"\n卡片序号:" + uidstr+"\n"+"签名信息:"+verdstr);
            }else {
                PrintErrInf(status);   //返回代码提示
            }
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }
    public void getcountervalue(View view){
        byte status;                          //存放返回值
        byte[] mypiccserial = new byte[7];    //7字节卡号
        byte[] mypicccntdata=new byte[3];

        tv.setText("");
        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

        status = ourmifare.piccrequestul(mypiccserial);

        if (status == 0) {
            String uidstr = "";
            for (int i = 0; i < 7; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) ;
            }
            ctruid.setText(uidstr);

            status = ourmifare.piccreadcntntag(mypicccntdata);
            if (status == 0) {
                ourmifare.pcdbeep(38);
                String verdstr = "";
                for (int i = 0; i < 3; i++) {
                    String bytestr = "00" + Integer.toHexString(mypicccntdata[i] & 0xff);
                    verdstr = verdstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";
                }
                tv.setText("读取卡片单向计数器值成功!"+"\n卡片序号:" + uidstr+"\n"+"计数器值:"+verdstr);
            }else {
                tv.setText("读取卡片单向计数器值失败!错误代码:"+Integer.toString(status)+"\n卡片序号:" + uidstr+"\n"+"可能是计数器功能尚未启用或卡本身不支持计数功能!");
            }
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void getStaticlock(View view){
        byte status;                          //存放返回值
        byte[] mypiccserial = new byte[7];    //7字节卡号
        byte[] mypiccdata=new byte[16];

        tv.setText("");
        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

        status = ourmifare.piccrequestul(mypiccserial);
        if (status == 0) {
            String uidstr = "";
            for (int i = 0; i < 7; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length());
            }
            ctruid.setText(uidstr);

            status = ourmifare.piccreadul((byte) 0,mypiccdata);
            if (status == 0) {
                ourmifare.pcdbeep(38);
                String verdstr = "";
                for (int i = 10; i < 12; i++) {
                    String bytestr = "00000000" + Integer.toBinaryString(mypiccdata[i] & 0xff);
                    verdstr = verdstr + bytestr.substring(bytestr.length() - 8, bytestr.length()) ;
                }
                EditText ctrstationlock=findViewById(R.id.edit_Staticlock);
                ctrstationlock.setText(verdstr);

                String tagtypestr="";
                switch(mypiccdata[14]){
                    case 0x12:
                        tagtypestr="Ntag213,144 Bytes";
                        break;
                    case 0x3e:
                        tagtypestr="Ntag215,504 Bytes";
                        break;
                    case 0x6D:
                        tagtypestr="Ntag216,888 Bytes";
                        break;
                    default:
                        tagtypestr="OtherNtag";
                        break;
                }
                tv.setText("读取卡片静态锁标识成功!"+"\n卡片序号:" + uidstr+"\n芯片类型:"+tagtypestr+"\n静态锁标识:"+verdstr);
            }else {
                PrintErrInf(status);   //返回代码提示
            }
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void Staticlock(View view){
        byte status;                          //存放返回值
        byte[] mypiccserial = new byte[7];    //7字节卡号
        byte[] mypiccdata=new byte[4];

        tv.setText("");
        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

        EditText ctrstaticlock=findViewById(R.id.edit_Staticlock);
        String staticlockstr=ctrstaticlock.getText().toString().trim();
        if (staticlockstr.length()!=16){
            tv.setText("静态锁标识输入错误,请输入16位2进制静态锁标识值!");
            return;
        }
        status = ourmifare.piccrequestul(mypiccserial);
        if (status == 0) {
            String uidstr = "";
            for (int i = 0; i < 7; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length());
            }
            ctruid.setText(uidstr);

            mypiccdata[0]=0;
            mypiccdata[1]=0;
            mypiccdata[2]=(byte)Integer.parseInt(staticlockstr.substring(0,8),2);
            mypiccdata[3]=(byte)Integer.parseInt(staticlockstr.substring(staticlockstr.length() - 8, staticlockstr.length()),2);
            status = ourmifare.picclockntag((byte) 0,mypiccdata);
            if (status == 0) {
                ourmifare.pcdbeep(38);
                tv.setText("卡片加入静态锁成功!"+"\n卡片序号:" + uidstr+"\n");
            }else {
                PrintErrInf(status);   //返回代码提示
            }
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }
    public void InitNtagCard(View view){
        byte status;                          //存放返回值
        byte[] mypicckey = new byte[4];      //4个字节的卡旧密码
        byte[] newPickey = new byte[44];     //4个字节的新密码
        byte[] mypiccserial = new byte[7];   //7字节卡号
        byte ReadNeedKey = 0;                //取值0读卡不需要认证密码,非0读卡也要认证密码
        byte myctrlword ;                    //取值0不需要认证密码,16要认证卡密码  对卡操作
        byte KeyErrNum=0;                    //对卡操作允许密码错误次数(0为不限次)
        byte[] mypiccdata=new byte[16];      //本次对卡的配置信息

        tv.setText("");

        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

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

        EditText ctrbeginblock=findViewById(R.id.edit_initbeginblock);
        byte beginblock=(byte) Integer.parseInt(ctrbeginblock.getText().toString().trim());

        EditText ctrliminub=findViewById(R.id.edit_limitauthfailed);
        KeyErrNum=(byte) Integer.parseInt(ctrliminub.getText().toString().trim());

        Spinner ctrreadkeyen=findViewById(R.id.spin_selereadauth);
        ReadNeedKey=(byte)ctrreadkeyen.getSelectedItemId();

        Spinner spls = findViewById(R.id.spin_seleopencloskey);
        if (spls.getSelectedItemId()==0){       //卡片开启密码保护功能,开启后要记住卡密码,否则卡报废!!!
            myctrlword=(byte)(myctrlword+4);
            mypiccdata[3]=beginblock;           //配置:密码保护起始页,要根据不同类型的卡来设置
            myctrlword=(byte)(myctrlword+1);

            mypiccdata[4]=(byte)(KeyErrNum % 8); //配置:最大卡密码认证错误次数
            if(ReadNeedKey>0){                   //配置:读卡时需要认证卡密码
                mypiccdata[4]=(byte)(mypiccdata[4]+(byte)(0x80));
            }
            myctrlword=(byte)(myctrlword+2);   //配置:启用计数器

            EditText ctrnewkey=findViewById(R.id.edit_newkey);
            String newkeystr=ctrnewkey.getText().toString().trim();
            if (newkeystr.length() != 8)
            {
                tv.setText("密钥是8位16进制数据,请输入正确的新密钥!");
                return;
            }else {
                for (int i = 0; i < 4; i++) {
                    newPickey[i] = (byte) Integer.parseInt(newkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
                    mypiccdata[8+i]= (byte) Integer.parseInt(newkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
                }
            }

            EditText ctrpack=findViewById(R.id.edit_packkey);
            String packkeystr=ctrpack.getText().toString().trim();
            if (packkeystr.length() != 4)
            {
                tv.setText("PACK密钥确认码是4位16进制数据,请输入正确的密钥确认码!");
                return;
            }else {
                for (int i = 0; i < 2; i++) {
                    mypiccdata[12+i] = (byte) Integer.parseInt(packkeystr.substring(i * 2, i * 2 + 2), 16);//只有用Integer.parseInt才能杜绝大于128时的错误
                }
            }
        }else{   //卡片取消密码保护功能
            mypiccdata[3]=(byte)(0xff);
            myctrlword=(byte)(myctrlword+1);
            myctrlword=(byte)(myctrlword+2);
        }

        status = ourmifare.piccinitntag(myctrlword,mypiccserial,mypicckey,mypiccdata) ;
        if (status == 0) {
            ourmifare.pcdbeep(38);
            String uidstr = "";
            for (int i = 0; i < 7; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xff);
                uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) ;
            }
            ctruid.setText(uidstr);
            tv.setText("设置卡密码及保护机制成功!"+"\n卡片序号:" + uidstr);
        } else {
            PrintErrInf(status);   //返回代码提示
        }
    }

    public void ReadNtagCard(View view){
        byte status=1;                       //存放返回值
        byte[] mypiccserial = new byte[7];   //7字节卡号
        byte[] mypicckey=new byte[4];        //4个字节的卡密码
        byte[] mypiccdata=new byte[48];      //48个字节读卡的数据缓冲
        byte myblockaddr;                    //读起始页地址
        byte myblocksize;                    //读卡页数
        byte myctrlword;                     //取值0不认证密码,16要认证卡密码

        tv.setText("");

        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

        EditText ctrrwdata=findViewById(R.id.edit_rwdata);
        ctrrwdata.setText("");

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

        EditText ctrblokadd=findViewById(R.id.edit_RWbeginblock);
        myblockaddr=(byte)Integer.parseInt(ctrblokadd.getText().toString().trim());

        EditText ctrbloknum=findViewById(R.id.edit_RWblocks);
        myblocksize=(byte)Integer.parseInt(ctrbloknum.getText().toString().trim());

        //使用循环的方式读取,每次读一个块,这样可以连续读取标签N个块数据
        if (myblocksize<1){
            tv.setText("请输入本次读写块数!");
            return;
        }
        int j=0;
        String readdatastr="";
        while (j12){
            tv.setText("读写块数必须>=1,13<=");
            return;
        }
        status = ourmifare.piccreadexntag(myctrlword,mypiccserial,mypicckey,myblockaddr,myblocksize,mypiccdata) ;
        if (status == 0) {
            ourmifare.pcdbeep(38);
            String uidstr = "";
            for (int i = 0; i < 7; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccserial[i] & 0xFF);
                uidstr = uidstr + bytestr.substring(bytestr.length() - 2, bytestr.length()) ;
            }
            ctruid.setText(uidstr);

            String databuf="";
            for (int i = 0; i < myblocksize*4; i++) {
                String bytestr = "00" + Integer.toHexString(mypiccdata[i] & 0xFF);
                databuf = databuf + bytestr.substring(bytestr.length() - 2, bytestr.length()) + " ";
            }
            ctrrwdata.setText(databuf);
            tv.setText("读取Ntag卡块数据成功!"+"\n卡片序号:" + uidstr);
        } else {
            PrintErrInf(status);   //返回代码提示
        }*/
    }

    public void WriteNtagCard(View view){
        byte status=1;                       //存放返回值
        byte[] mypiccserial = new byte[7];   //7字节卡号
        byte[] mypicckey=new byte[4];        //4个字节的卡密码
        byte[] mypiccdata=new byte[48];      //48个字节读卡的数据缓冲
        byte myblockaddr;                    //读起始页地址
        byte myblocksize;                    //读卡页数
        byte myctrlword;                     //取值0不认证密码,16要认证卡密码

        tv.setText("");

        TextView ctruid=findViewById(R.id.textViewuid);
        ctruid.setText("");

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

        EditText ctrblokadd=findViewById(R.id.edit_RWbeginblock);
        myblockaddr=(byte)Integer.parseInt(ctrblokadd.getText().toString().trim());

        EditText ctrbloknum=findViewById(R.id.edit_RWblocks);
        myblocksize=(byte)Integer.parseInt(ctrbloknum.getText().toString().trim());
        if (myblocksize<1){
            tv.setText("请输入本次读写块数!");
            return;
        }

        EditText ctrrwdata=findViewById(R.id.edit_rwdata);
        String rwdatahex=ctrrwdata.getText().toString().trim();
        String[] strArr = rwdatahex.split("\\ ");    /*分割接收到的数据后再分析、处理、返回指令*/
        if (strArr.length11){
            tv.setText("读写块数必须>=1,12<=");
            return;
        }
        for (int i=0;i

 

 

你可能感兴趣的:(IC读写器,18002295132,QQ:954486673,android,android,studio,NFC,ntag,安卓,读写)