Android Question cannot read and write to NFC card

Cnrez

Member
Licensed User
Longtime User
hi all,
i want to read and write to mifare desfire 4k card
it support tech :
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]

i want to use android.nfc.tech.NdefFormatable
i modified the sample project, but it didn't work,

there is nothing happened.
no errors, Reading completed. Success=false, Flag=0, Writing completed. Success=false, Flag=0

here is the log :

B4X:
Logger connected to:  CipherLab CipherLab RS30
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
Connected: true
Reading completed. Success=false, Flag=0
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
Connected: true
Reading completed. Success=false, Flag=0

please help.
regards

here is my project
 

Attachments

  • advanced_NFC_ndefformatable.zip
    9 KB · Views: 691

Cnrez

Member
Licensed User
Longtime User
Ok, i have tried using NXP TagWritter App
when i do 'Erase and Format as NDEF' , result is success
and when to write tags, also success.
but with my code , cannot read and write.

note : this card is not support NDEF tech, but IsoDep, NfcA and NdefFormatable

is my code above (attached in first post) is correct ?, and can be use for NdefFormatable tech ?
is tech beside NDEF can be used for read and write tags ? if can , i need an script examples.. please

attached is screenshot with NXP TagWritter and NXP Reader

regards
 

Attachments

  • Screenshot_2017-09-27-22-01-27.png
    Screenshot_2017-09-27-22-01-27.png
    262.8 KB · Views: 646
  • Screenshot_2017-09-27-21-57-17.png
    Screenshot_2017-09-27-21-57-17.png
    62.1 KB · Views: 1,010
  • Screenshot_2017-09-27-21-59-05.png
    Screenshot_2017-09-27-21-59-05.png
    54.5 KB · Views: 648
  • Screenshot_2017-09-27-21-59-14.png
    Screenshot_2017-09-27-21-59-14.png
    69.4 KB · Views: 640
  • Screenshot_2017-09-27-21-59-23.png
    Screenshot_2017-09-27-21-59-23.png
    67.8 KB · Views: 684
  • Screenshot_2017-09-27-21-59-32.png
    Screenshot_2017-09-27-21-59-32.png
    44.4 KB · Views: 628
  • Screenshot_2017-09-27-21-59-39.png
    Screenshot_2017-09-27-21-59-39.png
    61.6 KB · Views: 681
Upvote 0

Cnrez

Member
Licensed User
Longtime User
Which techs are listed after you write the data with this app?

B4X:
Log($"Techs: ${techs}"$)

the same,
B4X:
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Call WriteNdef after the connection:
B4X:
Private Sub WriteNdef (Records() As Object)
   Dim RecordsJO As JavaObject
   RecordsJO.InitializeArray("android.nfc.NdefRecord", Array(nfc.CreateUriRecord("https://www.b4x.com")))
   Dim message As JavaObject
   message.InitializeNewInstance("android.nfc.NdefMessage", Array(RecordsJO))
   TagTech.RunAsync("Format", "format", Array(message), 0)
   Wait For Format_RunAsync (Flag As Int, Success As Boolean, Result As Object)
   Log("format: " & Success)
   Log(Result)
End Sub
What is the output?
 
Upvote 0

Cnrez

Member
Licensed User
Longtime User
ok, here is my 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}"$)
        'in this case we are only accessing Ndef tags.
        If techs.IndexOf("android.nfc.tech.NdefFormatable") > -1 Then
            ''TagTech.Initialize("TagTech", "android.nfc.tech.NfcA" , si)
            'Connect to the tag
            ''TagTech.Connect
           
            TagTech.Initialize("TagTech", "android.nfc.tech.NdefFormatable" , si)
            TagTech.Connect
            WriteNdef(Array(nfc.CreateMimeRecord("text/plain", flTxt1.Text.GetBytes("UTF8")), _
                      nfc.CreateMimeRecord("text/plain", flTxt2.Text.GetBytes("UTF8"))))       
                   
        Else
            ToastMessageShow("Tag does not support Ndef.", True)
        End If
    End If
End Sub


Private Sub WriteNdef (Records() As Object)
    Dim RecordsJO As JavaObject
    RecordsJO.InitializeArray("android.nfc.NdefRecord", Array(nfc.CreateUriRecord("https://www.b4x.com")))
    Dim message As JavaObject
    message.InitializeNewInstance("android.nfc.NdefMessage", Array(RecordsJO))
    TagTech.RunAsync("Format", "format", Array(message), 0)
    Wait For Format_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log("format: " & Success)
    Log(Result)
End Sub

and here is the result (log)

B4X:
Logger connected to:  CipherLab CipherLab RS30
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
format: false
null
 
Upvote 0

Cnrez

Member
Licensed User
Longtime User
ok, like this

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}"$)
        'in this case we are only accessing Ndef tags.
        If techs.IndexOf("android.nfc.tech.NdefFormatable") > -1 Then
            ''TagTech.Initialize("TagTech", "android.nfc.tech.NfcA" , si)
            'Connect to the tag
            ''TagTech.Connect
           
            TagTech.Initialize("TagTech", "android.nfc.tech.NdefFormatable" , si)
            TagTech.Connect
                   
        Else
            ToastMessageShow("Tag does not support Ndef.", True)
        End If
    End If
End Sub


Private Sub WriteNdef (Records() As Object)
    Dim RecordsJO As JavaObject
    RecordsJO.InitializeArray("android.nfc.NdefRecord", Array(nfc.CreateUriRecord("https://www.b4x.com")))
    Dim message As JavaObject
    message.InitializeNewInstance("android.nfc.NdefMessage", Array(RecordsJO))
    TagTech.RunAsync("Format", "format", Array(message), 0)
    Wait For Format_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log("format: " & Success)
    Log(Result)
End Sub

Private Sub TagTech_Connected (Success As Boolean)
    Log($"Connected: ${Success}"$)
    WriteNdef(Array(nfc.CreateMimeRecord("text/plain", flTxt1.Text.GetBytes("UTF8")), _
                      nfc.CreateMimeRecord("text/plain", flTxt2.Text.GetBytes("UTF8"))))       
           
End Sub


the result :
B4X:
Logger connected to:  CipherLab CipherLab RS30
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
Connected: true
format: false
null
 
Upvote 0

Cnrez

Member
Licensed User
Longtime User
B4X:
Private Sub TagTech_Connected (Success As Boolean)
    Log($"Connected: ${Success}"$)
    If Success = False Then
        ToastMessageShow("Error connecting to tag", True)
        Log(LastException)
    Else
        WriteNdef(Array(nfc.CreateMimeRecord("text/plain", flTxt1.Text.GetBytes("UTF8")), _
                      nfc.CreateMimeRecord("text/plain", flTxt2.Text.GetBytes("UTF8"))))
    End If
           
End Sub

same result

B4X:
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
Connected: true
format: false
null
 
Upvote 0

Cnrez

Member
Licensed User
Longtime User
ok

B4X:
** Activity (main) Pause, UserClosed = false **
ACT-NEW_INTENT handled : 0 / NewIntentData{intents=[Intent { act=android.nfc.action.TAG_DISCOVERED flg=0x10020000 cmp=b4a.example/.main (has extras) }] token=android.os.BinderProxy@41f7c288}
** Activity (main) Resume **
Techs: [android.nfc.tech.IsoDep, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable]
Connected: true
(IOException) java.io.IOException
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
    at anywheresoftware.b4a.objects.NFC$TagTechnologyWrapper$2.call(NFC.java:298)
    at anywheresoftware.b4a.objects.NFC$TagTechnologyWrapper$2.call(NFC.java:1)
    at anywheresoftware.b4a.BA$3.run(BA.java:451)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:841)
Caused by: java.io.IOException
    at android.nfc.tech.NdefFormatable.format(NdefFormatable.java:141)
    at android.nfc.tech.NdefFormatable.format(NdefFormatable.java:95)
    ... 11 more
format: false
null

what should i do ?
 
Last edited:
Upvote 0

Cnrez

Member
Licensed User
Longtime User
i see, so my device (chiperlab RS30) is not supported for this operation,
thanks Erel, i will try on another device

note : can i do low level command with B4A ?

regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It shouldn't be too difficult to implement the code from that link with TagTech.RunAsync.

Something like:
B4X:
'after connection
TagTech.RunAsync("TT", "transceive", Array(Array As Byte(0xA2, 0x03, 0xE1, 0x10, 0x06, 0)), 0)
Wait For TT_RunAsync (Flag As Int, Success As Boolean, Result As Object)
Log(Success)
TagTech.RunAsync("TT", "transceive", Array(Array As Byte(0xA2, 0x04, 0x03, 0x00, 0xFE, 0)), 0)
Wait For TT_RunAsync (Flag As Int, Success As Boolean, Result As Object)
Log(Success)
TagTech.Close

I'm not familiar with this structure. Just copied it from the answer.

Note that you need to use android.nfc.tech.NfcA instead of android.nfc.tech.NdefFormatable.
 
Upvote 0
Top