Android Question Mifare Classic card, read and write card's block 0 of sector 1

fabton1963

Member
Licensed User
Longtime User
Good morning everyone,
I have this problem where I need to read and write to the first page of the second sector of a Mifare Classic card. To read the tag, following the example posted by @Erel NFC - Reading and Writing, I used the TagTechnology library, but when I try to read the first page of sector 1, highlighted in red in the attached image, I get an error.
To access that page, I used page index 4 because I noticed that from index 0 to 3 I can read and write without any problems. Therefore, I suppose I need to tell the library to read the next sector of the card and then page 0, but I have no idea how to do it using the TagTechnology library. Also, not all the methods described in the Android documentation seem to work with the B4A porting.

photo_2023-04-21_22-29-52.jpg


sample code:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private nfc As NFC
    Private TagTech As TagTechnology
    
    ...
    ..
    ..
    
Sub Activity_Resume
    Try
        nfc.EnableForegroundDispatch
        LogColor("[activity Resume][nfc] [EnableForegroundDispatch] ",Colors.Magenta)
        Dim si As Intent = Activity.GetStartingIntent   
        If si.IsInitialized = False Or si = prevIntent Then Return       
        LogColor($"[activity resume][intent][new][${si.Action}]"$,Colors.Magenta)       
        prevIntent = si
        Dim tag As JavaObject
        If si.Action.EndsWith("TECH_DISCOVERED") Or si.Action.EndsWith("NDEF_DISCOVERED") Or si.Action.EndsWith("TAG_DISCOVERED") Then
            Dim techs As List = nfc.GetTechList(si)
            Log($"Techs: ${techs}"$)       
            Dim Id0() As  Byte
            Dim Id(8) As  String
            Dim jTagTech As JavaObject
            'NfcA
            If techs.IndexOf("android.nfc.tech.NfcA")> -1 Then
                TagTech.Initialize("TagTech", "android.nfc.tech.MifareClassic", si)
                TagTech.Connect
                jTagTech  = TagTech
                tag   = jTagTech.RunMethod("getTag", Null)
                Id0   = tag.RunMethod("getId", Null)
.....
.....
.....
Private Sub TagTech_Connected (Success As Boolean)
    If Success  Then
        TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyA", Array(sector,  key), 0)
....
....

Private Sub Authenticate_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    If Success then
        dim as int blockIndex = 1
        TagTech.RunAsync( "readBlock1","readBlock", Array(blockIndex), 0)
....
....
Private Sub readBlock1_RunAsync(Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to read block1. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        dim as int blockIndex = 2
        TagTech.RunAsync( "readBlock2","readBlock", Array(blockIndex), 0)
Every thingh works good until I use blockIndex = 4 and I try ro read the block using TagTech.RunAsync( "readBlock4","readBlock", Array(blockIndex), 0).

Any suggestions?
 

drgottjr

Expert
Licensed User
Longtime User
how did you authenticate for sector 0? just do the same thing for sector 1 and
reset the block index to 0. you have to authenticate for each sector.
 
Upvote 0

fabton1963

Member
Licensed User
Longtime User
Now I add autenthication for each sector and
sample code:
TagTech.RunAsync( "readBlock1","readBlock", Array(blockIndex), SectorIndex)
works good,

sector 1 use blocks from 4 to7 , sector 2 from 8 to 11 and so on.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
is something wrong? or is it resolved?
 
Upvote 0
Top