Skip to content

Commit

Permalink
NFC READ 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
sShrimPs committed Apr 3, 2023
1 parent b6f7530 commit 8187625
Show file tree
Hide file tree
Showing 23 changed files with 165 additions and 69 deletions.
8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
}

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
namespace 'com.example.dice'
Expand All @@ -26,6 +27,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand All @@ -51,6 +55,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.preference:preference-ktx:1.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand All @@ -61,8 +66,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.21"
implementation "com.naver.maps:map-sdk:3.16.1"
implementation 'com.google.android.gms:play-services-location:17.0.0'
//네이버 로그인
implementation 'com.navercorp.nid:oauth-jdk8:5.1.0' // jdk 8
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.0.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
//결제
implementation 'io.github.bootpay:android:4.2.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.dice
package com.dice.dice

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.dice">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-feature
android:name="android.hardware.nfc"
android:required="false" />

<application
android:allowBackup="true"

Expand Down Expand Up @@ -41,6 +36,11 @@
<activity
android:name=".SubActivity4"
android:exported="true" />
<activity
android:name=".cardread"
android:exported="true">
</activity>

<activity
android:name=".SubActivity3"
android:exported="true">
Expand Down
26 changes: 0 additions & 26 deletions app/src/main/java/com/example/dice/cardread.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.navercorp.nid.NaverIdLoginSDK
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_sub5.*
import retrofit2.Call
import retrofit2.Response
import retrofit2.Retrofit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.dice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.dice.R
import kotlinx.android.synthetic.main.activity_parkinfo.*

class ParkinfoActivity : AppCompatActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,21 @@ import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.example.dice.databinding.ActivitySub5Binding
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import kotlinx.android.synthetic.main.activity_sub1.*
import kr.co.bootpay.android.Bootpay
import kr.co.bootpay.android.events.BootpayEventListener
import kr.co.bootpay.android.models.BootExtra
import kr.co.bootpay.android.models.BootItem
import kr.co.bootpay.android.models.BootUser
import kr.co.bootpay.android.models.Payload
import java.util.Scanner
import kotlinx.android.synthetic.main.activity_sub5.*
import kotlinx.android.synthetic.main.activity_sub3.*
import kotlinx.android.synthetic.main.activity_pointcharge.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import kotlin.properties.Delegates

class PointchargeActivity : AppCompatActivity() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.example.dice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.dice.R

class PointrenewalActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import com.naver.maps.map.overlay.Marker
import com.naver.maps.map.overlay.Overlay
import com.naver.maps.map.overlay.InfoWindow
import com.naver.maps.map.util.FusedLocationSource
import com.naver.maps.map.util.MarkerIcons
import android.content.ClipboardManager
import android.view.View
import android.content.ClipData
import com.example.dice.R
import kotlinx.android.synthetic.main.activity_sub1.*
class SubActivity1 : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mapView: MapView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_sub3.*
import kotlinx.android.synthetic.main.activity_sub3.edit_id
import kotlinx.android.synthetic.main.activity_sub3.edit_pw
import kotlinx.android.synthetic.main.activity_sub4.*
import kotlinx.android.synthetic.main.activity_sub5.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import retrofit2.Call
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.dice

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.example.dice.databinding.ActivitySub5Binding
import kotlinx.android.synthetic.main.activity_sub1.*
import kotlinx.android.synthetic.main.activity_sub5.*
import kotlinx.android.synthetic.main.activity_sub3.*
import kotlinx.android.synthetic.main.activity_pointcharge.*
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
Expand Down
120 changes: 120 additions & 0 deletions app/src/main/kotlin/com/example/dice/cardread.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.example.dice

import android.app.PendingIntent
import android.content.Intent
import android.content.IntentFilter
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.nfc.TagLostException
import android.nfc.tech.MifareClassic
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_cardread.*

class cardread : AppCompatActivity(), NfcAdapter.ReaderCallback {

private var nfcAdapter: NfcAdapter? = null
var uid:String = ""

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cardread)

nfcAdapter = NfcAdapter.getDefaultAdapter(this) ?: run {
Toast.makeText(this, "이 장치는 nfc를 지원하지 않습니다.", Toast.LENGTH_LONG).show()
finish()
return
}

if (!nfcAdapter?.isEnabled!!) {
Toast.makeText(this, "NFC가 활성화 되어 있지 않습니다.", Toast.LENGTH_SHORT).show()
enableNfcSetting()
} else {
Toast.makeText(this, "NFC가 활성화 되었습니다.", Toast.LENGTH_SHORT).show()
}

returnbtn.setOnClickListener {
val intents = Intent(this, SubActivity5::class.java)
intent.apply {
this.putExtra("uid", uid)
}
startActivity(intents)
}
}

private fun enableNfcSetting() {
// NFC 설정 화면으로 이동하는 인텐트 생성
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startActivity(Intent(Settings.Panel.ACTION_NFC))
} else {
startActivity(Intent(Settings.ACTION_NFC_SETTINGS))
}
}

override fun onResume() {
super.onResume()
nfcAdapter?.let {
val techList = arrayOf(arrayOf(MifareClassic::class.java.name))
it.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A or NfcAdapter.FLAG_READER_NFC_B or NfcAdapter.FLAG_READER_NFC_F or NfcAdapter.FLAG_READER_NFC_V, null)
}
}

override fun onPause() {
super.onPause()
nfcAdapter?.disableReaderMode(this)
}
override fun onTagDiscovered(tag: Tag?) {
try {
val mifareClassic = MifareClassic.get(tag)
mifareClassic?.let {
if (!it.isConnected) {
it.connect()
}
val sectorCount = it.sectorCount
for (i in 0 until sectorCount) {
val auth = it.authenticateSectorWithKeyA(i, MifareClassic.KEY_DEFAULT)
if (auth) {
val blockCount = it.getBlockCountInSector(i)
for (j in 0 until blockCount) {
val blockIndex = it.sectorToBlock(i) + j
val data = it.readBlock(blockIndex)
Log.d("결과", "Sector $i, Block $j, Data: ${bytesToHexString(data)}")
}
}
}
uid = bytesToHexString(it.tag.id)
it.close()
runOnUiThread {
Toast.makeText(this, "UID: $uid", Toast.LENGTH_LONG).show()
taguidTextView.setText(uid)
Log.d("uid", "UID IS $uid")
}
} ?: run {
Log.d("결과", "Tag is null")
runOnUiThread {
Toast.makeText(this@cardread, "카드를 다시 접촉해주세요.", Toast.LENGTH_SHORT).show()
}

}
} catch (e: TagLostException) {
runOnUiThread {
Toast.makeText(this@cardread, "카드를 다시 접촉해주세요.", Toast.LENGTH_SHORT).show()
}
}
}

private fun bytesToHexString(bytes: ByteArray?): String {
if (bytes == null) {
return ""
}
val builder = StringBuilder()
for (b in bytes) {
builder.append(String.format("%02x", b))
}
return builder.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.example.dice

import com.google.gson.JsonObject
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.POST

interface memberid {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.example.dice
import com.google.gson.JsonObject
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST

interface milesService {
Expand Down
25 changes: 13 additions & 12 deletions app/src/main/res/layout/activity_cardread.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
android:layout_height="match_parent"
tools:context=".cardread">


<TextView
android:id="@+id/taguidTextView"
android:layout_width="363dp"
android:layout_height="74dp"
android:layout_marginTop="500dp"
app:layout_constraintBottom_toTopOf="@+id/returnbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />

<Button
android:id="@+id/returnbtn"
android:layout_width="wrap_content"
Expand All @@ -18,17 +30,6 @@
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/nfcread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="44dp"
android:text="NFC 읽기"
app:layout_constraintBottom_toTopOf="@+id/returnbtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />


</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit 8187625

Please sign in to comment.