Android Question NdefFormatable: Unable to Format Tag to Read and Write

MCU01

Member
Licensed User
Longtime User
Hi,
I've been using this code for a while to read NFC tags and it works great. I have an electronic design where I'm using an NXP chip with an antenna tuned properly. Sometimes the phone recognizes some tag and sometimes I get the message "Tag does not support Ndef." from the following code,

B4X:
'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.Ndef") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.Ndef" , si)
            'Connect to the tag
            TagTech.Connect
        Else
            ToastMessageShow("Tag does not support Ndef.", True)  'CHECK WHY THIS MESSAGE SOMETIMES COMES UP....
        End If
    End If

The tags are not NDEF formatted. Just in case that you want to take a look at the datasheet of this tag here is the datasheet,

https://www.mouser.com/datasheet/2/302/NT3H2111_2211-1127378.pdf

Below is a screenshot of the tag read using a different program. It shows that the tag is NdefFormatable instead of being Ndef enabled.

upload_2018-6-30_1-42-53-png.69440


Any help is greatly appreciated.

Thank you,

Robert
 

MCU01

Member
Licensed User
Longtime User
Hmm, I tried Erel's code in here and still the tag doesn't get formatted. I still get the NdefFormatable as in the picture above. I inserted a new ELSE IF in the code below. The program returns the message "Tag has been formatted." but the tag doesn't get formatted. Any help or guidance is highly appreciated. Thanks.

B4X:
    '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.Ndef") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.Ndef" , si)
            'Connect to the tag
            TagTech.Connect
           
        else if techs.IndexOf("android.nfc.tech.NdefFormatable") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.NdefFormatable" , si)
            TagTech.Connect
            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)
            ToastMessageShow("Tag has been formatted.", True)
           
        Else
            ToastMessageShow("Tag does not support Ndef.", True)  'CHECK WHY THIS MESSAGE SOMETIMES  COMES UP....
        End If
    End If
 
Upvote 0

DavideV

Active Member
Licensed User
Longtime User
Are you sure it is not depending on the phone? If i well understand it's not a common, commercial nfc tag. Try with some other phones using the same code. Does the code work with commercial tags?

bye
Davide
 
Upvote 0

MCU01

Member
Licensed User
Longtime User
Where is the Format_RunAsync sub? Maybe it failed.

Hi Erel, yes I'm including the Format_RunAsync sub. When running the program in DEBUG mode the log entry says that "SUCCESS = FALSE".

I can format the tags with other NFC apps like the one in the attached picture. I use the reset option. After the NdefFormatable is gone I can use your code without any issues. I just want to include the formatting code in my app.

Do you think that I might need to put some kind of time pause in the code to let it finish the format?
 

Attachments

  • Screenshot_20180701-175932_NTAG I2C Demo.jpg
    Screenshot_20180701-175932_NTAG I2C Demo.jpg
    493.2 KB · Views: 316
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Hmm, I tried Erel's code in here and still the tag doesn't get formatted. I still get the NdefFormatable as in the picture above. I inserted a new ELSE IF in the code below. The program returns the message "Tag has been formatted." but the tag doesn't get formatted. Any help or guidance is highly appreciated. Thanks.
This description is wrong. There is no meaning to the toast message you show before the event is raised. Only the result in the event matters.

Check the output of Log(LastException) and also check the unfiltered logs. There might be more information there.
 
Upvote 0

MCU01

Member
Licensed User
Longtime User
This description is wrong. There is no meaning to the toast message you show before the event is raised. Only the result in the event matters.

Check the output of Log(LastException) and also check the unfiltered logs. There might be more information there.

Hmm, this is weird. Sometimes it works and sometimes it doesn't. Below is a screenshot of the unfiltered logs. Is there a way to keep trying to format until it is successful?


upload_2018-7-3_5-5-48.png
 
Upvote 0

MCU01

Member
Licensed User
Longtime User
Please post the relevant code and logs as text, not screenshot.

Sorry, okay here it is the log and part of the code,

B4X:
LogCat connected to: B4A-Bridge: samsung SM-G955U
--------- beginning of main
SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SM-G955U_8.0.0_0009, [-1 -1 -1 -1 0 1]
SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=anywheresoftware.b4a.b4abridge
TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service
Added TimaKeyStore provider
sphal namespace is not configured for this process. Loading /vendor/lib64/egl/libEGL_adreno.so from the current namespace instead.
loaded /vendor/lib64/egl/libEGL_adreno.so
sphal namespace is not configured for this process. Loading /vendor/lib64/egl/libGLESv1_CM_adreno.so from the current namespace instead.
loaded /vendor/lib64/egl/libGLESv1_CM_adreno.so
Bridge logger not enabled.
HWUI GL Pipeline
setView = [email protected][main] TM=true MM=false
*** Service (starter) Create ***
** Service (starter) Start **
dispatchAttachedToWindow
sf_framedrop debug : 0x4f4c, game : false, logging : 0
Relayout returned: old=[0,0][0,0] new=[0,0][1080,2220] result=0x27 surface={valid=true 539857195008} changed=true
** Activity (main) Create, isFirst = true **
sphal namespace is not configured for this process. Loading /vendor/lib64/egl/libGLESv2_adreno.so from the current namespace instead.
ignoring event: chkftpserver_checkedchange
** Activity (main) Resume **
loaded /vendor/lib64/egl/libGLESv2_adreno.so
QUALCOMM build                   : 4bd8562, I53d7d6a370
Build Date                       : 05/02/18
OpenGL ES Shader Compiler Version: EV031.22.00.01
Local Branch                     :
Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.6.4.R1.08.00.00.309.049
Remote Branch                    : NONE
Reconstruct Branch               : NOTHING
sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
PFP: 0x005ff087, ME: 0x005ff063
MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1080, 2220) ci=Rect(0, 63 - 0, 126) vi=Rect(0, 63 - 0, 126) or=1
MSG_WINDOW_FOCUS_CHANGED 1
Starting input: [email protected] nm : anywheresoftware.b4a.b4abridge ic=null
startInputInner - mService.startInputOrWindowGainedFocus
Initialized EGL, version 1.4
Swap behavior 2
STS_GLApi : DTS, ODTC are not allowed for Package : anywheresoftware.b4a.b4abridge
eglCreateWindowSurface = 0x7da9de4d50
sphal namespace is not configured for this process. Loading /vendor/lib64/hw/gralloc.msm8998.so from the current namespace instead.
Starting input: [email protected] nm : anywheresoftware.b4a.b4abridge ic=null
*** Service (service1) Create ***
TcpOptimizer-ON
Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
allPendingIntents
allPendingIntents
** Service (service1) Start **
Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
allPendingIntents
** Activity (main) Pause, UserClosed = false **
MSG_WINDOW_FOCUS_CHANGED 0
eglDestroySurface = 0x7da9de4d50
Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x5 surface={valid=false 0} changed=true
Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x1 surface={valid=false 0} changed=false
Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x1 surface={valid=false 0} changed=false
sf_framedrop debug : 0x4f4c, game : false, logging : 0
Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x7 surface={valid=true 539857195008} changed=true
eglCreateWindowSurface = 0x7da9de4d50
** Activity (main) Resume **
MSG_WINDOW_FOCUS_CHANGED 1
Starting input: [email protected] nm : anywheresoftware.b4a.b4abridge ic=null
startInputInner - mService.startInputOrWindowGainedFocus
ViewPostIme pointer 0
ViewPostIme pointer 1
** Service (service1) Destroy **
*** Service (service1) Create ***
Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
allPendingIntents
allPendingIntents
** Service (service1) Start **
Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
allPendingIntents
ViewPostIme pointer 0
ViewPostIme pointer 1
** Service (service1) Destroy **
*** Service (service1) Create ***
Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
allPendingIntents
allPendingIntents
** Service (service1) Start **
Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
allPendingIntents
Connected to B4A-Bridge (Wifi)
Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
allPendingIntents
** Activity (main) Pause, UserClosed = false **
MSG_WINDOW_FOCUS_CHANGED 0
eglDestroySurface = 0x7da9de4d50
Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x5 surface={valid=false 0} changed=true
Installing file.
Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x1 surface={valid=false 0} changed=false
sf_framedrop debug : 0x4f4c, game : false, logging : 0
Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x7 surface={valid=true 539857195008} changed=true
eglCreateWindowSurface = 0x7da9de4d50
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
MSG_WINDOW_FOCUS_CHANGED 1
Starting input: [email protected] nm : anywheresoftware.b4a.b4abridge ic=null
startInputInner - mService.startInputOrWindowGainedFocus
eglDestroySurface = 0x7da9de4d50
Relayout returned: old=[0,0][1080,2220] new=[0,0][1080,2220] result=0x5 surface={valid=false 0} changed=true
MSG_WINDOW_FOCUS_CHANGED 0
PackageAdded: package:b4a.example
** 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.NfcA, android.nfc.tech.MifareUltralight, android.nfc.tech.NdefFormatable]
Connected: true
Writing completed. Success=false, Flag=0
Writing completed. Success=true, Flag=0
PropClient failed to load
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.NfcA, android.nfc.tech.MifareUltralight, android.nfc.tech.Ndef]
Connected: true
Writing completed. Success=true, Flag=0
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
PropClient failed to load
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.NfcA, android.nfc.tech.MifareUltralight, android.nfc.tech.NdefFormatable]
Connected: true
Writing completed. Success=false, Flag=0
Writing completed. Success=false, Flag=0
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.NfcA, android.nfc.tech.MifareUltralight, android.nfc.tech.NdefFormatable]
Writing completed. Success=false, Flag=0
Connected: true
Writing completed. Success=false, Flag=0
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.NfcA, android.nfc.tech.MifareUltralight, android.nfc.tech.NdefFormatable]
Connected: true
Writing completed. Success=false, Flag=0
Writing completed. Success=false, Flag=0
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Techs: [android.nfc.tech.NfcA, android.nfc.tech.MifareUltralight, android.nfc.tech.NdefFormatable]
Connected: true
Writing completed. Success=false, Flag=0
Writing completed. Success=true, Flag=0
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **

B4X:
    '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.Ndef") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.Ndef" , si)
            'Connect to the tag
            TagTech.Connect
            
        else if    techs.IndexOf("android.nfc.tech.NdefFormatable") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.NdefFormatable" , si)
            TagTech.Connect
            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)
            ToastMessageShow("Tag has been formatted.", True)
            
        Else
            ToastMessageShow("Tag does not support Ndef.", True)  'CHECK WHY THIS MESSAGE SOMETIMES  COMES UP....
        End If
    End If
End Sub
 
Upvote 0

MCU01

Member
Licensed User
Longtime User
I had the RunAsync event already. Okay, I'm erasing that ToastMessage line,

B4X:
'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.Ndef") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.Ndef" , si)
            'Connect to the tag
            TagTech.Connect
            
        else if    techs.IndexOf("android.nfc.tech.NdefFormatable") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.NdefFormatable" , si)
            TagTech.Connect
            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)
            
        Else
            ToastMessageShow("Tag does not support Ndef.", True)  'CHECK WHY THIS MESSAGE SOMETIMES  COMES UP....
        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
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As it sometimes work and sometimes fail, your best option is to make several attempts. Something like this:

B4X:
Sub FormatTag 
 For i = 1 to 5
  
  TagTech.Initialize("TagTech", "android.nfc.tech.NdefFormatable" , si)
            TagTech.Connect
            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)
             If Success Then
                Log("Success")
                Return
             Else
               Sleep(500)
             End If
Next
Log("Error")
End Sub
 
Upvote 0
Top