forked from andras-adam/virtualtag
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
900a045
commit c1394b2
Showing
3 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/virtualtag/app/data/MifareClassicHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.virtualtag.app.data | ||
|
||
import android.nfc.tech.MifareClassic | ||
import android.util.Log | ||
import java.io.IOException | ||
|
||
class MifareClassicHelper(tag: MifareClassic) { | ||
val timeout: Int | ||
val maxTransceiveLength: Int | ||
val size: Int | ||
val type: Int | ||
val sectorCount: Int | ||
val blockCount: Int | ||
val data: String | ||
|
||
init { | ||
timeout = tag.timeout | ||
maxTransceiveLength = tag.maxTransceiveLength | ||
size = tag.size | ||
type = tag.type | ||
sectorCount = tag.sectorCount | ||
blockCount = tag.blockCount | ||
data = readData(tag) | ||
} | ||
|
||
private fun readData(tag: MifareClassic): String { | ||
try { | ||
var dataAccumulator = "" | ||
// Connect to the tag | ||
tag.connect() | ||
// Loop through sectors on the tag | ||
for (currentSector in 0 until tag.sectorCount) { | ||
// Authenticate sector with the default key | ||
val auth = tag.authenticateSectorWithKeyA(currentSector, MifareClassic.KEY_DEFAULT) | ||
if (!auth) throw IOException("Failed to authenticate sector") | ||
// Get block count in sector | ||
val sectorBlockCount = tag.getBlockCountInSector(currentSector) | ||
// Loop through blocks in the sector | ||
for (currentBlockInSector in 0 until sectorBlockCount) { | ||
// Calculate the block's index | ||
val blockIndex = tag.sectorToBlock(currentSector) + currentBlockInSector | ||
// Read the block's data and store as HEX | ||
dataAccumulator += tag.readBlock(blockIndex).toHex() | ||
} | ||
} | ||
return dataAccumulator | ||
} catch (e: IOException) { | ||
e.localizedMessage?.let { Log.e("MifareClassicHelper", it) } | ||
return "" | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/virtualtag/app/data/MifareUltralightHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.virtualtag.app.data | ||
|
||
import android.nfc.tech.MifareUltralight | ||
import android.util.Log | ||
import java.io.IOException | ||
|
||
class MifareUltralightHelper(tag: MifareUltralight) { | ||
val type: Int | ||
val timeout: Int | ||
val maxTransceiveLength: Int | ||
val data: String | ||
|
||
init { | ||
type = tag.type | ||
timeout = tag.timeout | ||
maxTransceiveLength = tag.maxTransceiveLength | ||
data = readData(tag) | ||
} | ||
|
||
private fun readData(tag: MifareUltralight): String { | ||
try { | ||
var dataAccumulator = "" | ||
// Connect to the tag | ||
tag.connect() | ||
// Get the number of readable pages | ||
val readablePagesCount = when (type) { | ||
// Ultralight has 16 pages, all readable | ||
MifareUltralight.TYPE_ULTRALIGHT -> 16 | ||
// Ultralight C has 48 pages, but the last 4 are unreadable | ||
MifareUltralight.TYPE_ULTRALIGHT_C -> 44 | ||
else -> 0 | ||
} | ||
// Read every fourth page, as 4 pages are read at a time by `readPages()` | ||
for (currentPage in 0..readablePagesCount) { | ||
if (currentPage % 4 == 0) dataAccumulator += tag.readPages(currentPage).toHex() | ||
} | ||
return dataAccumulator | ||
} catch (e: IOException) { | ||
e.localizedMessage?.let { Log.e("MifareUltralightHelper", it) } | ||
return "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters