Notice
Recent Posts
Recent Comments
Link
투케이2K
591. (Android/Java) [android 13 / target 33] mifareClassic 마이페어 A Key 키 권한 확인, sector 섹터 번호 block 읽기 본문
Android
591. (Android/Java) [android 13 / target 33] mifareClassic 마이페어 A Key 키 권한 확인, sector 섹터 번호 block 읽기
투케이2K 2023. 7. 1. 11:37[개발 환경 설정]
개발 툴 : AndroidStudio
[소스 코드]
package com.example.javaproject;
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_Mifare extends AppCompatActivity {
/**
* // --------------------------------------------------------------------------------------
* TODO [클래스 설명]
* // --------------------------------------------------------------------------------------
* 1. 인트로 로딩 화면 액티비티
* // --------------------------------------------------------------------------------------
* */
/**
* // --------------------------------------------------------------------------------------
* TODO [빠른 로직 찾기 : 주석 로직 찾기]
* // --------------------------------------------------------------------------------------
* // [SEARCH FAST] : []
* // --------------------------------------------------------------------------------------
*/
// -----------------------------------------------------------------------------------------
// TODO [컴포넌트 선언]
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
// TODO [전역 변수]
// -----------------------------------------------------------------------------------------
private static final String ACTIVITY_NAME = "A_Mifare";
private NfcAdapter nfcAdapter = null; // [NfcAdapter]
private PendingIntent pendingIntent = null; // [PendingIntent]
// -----------------------------------------------------------------------------------------
// TODO [액티비티 onCreate]
// -----------------------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ===============================================================
S_Log._D_("액티비티 수행 실시", null);
// ===============================================================
// ---------------------------------------------------------------
// [타이틀 바 없애기]
// ---------------------------------------------------------------
C_App.setTitleBarRemove(A_Mifare.this);
// ---------------------------------------------------------------
// [액티비티 레이아웃 지정 실시]
// ---------------------------------------------------------------
setContentView(R.layout.activity_a_intro);
// -------------------------------------
// [로직 처리 실시]
// -------------------------------------
try {
/**
* ---------------------------------
* [필요 퍼미션 등록]
* ---------------------------------
* <uses-permission android:name="android.permission.NFC"/>
* <uses-feature android:name="android.hardware.nfc" android:required="true" />
* ---------------------------------
* */
// [NFC 사용 여부 및 활성 상태 확인]
if (C_StateCheck.getNfcState(A_Mifare.this) == true){
S_Log._W_("NFC 활성 상태 :: NfcAdapter.getDefaultAdapter 지정", null);
// [NFC 어댑터 지정 실시]
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
else {
S_Log._E_("NFC 비활성 상태", null);
}
}
catch (Exception e){
e.printStackTrace();
}
} // TODO [메인 종료]
// -----------------------------------------------------------------------------------------
// TODO [액티비티 onResume]
// -----------------------------------------------------------------------------------------
@Override
public void onResume(){
// ===============================================================
S_Log._D_("액티비티 실행 준비", null);
// ===============================================================
try {
// [NFC 리더 모드 등록]
if (nfcAdapter != null) {
Bundle options = new Bundle();
options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 250);
nfcAdapter.enableReaderMode(this,
new A_Mifare().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();
// ===============================================================
S_Log._E_("액티비티 정지 상태", null);
// ===============================================================
try {
// [NFC 리더 모드 해제]
if (nfcAdapter != null) {
nfcAdapter.disableReaderMode(this);
}
}
catch (Exception e){
e.printStackTrace();
}
}
// -----------------------------------------------------------------------------------------
// TODO [액티비티 onDestroy]
// -----------------------------------------------------------------------------------------
@Override
public void onDestroy(){
super.onDestroy();
// ===============================================================
S_Log._E_("액티비티 종료 상태", null);
// ===============================================================
}
// -----------------------------------------------------------------------------------------
// 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) {
// ===============================================================
S_Log._D_("onTagDiscovered :: 이벤트 감지", null);
// ===============================================================
try {
if (tag != null){
// [print info]
byte[] tagId = tag.getId();
String hexTag = C_Encryption.byteToHex(tagId);
// ===============================================================
S_Log._W_("tagId 정보 확인", new String[]{
"Byte :: " + Arrays.toString(tagId),
"Hex :: " + hexTag
});
// ===============================================================
// [MifareClassic 연결]
MifareClassic mifareClassic = MifareClassic.get(tag);
mifareClassic.connect();
// [섹터 값 읽기]
boolean auth = false;
int bCount = 0;
int bIndex = 0;
byte[] bData = null;
int sectorCount = mifareClassic.getSectorCount();
if (sectorCount >= 0){
for (int i=0; i<sectorCount; i++){
// [권한 체크]
auth = mifareClassic.authenticateSectorWithKeyA(i, MifareClassic.KEY_DEFAULT);
if(auth){ // [권한 확인]
bCount = mifareClassic.getBlockCountInSector(i);
bIndex = 0;
for(int j = 0; j < bCount; j++){
bIndex = mifareClassic.sectorToBlock(j);
try {
bData = mifareClassic.readBlock(bIndex);
}
catch (Exception e){
bData = null;
}
if (bData != null && bData.length>0){
String bHex = C_Encryption.byteToHex(bData);
S_Log._W_("섹터 블럭 정보 확인", new String[]{
"authenticateSectorWithKeyA :: " + String.valueOf(i),
"blockIndex :: " + String.valueOf(bIndex),
"blockData :: " + String.valueOf(Arrays.toString(bData)),
"blockHex :: " + String.valueOf(bHex)
});
}
}
}
}
}
// [마이페어 연결 종료]
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/: [LOG :: CLASS PLACE :: com.example.javaproject.A_Mifare$NfcReaderClass.onTagDiscovered(A_Mifare.java:290)]
I/: ----------------------------------------------------
I/: [LOG :: DESCRIPTION :: 섹터 블럭 정보 확인]
I/: ----------------------------------------------------
I/: [LOG :: authenticateSectorWithKeyA :: 15]
I/: ----------------------------------------------------
I/: [LOG :: blockIndex :: 4]
I/: ----------------------------------------------------
I/: [LOG :: blockData :: [16, -78]]
I/: ----------------------------------------------------
I/: [LOG :: blockHex :: 0x10 0xb2]
W///===========//: ================================================
반응형
'Android' 카테고리의 다른 글
Comments