Код: Выделить всё
private lateinit var pendingIntent: PendingIntent
private lateinit var nfcAdapter: NfcAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nfcAdapter = NfcAdapter.getDefaultAdapter(this)
if (nfcAdapter == null) {
Log.e(TAG, "NFC not supported on this device.")
return
}
val intent = Intent(this, javaClass).apply {
addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
}
pendingIntent = PendingIntent.getActivity(
this, 0, intent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0
)
}
override fun onResume() {
super.onResume()
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null)
}
override fun onPause() {
super.onPause()
nfcAdapter.disableForegroundDispatch(this)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
if (intent.action == NfcAdapter.ACTION_TAG_DISCOVERED ||
intent.action == NfcAdapter.ACTION_TECH_DISCOVERED ||
intent.action == NfcAdapter.ACTION_NDEF_DISCOVERED
) {
val tag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getParcelableExtra(NfcAdapter.EXTRA_TAG, Tag::class.java)
} else {
@Suppress("DEPRECATION")
intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)
}
tag?.let { handleNfcTag(it) }
}
}
private fun handleNfcTag(tag: Tag) {
val tagId = tag.id
Log.d(TAG, "NFC Tag ID: ${bytesToHex(tagId)}")
when {
IsoDep.get(tag) != null -> handleIsoDep(tag)
NfcA.get(tag) != null -> handleNfcA(tag)
MifareClassic.get(tag) != null -> handleMifareClassic(tag)
else -> Log.d(TAG, "Unsupported tag technology")
}
}
private fun handleIsoDep(tag: Tag) {
IsoDep.get(tag)?.use { isoDep ->
try {
isoDep.connect()
Log.d(TAG, "Connected to IsoDep tag")
val selectAidCommand = byteArrayOf(0x00.toByte(), 0xA4.toByte(), 0x04.toByte(), 0x00.toByte(), 0x07.toByte(), 0xA0.toByte(), 0x00.toByte(), 0x00.toByte(), 0x02.toByte(), 0x47.toByte(), 0x10.toByte(), 0x01.toByte())
val response = isoDep.transceive(selectAidCommand)
readBinaryData(isoDep)
readRecordData(isoDep)
Log.d(TAG, "IsoDep Response: ${bytesToHex(response)}")
Toast.makeText(this, "IsoDep Response: ${bytesToHex(response)}", Toast.LENGTH_LONG).show()
} catch (e: Exception) {
Log.e(TAG, "Error communicating with IsoDep tag: ${e.message}")
}
}
}
private fun readBinaryData(isoDep: IsoDep) {
for (i in 0..20) {
val command = byteArrayOf(0x00.toByte(), 0xB0.toByte(), i.toByte(), 0x00.toByte(), 0x10.toByte())
try {
val response = isoDep.transceive(command)
Log.d(TAG, "Read Binary Page $i: ${bytesToHex(response)}")
} catch (e: Exception) {
Log.d(TAG, "Error reading binary page $i: ${e.message}")
break
}
}
}
private fun readRecordData(isoDep: IsoDep) {
for (i in 1..10) { // Try reading first 10 records
val command = byteArrayOf(0x00.toByte(), 0xB2.toByte(), i.toByte(), 0x04.toByte(), 0x00.toByte())
try {
val response = isoDep.transceive(command)
Log.d(TAG, "Read Record $i: ${bytesToHex(response)}")
} catch (e: Exception) {
Log.d(TAG, "Error reading record $i: ${e.message}")
break // Stop if we encounter an error
}
}
}
private fun handleNfcA(tag: Tag) {
NfcA.get(tag)?.use { nfcA ->
try {
nfcA.connect()
Log.d(TAG, "Connected to NfcA tag")
val atqa = nfcA.atqa
val sak = nfcA.sak
Log.d(TAG, "NfcA ATQA: ${bytesToHex(atqa)}, SAK: $sak")
Toast.makeText(this, "NfcA ATQA: ${bytesToHex(atqa)}, SAK: $sak", Toast.LENGTH_LONG).show()
} catch (e: Exception) {
Log.e(TAG, "Error communicating with NfcA tag: ${e.message}")
}
}
}
private fun handleMifareClassic(tag: Tag) {
MifareClassic.get(tag)?.use { mifare ->
try {
mifare.connect()
Log.d(TAG, "Connected to MifareClassic tag")
val type = when (mifare.type) {
MifareClassic.TYPE_CLASSIC -> "Classic"
MifareClassic.TYPE_PLUS -> "Plus"
MifareClassic.TYPE_PRO -> "Pro"
else -> "Unknown"
}
val sectorCount = mifare.sectorCount
Log.d(TAG, "MifareClassic Type: $type, Sector Count: $sectorCount")
Toast.makeText(this, "MifareClassic Type: $type, Sector Count: $sectorCount", Toast.LENGTH_LONG).show()
} catch (e: Exception) {
Log.e(TAG, "Error communicating with MifareClassic tag: ${e.message}")
}
}
}
private fun bytesToHex(bytes: ByteArray): String {
return bytes.joinToString("") { "%02x".format(it) }
}
companion object {
private const val TAG = "NfcReader"
}
Код: Выделить всё
Connected to IsoDep tag IsoDep Response: 9000Подробнее здесь: https://stackoverflow.com/questions/790 ... error-6982