Android Question #Additionaljar and JavaObject how to do?

Roberto P.

Well-Known Member
Licensed User
good morning
I'm trying to use a third-party library to a printer.
I can not use the methods of the library. for example, call the method GetPortName, but by mistake.

Here is the structure and instructions

'Inclusion library
#AdditionalJar: StarIOPort3.1

Dim jo As JavaObject

jo.InitializeStatic ("com.starmicronics.stario.StarIOPort") 'ok load library

'error
Dim sPort As String = jo.RunMethod ("getPortName", null)

java.lang.IllegalArgumentException: expected receiver of type com.starmicronics.stario.StarIOPort, but got java.lang.Class <com.starmicronics.stario.StarIOPort>


.....
com.android.internal.os.ZygoteInit at $ MethodAndArgsCaller.run (ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1107)
at dalvik.system.NativeStart.main (Native Method)
java.lang.IllegalArgumentException: expected receiver of type com.starmicronics.stario.StarIOPort, but got java.lang.Class <com.starmicronics.stario.StarIOPort>


Here is the structure of the class

upload_2015-1-9_12-44-33.png


thank you very much
 

Roberto P.

Well-Known Member
Licensed User
thanks Erel,
I managed to solve the problem. It was a problem of parameters! :(

if you do not use "InitializeStatic" is in error.

If can help someone, I can make available the access parameters to the printer. ;)
Thank You
 

Roberto P.

Well-Known Member
Licensed User
Hi,
here are the parts of the code I used to communicate with the printer.
The rest of the implementation should be as simple .....

B4X:
Sub Class_Globals

Dim connection, ctxt, m_port As JavaObject
End Sub

Sub GetContext As JavaObject
   Return GetBA.GetField("context")
End Sub
'restituisce il contesto
Sub GetBA As JavaObject

  Dim jo As JavaObject
  Dim cls As String = mParent
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")

End Sub

' apre la porta con la stampante
Sub OpenPort As Boolean

    Try
          
        ctxt = GetContext
      
        connection.InitializeStatic("com.starmicronics.stario.StarIOPort")
                                              
        m_port = connection.RunMethodJO("getPort", Array("BT:", "mini", 10000, ctxt))
      
        If CheckPrinterkStatus(m_port) = False Then
            Return False
        End If
      
    Catch
        Msgbox(LastException, "Errore Connessione stampante! Controlla che sia accesa")
        Return False
      
    End Try
  
    Return True

End Sub
' chiude la sessione di lavoro con la stampante
Sub ClosePort
  
    connection.RunMethod("releasePort", Array(m_port))
  
    Log("Ho chiuso la porta")
  
    aCommands.Clear
  
End Sub

Sub CheckPrinterkStatus(port As JavaObject) As Boolean

    Dim status As JavaObject        = port.RunMethodJO("retreiveStatus", Null)
  
    Dim bOnline As Boolean             = status.GetField("offline")
    Dim bNoCarta As Boolean         = status.GetField("receiptPaperEmpty")
    Dim bCoverAperta As Boolean     = status.GetField("coverOpen")
    Dim rawLength As Int            = status.GetField("rawLength")
  
    Dim portname As String             = port.RunMethod("getPortName", Null)
    Dim portSetting As String        = port.RunMethod("getPortSettings", Null)
  
  
    Log("-------------------------------------")
    Log("Nome porta: " & portname)
    Log("Impostazioni porta: " & portSetting)
  
    Log("La stampante è OffLine: " & bOnline)
    Log("No Carta: " & bNoCarta)
    Log("Cover Aperta: " & bCoverAperta)
    Log("Lunghezza raw: " & rawLength)
  
    Dim bOk As Boolean = True
    Dim aMsgError As String
  
    If bOnline = True Then OR  = True Then
        bOk = False
        aMsgError = "Stampante OffLine"
    End If
  
    If bNoCarta = True Then
        bOk = False
        aMsgError = "Stampante Senza Carta"
    End If
  
    If bCoverAperta = True Then
        bOk = False
        aMsgError = "Cover Stampante Aperta"
    End If
  
    If bOk = False Then
        Msgbox(aMsgError, "Errore Stampante")
    End If
  
    Return bOk
  
End Sub
you must pay attention to these parameters to be compatible with your printer, you will find everything on the website of the Star:
B4X:
m_port = connection.RunMethodJO("getPort", Array("BT:", "mini", 10000, ctxt))
 
Top