Android Question NFC TAD UID

Fabio Cernuschi

Member
Licensed User
Hallo to All,
first of all sorry for my question, I am quite a newbie in programming, so maybe I am posting a very stupid problem.
Anyway, I am trying to develop an App to read bulk NFC TAG UID, link this UID to a S/N of a machine in wich the tag is inserted and then save this record to a remote database (with some other details like date, time, operator, and so on).
I red the EMEL TUTORIAL for NFC, and my app correctly reads the TEXT, but the part in Java that should read the UID seems not working correctly.
Here's the log of error:

** Activity (main) Pause, UserClosed = false **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
Prova dal tel di Edo S7 03 febbraio 2017 07:15
Error occurred on line: 64 (Main)
java.lang.RuntimeException: Object should first be initialized (TagTechnology).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at b4a.example.main._activity_resume(main.java:455)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at b4a.example.main$ResumeMessage.run(main.java:302)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Activity (main) Pause, UserClosed = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **

I have to initialize the TagTechnology? How to do it?

Thank you very much for your help and...for your time!
Ciao
Fabio
 

Fabio Cernuschi

Member
Licensed User
Hi Erel!
Thank for your reply.
Here's the code:

Sub Activity_Resume

NFC.EnableForegroundDispatch
If NFC.IsNdefIntent(Activity.GetStartingIntent) Then
Dim records As List
records = NFC.GetNdefRecords(Activity.GetStartingIntent)
For i = 0 To records.Size - 1
Dim r As NdefRecord
r = records.Get(i)
Log(r.GetAsTextType)
Next
Stringa_NFC=r.GetAsTextType

End If
End Sub


Thanks!
Ciao
Fabio

PS: very interesting sw...
 
Upvote 0

Fabio Cernuschi

Member
Licensed User
Sorry Erel, you are right.
The error appears when I try to put this code in the Activity_Resume:

B4X:
Dim jo AsJavaObject = TagTech
Dim tag AsJavaObject = jo.RunMethod("getTag", Null)
Dim rawId() As Byte = tag.RunMethod("getId", Null)
Log(bc.HexFromBytes(rawId))

Thank you

Ciao

Fabio
 
Upvote 0

Fabio Cernuschi

Member
Licensed User
Hallo Erel, you are right. I will try to summarize and explain better my app and my problem.
My MAIN has TWO BUTTONS, Button1 and Button2.
As soon as a NFC TAG is detected, the Button1 background becomes RED and if I push it, a MsgBox shows me the TEXT in the TAG memory.
This app works correctly.
Here's the code:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim NFC As NFC
    Dim UID_NFC As String
    Dim Stringa_NFC As String
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
End Sub

Sub Activity_Resume
    NFC.EnableForegroundDispatch
        If NFC.IsNdefIntent(Activity.GetStartingIntent) Then
        Dim records As List
        Button1.Color = Colors.Red
        records = NFC.GetNdefRecords(Activity.GetStartingIntent)
        For i = 0 To records.Size - 1
            Dim r As NdefRecord
            r = records.Get(i)
            Log(r.GetAsTextType)
        Next
        Stringa_NFC=r.GetAsTextType
    End If
End Sub

Sub Button1_Click
    Button1.Color=Colors.Gray
    Msgbox(Stringa_NFC, "TEXT IN THE TAG")
End Sub

Sub Button2_Click
    Msgbox(UID_NFC, "TAG UID")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Now pushing Button2 I would like to show with a MsgBox the UID of the TAG.
I put the JavaObject "TagTech" as you described in your post #64451, but it does not work.
Here's the code:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim NFC As NFC
    Dim UID_NFC As String
    Dim Stringa_NFC As String
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
End Sub

Sub Activity_Resume
    Dim jo As JavaObject = TagTech
    Dim Tag As JavaObject = jo.RunMethod("getTag", Null)
    Dim rawId() As Byte = Tag.RunMethod("getId", Null)
    'Log(bc.HexFromBytes(rawId)) 'bc = ByteConverter
    NFC.EnableForegroundDispatch
        If NFC.IsNdefIntent(Activity.GetStartingIntent) Then
        Dim records As List
        Button1.Color = Colors.Red
        records = NFC.GetNdefRecords(Activity.GetStartingIntent)
        For i = 0 To records.Size - 1
            Dim r As NdefRecord
            r = records.Get(i)
            Log(r.GetAsTextType)
        Next
        Stringa_NFC=r.GetAsTextType
    End If
End Sub

Sub Button1_Click
    Button1.Color=Colors.Gray
    Msgbox(Stringa_NFC, "TEXT IN THE TAG")
End Sub

Sub Button2_Click
    Msgbox(UID_NFC, "TAG UID")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
With this code I receive the ERROR:
"Error parsing program.
Error description: Undeclared variable 'tagtech' is used before it was assigned any value.
Occurred on line: 35 (Main)
Dim jo As JavaObject = TagTech"

I tried to declare in the Sub Globals the TagTech variable with this code:
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim NFC As NFC
    Dim UID_NFC As String
    Dim Stringa_NFC As String
    Private Button1 As Button
    Dim TagTech As TagTechnology
End Sub
But then the problem is the LOG posted at the beginning of the thread:

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 36 (Main)
java.lang.RuntimeException: Object should first be initialized (TagTechnology).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at b4a.example.main._activity_resume(main.java:411)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at b4a.example.main.afterFirstLayout(main.java:108)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Activity (main) Pause, UserClosed = false **


I hope that now the issue is a little more clear and sorry for this verbose reply.

Thank you very much again for your help and for your patience...

Ciao

Fabio
 
Upvote 0

Fabio Cernuschi

Member
Licensed User
Hi Erel,
sorry for being boring...but I downloaded the ADVANCE NFC lib.
It works perfectly but I cannot find the right way to put the code to read th UID.
The code is what you suggested:
B4X:
Dim jo AsJavaObject = TagTech
Dim tag AsJavaObject = jo.RunMethod("getTag", Null)
Dim rawId() As Byte = tag.RunMethod("getId", Null)
Log(bc.HexFromBytes(rawId)) 'bc = ByteConverter

I put it just after the Sub Activity_Resume line but still the same error....
Here's the LOG:

** Activity (main) Pause, UserClosed = false **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 37 (Main)
java.lang.RuntimeException: Object should first be initialized (TagTechnology).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at b4a.example.main.afterFirstLayout(main.java:108)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **


Thank you very much for your help and patience...

Ciao

Fabio
 
Upvote 0

Fabio Cernuschi

Member
Licensed User
Hi Erel,
thanks for your time. I prefer to try to find someone who can help us.
Anyway, you posted that code in the thread 64451 last March, 13.
This is the reason why I said "the code you suggested"...

Thank you again

Warmest Regards

Fabio
 
Upvote 0
Top