1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question MifareClassic

Discussion in 'Android Questions' started by Pantelis Loizou, Jul 29, 2019.

  1. Pantelis Loizou

    Pantelis Loizou Member Licensed User

    Hi to everyone

    is there are any examples how to get records from mifareclassic tech using NFC library?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Pantelis Loizou

    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

    Code:
    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(6As 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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Where are the logs?
    Is Success True in TagTech_Connected ?
     
  5. Pantelis Loizou

    Pantelis Loizou Member Licensed User

    Logs below
    Code:
    Finished trying to auth. Success=true, Flag=0
    Code:
    Finished trying to read. Success=false, Flag=0
    Code:
    Techs: [android.nfc.tech.NfcA, android.nfc.tech.MifareClassic, android.nfc.tech.NdefFormatable]
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Tip:
    Code:
    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?
     
  7. Pantelis Loizou

    Pantelis Loizou Member Licensed User

    what i have to it? whatever i put the Success becomes False
     
  8. Pantelis Loizou

    Pantelis Loizou Member Licensed User

    Sorry. i change it to but i still get the same logs result

    Code:
    Dim key() As Byte = Array As Byte(0,1,2,3,4,6)
     
  9. Pantelis Loizou

    Pantelis Loizou Member Licensed User

    what java method should i use for MifareClassic
    Code:
    TagTech.RunAsync("readBlock",""Array(blockIndex), 0)
     
  10. JordiCP

    JordiCP Well-Known Member Licensed User

    Try with
    Code:
    TagTech.RunAsync("ReadBlock","readBlock",Array(blockIndex),0)
     
  11. Pantelis Loizou

    Pantelis Loizou Member Licensed User

    I TRY IT BUT NO RESULT
    Code:
    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
     
  12. Pantelis Loizou

    Pantelis Loizou Member Licensed User

    Code:
    (IOException) java.io.IOException: Transceive failed
     
  13. JordiCP

    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?
     
    f0raster0 and Erel like this.
  14. Pantelis Loizou

    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
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice