Android Question cannot read and write to NFC card

Cnrez

Member
Licensed 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

Erel

Administrator
Staff member
Licensed User
Try to write something on the tag first. You can use an NFC app to write it and make sure that it was written successfully.
 

Cnrez

Member
Licensed 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

Erel

Administrator
Staff member
Licensed User
Which techs are listed after you write the data with this app?

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

Cnrez

Member
Licensed 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]
 

Erel

Administrator
Staff member
Licensed 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("http://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?
 

Cnrez

Member
Licensed 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("http://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
 

Erel

Administrator
Staff member
Licensed User
Not good. You must wait for it to be connected. Call WriteNdef from TagTech_Connected.
 

Cnrez

Member
Licensed 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("http://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
 

Cnrez

Member
Licensed 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
 

Erel

Administrator
Staff member
Licensed User
- Add Log(LastException) at the end of WriteNdef.
- Check the unfiltered logs.
 

Cnrez

Member
Licensed 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:

Cnrez

Member
Licensed 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
 

Erel

Administrator
Staff member
Licensed 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.
 
Top