Android Question NFC NdefFormatable Tag IOException

janitra

Member
I have a little problem when injecting data to NFC Tags.

Here my code
B4X:
Sub Activity_Resume
    If app.GetNFCAdapter(Me).IsInitialized = False Then
        //Show Message
    Else
        If app.GetNFCAdapter(Me).RunMethod("isEnabled", Null) = False Then
            Msgbox2Async("NFC is Off. Turn on NFC?", "NFC Off", "Yes", "", "No", Null, False)
            Wait For MsgBox_Result (Confirmed As Int)
            
            If Confirmed = DialogResponse.POSITIVE Then
                app.OpenNFCSetting
            Else If Confirmed = DialogResponse.NEGATIVE Then
                Dim jo As JavaObject
                jo.InitializeStatic("java.lang.System")
                jo.RunMethod("exit", Array(0))
            End If
        Else
            nfc.EnableForegroundDispatch
            
            Dim intent As Intent = Activity.GetStartingIntent
            If intent.IsInitialized = False Or intent = prevIntent Then Return
            prevIntent = intent
            If intent.Action.EndsWith("TECH_DISCOVERED") Or intent.Action.EndsWith("NDEF_DISCOVERED") _
                    Or intent.Action.EndsWith("TAG_DISCOVERED") Then
                Dim tech As List = nfc.GetTechList(intent)
        
                If tech.IndexOf("android.nfc.tech.Ndef") > -1 Then
                    nfcType = "Ndef"
                    
                    tagTech.Initialize("TagTech", "android.nfc.tech.Ndef", intent)
                    tagTech.Connect
                Else If tech.IndexOf("android.nfc.tech.NdefFormatable") > -1 Then
                    nfcType = "NdefFormatable"
                    
                    tagTech.Initialize("TagTech", "android.nfc.tech.NdefFormatable", intent)
                    tagTech.Connect
                End If
            End If
        End If
    End If
End Sub

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

Private Sub TagTech_Connected (Success As Boolean)
    If Success = False Then
        ToastMessageShow("NFC not Connected", True)
    Else
        WriteNdef(Array(nfc.CreateMimeRecord("text/plain", EditText.Text.GetBytes("UTF8")), _
                    nfc.CreateMimeRecord("text/plain", EditText.Text.GetBytes("UTF8")), _
                    nfc.CreateMimeRecord("text/plain", EditText.Text.ToUpperCase.GetBytes("UTF8")), _
                    nfc.CreateMimeRecord("text/plain", EditText.Text.ToUpperCase.GetBytes("UTF8")), _
                    nfc.CreateMimeRecord("text/plain", EditText.Text.GetBytes("UTF8")), _
                    nfc.CreateMimeRecord("text/plain", EditText.Text.ToUpperCase.GetBytes("UTF8")), _
                    nfc.CreateMimeRecord("text/plain", EditText.Text.ToUpperCase.GetBytes("UTF8")), _
                    nfc.CreateMimeRecord("text/plain", EditText.Text.GetBytes("UTF8")), _
                    nfc.CreateMimeRecord("text/plain", formulaList.Text.GetItem(formulaList.SelectedIndex).GetBytes("UTF8"))))
    End If
End Sub

Private Sub WriteNdef (Records() As Object)
    Dim RecordsJO As JavaObject
    RecordsJO.InitializeArray("android.nfc.NdefRecord", Records)
    Dim message As JavaObject
    message.InitializeNewInstance("android.nfc.NdefMessage", Array(RecordsJO))
    
    If nfcType = "Ndef" Then
        tagTech.RunAsync("WriteNdef", "writeNdefMessage", Array(message), 0)
    Else If nfcType = "NdefFormatable" Then
        tagTech.RunAsync("Format", "format", Array(message), 0)
    End If
    
End Sub

Private Sub WriteNdef_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    If Success Then
        ToastMessageShow("Success", False)
        ReadNdef
    End If
End Sub

Private Sub ReadNdef
    tagTech.RunAsync("ReadNdef", "getNdefMessage", Null, 0)
End Sub

Private Sub ReadNdef_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    nfcContent.Clear()
    ScanNFC(Success, Result)
End Sub

Private Sub ScanNFC(nfcReadStatus As Boolean, result As Object)
    If nfcReadStatus Then
        If result = Null Then
            ToastMessageShow("No records found.", False)
        Else
            Dim xui As XUI
            Dim message As JavaObject = result
            Dim records() As Object = message.RunMethod("getRecords", Null)
            
            Dim i As Int = 0
            For Each ndefRecords As NdefRecord In records
                Dim panel As B4XView = xui.CreatePanel("")
                panel.SetLayoutAnimated(100, 0, 0, 100%x, 100dip)
                panel.LoadLayout("NfcItem")
                
                Dim byt() As Byte = ndefRecords.GetPayload
                
                nfcText.Text = BytesToString(byt, 0, byt.Length, "UTF8")
                nfcContent.Add(panel, i)
                
                i = i + 1
            Next
        End If
    End If
End Sub

The problem is, when i try to Write to the tag, the log show "IO Exception".
Is there any work around?
Like use other method (low level programming) or other library.

Note:
I'm using NFC Library v. 2.0.1
My tag tech is NfcA, MifareClassic, NdefFormatable.
1Kb, 16 Sectors, 64 Blocks.
 

janitra

Member
Thanks Erel,
I don't remember why I use jo.RunMethod to exit the apps.
I'll re-try ExitApplication later.

I've try it.
When I scan the tag first time, the log shows
Not Initialize
.
After that, the phone cannot scan the NFC anymore.
I need to restart the NFC and my app (kill and re-open)
 
Upvote 0

janitra

Member
Hi Erel,
I try it with few NDEF app from playstore, and it say "write failed".

I found out something.
I have a Samsung Note 9 and a Samsung A3 that can write and another 4 Samsung A31 that can't.
The 6 phone using same app.

But IF a tag that have been written by Note 9 or A3 that can write and use other 4 A31 that can't write to write on that tag, it will success.

So I think, the problem is when the phone is trying to format the tag.
But somehow, the 4 of the A31 can't format the tag.
 
Upvote 0
Top