Android Question NFC - can I read tags on a health card?

dibesw

Active Member
Licensed User
I am trying to read an health insurance card.
I read many post in the forum before writing.

Screenshot_2020-04-20-01-38-04-587_com.nxp.taginfolite.jpg



B4X:
Sub Process_Globals
    Private prevIntent As Intent
End Sub

Sub Globals
    Private nfc As NFC
    Private TagTech As TagTechnology
    Private ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    ListView1.SingleLineLayout.Label.TextSize = 15
End Sub

Sub Activity_Resume
    'forces all nfc intents to be sent to this activity
    nfc.EnableForegroundDispatch
    Dim si As Intent = Activity.GetStartingIntent
    'check that the intent is a new intent
    If si.IsInitialized = False Or si = prevIntent Then Return
    prevIntent = si
    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)
        'in this case we are only accessing Ndef tags.
        If techs.IndexOf("android.nfc.tech.IsoDep") > -1 Then
            ''TagTech.Initialize("TagTech", "android.nfc.tech.NfcB" , si)
            TagTech.Initialize("TagTech", "android.nfc.tech.IsoDep", si)
            TagTech.Connect
            ToastMessageShow("IsoDep",True)
        End If
    End If

End Sub

Private Sub Format_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Writing completed. Success=${Success}, Flag=${Flag}"$)
End Sub

Private Sub TagTech_Connected (Success As Boolean)
    'ToastMessageShow($"Connected: ${Success}"$,"")
    Msgbox(Success,"")
    If Success = False Then
        ToastMessageShow("Error connecting to tag", True)
        Log(LastException)
    Else
        ReadIsoDep
    End If
End Sub
Private Sub ReadIsoDep
    TagTech.RunAsync("ReadIsoDep", "transceive", Array(Array As Byte(0xA2, 0x03, 0xE1, 0x10, 0x06, 0)), 0)
    'TagTech.RunAsync("ReadIsoDep", "getNdefMessage", Null, 0)
End Sub

Private Sub ReadIsoDep_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    ToastMessageShow("Reading completed. Success=" & Success & "," & "Flag=" & Flag,True)
    ListView1.Clear
    If Success Then
        If Result = Null Then
            ToastMessageShow("No records found.", False)
        Else
            Dim message As JavaObject = Result
            Dim records() As Object = message.RunMethod("getRecords", Null)
            For Each r As NdefRecord In records
                ListView1.AddSingleLine(r.GetAsTextType)
            Next
        End If
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    nfc.DisableForegroundDispatch
End Sub

B4X:
AddActivityText(Main,
<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
    android:resource="@xml/nfc_tech_filter" />)
CreateResource(xml, nfc_tech_filter.xml,
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
  <tech-list>
  <tech>android.nfc.tech.IsoDep</tech>
  </tech-list>
</resources>)

TagTech_Connected give success=true
but ReadIsoDep_RunAsync give success=false
B4A v9.30
JavaObject v2.05
NFC v2.01

Am I on the right way or not?
 

dibesw

Active Member
Licensed User
after repeated attempts I managed to get something written

Screenshot_2020-04-21-13-45-02-432_b4a.example.jpg


Now code is:

B4X:
Sub Activity_Resume
    'forces all nfc intents to be sent to this activity
    nfc.EnableForegroundDispatch
    Dim si As Intent = Activity.GetStartingIntent
    'check that the intent is a new intent
    If si.IsInitialized = False Or si = prevIntent Then Return
    prevIntent = si
    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)
        If techs.IndexOf("android.nfc.tech.IsoDep") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.IsoDep", si)
            TagTech.Connect
        End If
    End If
End Sub
Private Sub Format_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Writing completed. Success=${Success}, Flag=${Flag}"$)
End Sub
Private Sub TagTech_Connected (Success As Boolean)
    'ToastMessageShow(Success,False)
    If Success = False Then
        ToastMessageShow("Error connecting to tag", True)
    Else
        TagTech.RunAsync("ReadIsoDep", "transceive", Array(Array As Byte(0x3B, 0xFF, 0x18, 0x00, 0xFF, 0x81, 0x31, 0xFE, 0x55, 0x00, 0x6B, 0x02, 0x09, 0x02, 0x00, 0x01, 0x11, 0x01, 0x43, 0x53, 0x11, 0x31, 0x80, 0x8F)), 0)
        'TagTech.RunAsync("ReadIsoDep", "transceive", Array(Array As Byte(0x90, 0x5A, 0x00, 0x00, 0x03, 0xC0, 0x8E, 0xF4, 0x00)), 0)
        Wait For ReadIsoDep_RunAsync (Flag As Int, Success As Boolean, Result As Object)
        If Success Then
            'Dim message As JavaObject = Result
            'Dim records() As Object = message.RunMethod("getRecords", Null)
            'For Each r As NdefRecord In records
             '    Dim b() As Byte = r.GetPayload
            '    ListView1.AddSingleLine(BytesToString(b, 0, b.Length, "utf8"))
            'Next
            Dim rawId() As Byte = Result
            Dim bc As ByteConverter
            ListView1.AddSingleLine(bc.HexFromBytes(rawId))
        End If
    End If
End Sub

TagInfo give this

Screenshot_2020-04-21-13-40-22-359_com.nxp.taginfolite.jpg


How can bring out the same result?
I think the problem is in the commands in hexadecimal in TagTech.RunAsync.
I have found this FileSystem for this card but it's all very complicated
 
Last edited:
Upvote 0
Top