Android Question MifareClassic

Pantelis Loizou

Member
Licensed User
Hi Erel

Thanks for your reply. I've already read that thread but i am facing some problems reading block sectors. i will appreciated for any help. below is my code. Reading success is always false

B4X:
Sub Activity_Resume
    nfc.EnableForegroundDispatch
    Dim si As Intent = Activity.GetStartingIntent
    If si.IsInitialized = False Or si = prevIntent Then Return
    prevIntent = si
    If si.Action.EndsWith("TECH_DISCOVERED") Or si.Action.EndsWith("TAG_DISCOVERED") Then
        Dim techs As List = nfc.GetTechList(si)
        Log($"Techs: ${techs}"$)
        If techs.IndexOf("android.nfc.tech.MifareUltralight") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.MifareUltralight" , si)
            TagTech.Connect
        Else If techs.IndexOf("android.nfc.tech.MifareClassic") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.MifareClassic" , si)
            TagTech.Connect
        Else
            ToastMessageShow("Tag does not support Ndef.", True)
        End If
    End If
End Sub


Private Sub TagTech_Connected (Success As Boolean)
    Dim key(6) As Byte = Array As Byte(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5)  ' Fake key for the forum
    Dim sector As Int=0
    TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyA", Array( sector, key ), 0)
    
'    Log($"Connected: ${Success}"$)
'    If Success = False Then
'        ToastMessageShow("Error connecting to tag", True)
'        Log(LastException)
'    Else
'        ReadNdef
'    End If
End Sub

Private Sub Authenticate_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to auth. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        Dim blockIndex As Int=0
        TagTech.RunAsync("readBlock","", Array(blockIndex), 0)
    End If
End Sub

Private Sub readBlock_RunAsync(Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to read. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        If Result<>Null Then
            Dim myBlock() As Byte = Result
        End If
    End If
End Sub
 

Erel

Administrator
Staff member
Licensed User
Where are the logs?
Is Success True in TagTech_Connected ?
 

Pantelis Loizou

Member
Licensed User
Logs below
B4X:
Finished trying to auth. Success=true, Flag=0
B4X:
Finished trying to read. Success=false, Flag=0
B4X:
Techs: [android.nfc.tech.NfcA, android.nfc.tech.MifareClassic, android.nfc.tech.NdefFormatable]
 

Erel

Administrator
Staff member
Licensed User
Tip:
B4X:
 Dim key() As Byte = Array As Byte(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5) 'the array length is not needed
What happens if you change this key to something wrong? Does 'Success' become false?
 

Pantelis Loizou

Member
Licensed User
I TRY IT BUT NO RESULT
B4X:
Private Sub TagTech_Connected (Success As Boolean)
    Dim key() As Byte = Array As Byte(0xA1,0xA2,0xA3,0xA4,0xA5,0xA6)
    Dim sector As Int=0
    TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyA", Array(sector, key), 0)
End Sub

Private Sub Authenticate_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to auth. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        Dim blockIndex As Int=0
        TagTech.RunAsync("ReadBlock","readBlock",Array(blockIndex),0)
    End If
End Sub

Private Sub ReadBlock_RunAsync(Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to read. Success=${Success}, Flag=${Flag}"$)
    Log(LastException)
    If Success Then
        If Result<>Null Then
            Dim myBlock() As Byte = Result
        End If
    End If
End Sub
 

JordiCP

Well-Known Member
Licensed User
Perhaps the used key has no read permissions (just a guess)

So, even if you can authenticate a sector with a key, it does not grant that you can read it.

Have you checked that the card is readable using that key with an external Mifare app?
 

Pantelis Loizou

Member
Licensed User
hi again

i am using mifare classic tool to read a card and attached you may find two different screenshots with results. i am trying also to read sector 5 which includes data but unfortunately i cannot. only sector 0 is readable. i spend more than a month to find a solution but nothing yet. what i am doing wrong? for sector 0 i use 0xA0A1A2A3A4A5 and for sector 5 i use 0xFFFFFFFFFF. Could somebody help me. i will appreciate it very much
 

Attachments

Top