天天看點

安卓Android下如何開發USB NFC讀寫器APP

        榮士第二代USB免驅動IC讀寫器IC-02支援Windows、安卓(Android)、Linux系統,為友善IC-02讀寫器能快速的接入安卓(Android)系統,我們提供了各種安卓版本的So庫及示例源碼,安卓工程師可直接拷貝以下代碼使用到項目中。

安卓Android下如何開發USB NFC讀寫器APP

Android Studio源碼:

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

繼續閱讀