Android Question [solved] NFC Tag (not NDEF) reading

udg

Expert
Licensed User
Longtime User
Hi all,
I tried to modify Erel's original code in order to access an internal code on a Tag sent to me by a partner.
They call it SID (Siemens SID from ScryptCard), but I wasn't able to find any doc about it.
As far as technologies, UID, ATQA and SAK are concerned I read the same values as the NFC Tool app does.

When it comes to discover the SID I receive a Success = false reply. Below my code (I tried both NfcA and IsoDep):

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)
       Log($"Techs: ${techs}"$)
       If techs.IndexOf("android.nfc.tech.NfcA") > -1 Then
           TagTech.Initialize("TagTech", "android.nfc.tech.NfcA" , si)
           'Get the UID
           Dim jo As JavaObject = TagTech
           Dim tag As JavaObject = jo.RunMethod("getTag", Null)
           Dim rawId() As Byte = tag.RunMethod("getId", Null)
           Dim bc As ByteConverter
           Log(bc.HexFromBytes(rawId)) 
           'Connect to the tag
           TagTech.Connect
       Else
           ToastMessageShow("Tag does not support Ndef/NfcA/IsoDep...", True)
       End If
   End If
End Sub

Private Sub TagTech_Connected (Success As Boolean)
   Log($"Connected: ${Success}"$)
   If Success = False Then
       ToastMessageShow("Error connecting to tag", True)
       Log(LastException)
   Else
       If rdbRead.Checked Then
           'after connection
           TagTech.RunAsync("TT", "transceive", Array(Array As Byte(0x90, 0x5A, 0x00, 0x00, 0x03, 0xC0, 0x8E, 0xF4, 0x00)), 0)
           Wait For TT_RunAsync (Flag As Int, Success As Boolean, Result As Object)
           Log(Success)
           TagTech.RunAsync("TT", "transceive", Array(Array As Byte(0x90, 0xBD, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00)), 0)
           Wait For TT_RunAsync (Flag As Int, Success As Boolean, Result As Object)
           Log(Success)
           TagTech.Close
       Else
           WriteNdef(Array(nfc.CreateMimeRecord("text/plain", flTxt1.Text.GetBytes("UTF8")), _
                     nfc.CreateMimeRecord("text/plain", flTxt2.Text.GetBytes("UTF8"))))
       End If
   End If
End Sub
Command codes appearing in the transceive commands are those that the partner sent to me. First one is to get an Application_ID while the second one is to get the SID.

NFC Tools identifies my tag as an ISO 14443-4, NXP MIFARE DESFire / NXP MIFARE DESFIRE EV1.

Anyone worked with tags like this one before? Any hint? TIA
 

udg

Expert
Licensed User
Longtime User
Unbelievable, now everything seems to work properly (ok, more or less).
I substituted IsoDep again to NfcA and now it works (not exactly as I expected, but at least it seems that I can read the value I was looking for).
Here's the updated code:
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)
       Log($"Techs: ${techs}"$)
       If techs.IndexOf("android.nfc.tech.IsoDep") > -1 Then
           TagTech.Initialize("TagTech", "android.nfc.tech.IsoDep" , si)
           'Dim jo As JavaObject = TagTech
           'Dim tag As JavaObject = jo.RunMethod("getTag", Null)
           'Dim rawId() As Byte = tag.RunMethod("getId", Null)
           'Dim bc As ByteConverter
           'Log(bc.HexFromBytes(rawId)) 'bc = ByteConverter
           'Connect to the tag
           TagTech.Connect
       Else
           ToastMessageShow("Tag does not support Ndef.", True)
       End If
   End If
End Sub

Private Sub TagTech_Connected (Success As Boolean)
   Log($"Connected: ${Success}"$)
   If Success = False Then
       ToastMessageShow("Error connecting to tag", True)
       Log(LastException)
   Else
       Log("isConnected: "&TagTech.Connected)     'not needed
       If rdbRead.Checked Then
           'Get APPLICATION_ID
           TagTech.RunAsync("TT", "transceive", Array(Array As Byte(0x90, 0x5A, 0x00, 0x00, 0x03, 0xC0, 0x8E, 0xF4,0)), 0)
           Wait For TT_RunAsync (Flag As Int, Success As Boolean, Result As Object)
           Log(Success)
           If Success Then
               Dim rawId() As Byte = Result
               Dim bc As ByteConverter
               Log(bc.HexFromBytes(rawId))
           End If
           'GET SID
           TagTech.RunAsync("TT", "transceive", Array(Array As Byte(0x90, 0xBD, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00)), 0)
           Wait For TT_RunAsync (Flag As Int, Success As Boolean, Result As Object)
           Log(Success)
           If Success Then
               Dim rawId() As Byte = Result
               Dim bc As ByteConverter
               Log(bc.HexFromBytes(rawId))
           End If
           TagTech.Close
       Else
           WriteNdef(Array(nfc.CreateMimeRecord("text/plain", flTxt1.Text.GetBytes("UTF8")), _
                     nfc.CreateMimeRecord("text/plain", flTxt2.Text.GetBytes("UTF8"))))
       End If
   End If
End Sub
The "Get SID" command returns 16 hex values plus 2 status codes (91 00).
The specific tag I'm testing on returns:
8B1F06003030303430313239319112409100
where the bold chars are the SID in a somewhat reverse order (I expected 40 12 91 because that's the value printed on the tag itself).

If testing with other tags confirms the above I can finally close the subject .. fingers crossed..

Edit: they confirmed that any tested tag shows the correct code.
 
Last edited:
Upvote 0
Top