Android Question Printing on a usb thermal printer using external lib and javaobject

fabton1963

Member
Licensed User
Longtime User
Hello,
I'm trying to translate the following java code from an external jar using javaobject:

B4X:
public class MainActivity extends Activity {
 …
 private Printer mPrinter;
 …
 public void onCreate(Bundle savedInstanceState) {
  …
  mPrinter = new Printer(this, mHandler, null);   
  mPrinter.findUsbPrinters();
   
 } 
 private final Handler mHandler = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message msg) {   
            switch (msg.what) {   
            case Printer.MESSAGE_USB_DEVICE_SET:     
                Set<UsbDevice> usbDeviceSet = (Set<UsbDevice>) msg.obj;     
                for (UsbDevice device : usbDeviceSet) {     
                    if (device.getProductId() == 0x6D) {       
                        mPrinter.connect(device);       
                        break;     
                    }     
                }     
            return true;

            case Printer.MESSAGE_STATE_CHANGE:     
                switch (msg.arg1) {     
                    case Printer.STATE_CONNECTING:      // TODO: Processing when connection to printer is being tried     
                    break;
                    case Printer.STATE_CONNECTED:      // TODO: Processing when printer connection is completed 
                    mPrinter.printText(“This is a test\n”,0,1,0,true)
                    break;
                    case Printer.STATE_NONE:      // TODO: Processing when printer is not connected     
                    break;     
                }     
            break;
            
            case Printer.MESSAGE_PRINT_COMPLETE:     
                mPrinter.disconnect();     
            break;

            case Printer.MESSAGE_DEVICE_NAME:     
                String connectedDeviceName = msg.getData().getString( Printer.KEY_STRING_DEVICE_NAME);     
            break;

            case Printer.MESSAGE_TOAST:     
                Toast.makeText(getApplicationContext(),msg.getData().getString(Printer.KEY_STRING_TOAST), Toast.LENGTH_SHORT).show();     
            break;   
            }   
            return true;   
        } 
    };
}

I try whith the following code:

B4X:
#AdditionalJar: printer

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    oprinter.InitializeStatic("com.printer.posbank.Printer")
    
    Dim ctxt As JavaObject
    ctxt.InitializeContext

    mprinter.InitializeNewInstance("com.printer.posbank.Printer", Array(ctxt))
    mprinter.RunMethod("findUsbPrinters",Null)
    
    Activity.LoadLayout("1")
    
End Sub

mprinter.RunMethod("findUsbPrinters",Null) raise "java.lang.RuntimeException: Constructor not found." error.

Need help please.
 

stevel05

Expert
Licensed User
Longtime User
Without seeing the documentation it's difficult to speculate, but the first thing to check is that the printer constructor appears to need 3 parameters:

B4X:
mPrinter = new Printer(this, mHandler, null);

You are passing it one

B4X:
mprinter.InitializeNewInstance("com.printer.posbank.Printer", Array(ctxt))

If there is not a one parameter constructor, this is why the constructor cannot be found.
 
Upvote 0
Top