투케이2K

483. (Android/Java) [android 12 / target 31] nfcAdapter.enableReaderMode , mifareClassic 마이페어 블럭 읽기 본문

Android

483. (Android/Java) [android 12 / target 31] nfcAdapter.enableReaderMode , mifareClassic 마이페어 블럭 읽기

투케이2K 2023. 2. 14. 20:53

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

import android.app.PendingIntent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.os.Build;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Arrays;

public class A_Intro extends AppCompatActivity {



    /**
     * // --------------------------------------------------------------------------------------
     * TODO [클래스 설명]
     * // --------------------------------------------------------------------------------------
     * 1. 인트로 로딩 화면 액티비티
     * // --------------------------------------------------------------------------------------
     * */





    /**
     * // --------------------------------------------------------------------------------------
     * TODO [빠른 로직 찾기 : 주석 로직 찾기]
     * // --------------------------------------------------------------------------------------
     * // [SEARCH FAST] : []
     * // --------------------------------------------------------------------------------------
     */





    // -----------------------------------------------------------------------------------------
    // TODO [컴포넌트 선언]
    // -----------------------------------------------------------------------------------------





    // -----------------------------------------------------------------------------------------
    // TODO [전역 변수]
    // -----------------------------------------------------------------------------------------
    private static final String ACTIVITY_NAME = "A_Intro";

    private NfcAdapter nfcAdapter = null; // [NfcAdapter]
    private PendingIntent pendingIntent = null; // [PendingIntent]





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onCreate]
    // -----------------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            // [타이틀바 없애기 : FEATURE_NO_TITLE]
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        // [액티비티 레이아웃 지정 실시]
        setContentView(R.layout.activity_a_intro);
        Log.i("---","---");
        Log.d("//===========//","================================================");
        Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onCreate() :: 액티비티 수행 실시]");
        Log.d("//===========//","================================================");
        Log.i("---","---");

        // -------------------------------------
        // [로직 처리 실시]
        // -------------------------------------
        try {

            /**
             * ---------------------------------
             * [필요 퍼미션 등록]
             * ---------------------------------
             * <uses-permission android:name="android.permission.NFC"/>
             * <uses-feature android:name="android.hardware.nfc" android:required="true" />
             * ---------------------------------
             * */


            // [NFC 사용 여부 및 활성 상태 확인]
            if (C_StateCheck.getNfcState(A_Intro.this) == true){
                Log.i("---","---");
                Log.w("//===========//","================================================");
                Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onCreate() :: NfcAdapter.getDefaultAdapter 지정]");
                Log.w("//===========//","================================================");
                Log.i("---","---");

                // [NFC 어댑터 지정 실시]
                nfcAdapter = NfcAdapter.getDefaultAdapter(this);

            }
            else {
                Log.i("---","---");
                Log.e("//===========//","================================================");
                Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onCreate() :: NFC 사용 여부 및 활성 상태 확인 실패]");
                Log.e("//===========//","================================================");
                Log.i("---","---");
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }


    } // TODO [메인 종료]





    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onResume]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onResume(){
        Log.i("---","---");
        Log.d("//===========//","================================================");
        Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onResume() :: 액티비티 실행 준비]");
        Log.d("//===========//","================================================");
        Log.i("---","---");
        try {
            // [NFC 리더 모드 등록]
            if (nfcAdapter != null) {
                Bundle options = new Bundle();
                options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 250);

                nfcAdapter.enableReaderMode(this,
                        new A_Intro().new NfcReaderClass(), // [콜백 클래스 등록]

                        NfcAdapter.FLAG_READER_NFC_A |
                                NfcAdapter.FLAG_READER_NFC_B |
                                NfcAdapter.FLAG_READER_NFC_F |
                                NfcAdapter.FLAG_READER_NFC_V |
                                NfcAdapter.FLAG_READER_NFC_BARCODE |
                                NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
                        options);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        super.onResume();
    }




    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onPause]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onPause(){
        super.onPause();
        Log.i("---","---");
        Log.e("//===========//","================================================");
        Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onPause() :: 액티비티 정지 상태]");
        Log.e("//===========//","================================================");
        Log.i("---","---");
        try {
            // [NFC 리더 모드 해제]
            if (nfcAdapter != null) {
                nfcAdapter.disableReaderMode(this);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }




    // -----------------------------------------------------------------------------------------
    // TODO [액티비티 onDestroy]
    // -----------------------------------------------------------------------------------------
    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.i("---","---");
        Log.e("//===========//","================================================");
        Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onDestroy() :: 액티비티 종료 상태]");
        Log.e("//===========//","================================================");
        Log.i("---","---");
    }





    // -----------------------------------------------------------------------------------------
    // TODO [모바일 키 이벤트 발생 체크 부분]
    // -----------------------------------------------------------------------------------------
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // [모바일 디바이스의 뒤로가기 키 이벤트가 발생한 경우]
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.i("---","---");
            Log.w("//===========//","================================================");
            Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onKeyDown() :: 백버튼 터치시 뒤로 가기 이벤트 발생]");
            Log.w("//===========//","================================================");
            Log.i("---","---");

            // ------------------------------------
            // [액티비티 종료 실시]
            finish();
            overridePendingTransition(0, 0);
            // ------------------------------------
        }
        return true;
    }





    // -----------------------------------------------------------------------------------------
    // TODO [내부 클래스 생성 : 콜백 등록]
    // -----------------------------------------------------------------------------------------
    class NfcReaderClass implements NfcAdapter.ReaderCallback {

        @Override
        public void onTagDiscovered(Tag tag) {
            Log.i("---","---");
            Log.w("//===========//","================================================");
            Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onTagDiscovered() :: start]");
            Log.w("//===========//","================================================");
            Log.i("---","---");
            try {
                if (tag != null){

                    // [print info]
                    byte[] tagId = tag.getId();

                    MifareClassic mifareClassic = MifareClassic.get(tag);
                    mifareClassic.connect();

                    int sample_sector_num = 12; // [샘플]
                    byte sample_read_key [] = C_Encryption.hexStringToByteArray("0xad 0x87 0xad 0x64 0xad 0x67"); // [샘플]

                    byte read_block[] = null;
                    if (mifareClassic.authenticateSectorWithKeyA(sample_sector_num, sample_read_key)){
                        Log.i("---","---");
                        Log.w("//===========//","================================================");
                        Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onTagDiscovered() :: A_Key 인증 성공]");
                        Log.w("//===========//","================================================");
                        Log.i("---","---");

                        // [블럭 데이터 읽기]
                        int sectorToBlock = mifareClassic.sectorToBlock(sample_sector_num);
                        read_block = mifareClassic.readBlock(sectorToBlock);
                    }
                    else {
                        Log.i("---","---");
                        Log.e("//===========//","================================================");
                        Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onTagDiscovered() :: A_Key 인증 실패]");
                        Log.e("//===========//","================================================");
                        Log.i("---","---");
                    }

                    Log.i("---","---");
                    Log.w("//===========//","================================================");
                    Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onTagDiscovered() :: NFC INFO]");
                    Log.i("","\n"+"[tagId [HEX] :: "+C_Encryption.byteToHex(tagId)+"]");
                    Log.i("","\n"+"[tagId [Byte] :: "+Arrays.toString(tagId)+"]");
                    Log.i("","\n"+"[mifareClassic [getType] :: "+String.valueOf(mifareClassic.getType())+"]");
                    Log.i("","\n"+"[mifareClassic [getBlockCount] :: "+String.valueOf(mifareClassic.getBlockCount())+"]");
                    Log.i("","\n"+"[mifareClassic [getSectorCount] :: "+String.valueOf(mifareClassic.getSectorCount())+"]");
                    Log.i("","\n"+"[mifareClassic [getSize] :: "+String.valueOf(mifareClassic.getSize())+"]");
                    Log.i("","\n"+"[read_block [HEX] :: "+String.valueOf(C_Encryption.byteToHex(read_block))+"]");
                    Log.i("","\n"+"[read_block [Byte] :: "+Arrays.toString(read_block)+"]");
                    Log.w("//===========//","================================================");
                    Log.i("---","---");

                    mifareClassic.close();
                }
                else {
                    Log.i("---","---");
                    Log.e("//===========//","================================================");
                    Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onTagDiscovered() :: NFC TAG IS NULL]");
                    Log.e("//===========//","================================================");
                    Log.i("---","---");
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }


} // TODO [클래스 종료]
 

[결과 출력]

 

W///===========//: ================================================
I/: [A_Intro >> onTagDiscovered() :: NFC INFO]
I/: [tagId [HEX] :: 0xd7 0x1d 0x4d 0x3c]
I/: [tagId [Byte] :: [-41, 29, 77, 60]]
I/: [mifareClassic [getType] :: 0]
I/: [mifareClassic [getBlockCount] :: 64]
I/: [mifareClassic [getSectorCount] :: 16]
I/: [mifareClassic [getSize] :: 1024]
I/: [read_block [HEX] :: 0x2e 0xad 0x3e 0x8d 0xad 0xcd 0xd1 0x4d 0xc7 0xad 0x0e 0x23 0xe7 0xad 0x57 0x92]
I/: [read_block [Byte] :: [46, -59, 62, -115, 9, -51, -47, 77, -57, 97, 14, 35, -25, 74, 87, -110]]
W///===========//: ================================================

 

반응형
Comments