Android Question Getting names for JavaObject wrapped library

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I am wrapping two 3rd party libraries: (Star Micronics Android SDK)

My first time wrapping a library. I opted to go the JavaObject route because I don't have Eclipse, and it may add days introducing a new tool to support for my development staff.

All is going pretty good. They (Star) likes to use static methods to create objects. Like below, getPort. For example, this wrapper works perfectly

B4X:
Sub Class_Globals
    Private StarIO As JavaObject
    Private port As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    StarIO.InitializeStatic("com.starmicronics.stario.StarIOPort" )
End Sub

Public Sub Open(portName As String, portSettings As String, timeoutMilliseconds As Int) As Boolean
    port = StarIO.RunMethod("getPort",Array As Object(portName,portSettings,timeoutMilliseconds))
End Sub

Public Sub Close()
    StarIO.RunMethod("releasePort",Array As Object(port))
End Sub

Public Sub SearchPrinter( target As String) As List
    Dim wl As List
    Dim al As List
    wl.Initialize
    al = StarIO.RunMethod("searchPrinter",Array As Object(target))
    For i = 0 To al.Size-1
        Dim spi As StarPortInfo
        spi.Initialize(al.Get(i))
        wl.Add(spi)
    Next
    Return wl   
End Sub

Public Sub ReadPort(buffer() As Byte, offset As Int, size As Int) As Int
    Dim bytesRead As Int = port.RunMethod("readPort",Array As Object(buffer,offset,size) )
    Return bytesRead
End Sub

Public Sub WritePort(buffer() As Byte, offset As Int, size As Int)
    port.RunMethod("writePort",Array As Object(buffer,offset,size) )
End Sub

Public Sub RetreiveStatus As StarPrinterStatus
    Dim sps As StarPrinterStatus
    sps.Initialize(port.RunMethod("retreiveStatus",Null))
    Return sps
End Sub

Public Sub BeginCheckdBlock As StarPrinterStatus
    Dim sps As StarPrinterStatus
    sps.Initialize(port.RunMethod("beginCheckdBlock",Null))
    Return sps   
    End Sub

Public Sub EndCheckdBlock As StarPrinterStatus
    Dim sps As StarPrinterStatus
    sps.Initialize(port.RunMethod("endCheckdBlock",Null))
    Return sps   
End Sub

Public Sub setEndCheckedBlockTimeoutMillis( endCheckedBlockTimeoutMillis As Int)
    port.RunMethod("setEndCheckedBlockTimeoutMillis",Array As Object(endCheckedBlockTimeoutMillis))
End Sub

Public Sub getFirmwareInformation() As Map
    Dim mp As Map
    mp = port.RunMethod("getFirmwareInformation",Null)
    Return mp
End Sub

Public Sub getStarIOVersion() As String
    Dim value As String
    value = port.RunMethod("getStarIOVersion",Null)
    Return value
End Sub

Unfortunately, another one is giving me fits, and not sure how to proceed.

It is documented as:
Declaration
public static ICommandBuilder createCommandBuilder(Emulation emulation);

Emulation is an Enum, and wrapping that seems to be another story. But my issue is calling createCommandBuilder because this static method is not found. I'm sure I'm not properly dealing with the Emulation parameter. But I have to get past the method not found error first.

Error occurred on line: 16 (StarCommandBuilder)
java.lang.RuntimeException: Method: createCommandBuilder not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:129)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
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 java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:4764)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5376)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

The class wrapper for the ICommandBuilder wrapper is (thus far)

B4X:
Sub Class_Globals
    Private builder As JavaObject
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize()' emulation As Emulation )
    Dim jo As JavaObject
    jo.InitializeStatic("com.starmicronics.starioextension.StarIoExt")

    Dim je As JavaObject
    je.InitializeStatic("com.starmicronics.starioextension.StarIoExt.Emulation")
   
    builder = jo.RunMethod("createCommandBuilder", Array As Object(je))
End Sub

The exception occurs on the "builder = ..." line.

I don't know names, except what Star provide for documentation, and I suspect in general my approach is close, because it worked to wrap the first class from the documentation.

Any idea how to get past this stuck point, please?
 

JordiCP

Expert
Licensed User
Longtime User
Emulation is an Enum, and wrapping that seems to be another story. But my issue is calling createCommandBuilder because this static method is not found. I'm sure I'm not properly dealing with the Emulation parameter. But I have to get past the method not found error first.
I think both problems are related. Look at the error
B4X:
java.lang.RuntimeException: Method: createCommandBuilder not matched.

"not matched" says that the method exists, but not with the argument types that you are using (otherwise the error would be "not found", "Can't find...",...). So, seems that solving the Emulation issue will solve this

not tested, but you could try with (changes are in last line)
B4X:
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize()' emulation As Emulation )Dim jo AsJavaObject
  jo.InitializeStatic("com.starmicronics.starioextension.StarIoExt")
  Dim je AsJavaObject
  je.InitializeStatic("com.starmicronics.starioextension.StarIoExt.Emulation")
   
  builder = jo.RunMethod("createCommandBuilder", ArrayAs Object(je.GetField("EscPos"))  ' or "StarGraphic","StarLine", ...
End Sub
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Not only fixes the createCommandBuilder, but you've ALSO shown me how to access Java enumerations. Together these have it working, and I'm back in business here! Thank you so much JordiCP!
 
Upvote 0
Top