Java Question Need help creating library

Danie Steyn

Member
Licensed User
Longtime User
I am trying to create a library to print on mobile device with a built in printer. I've placed an "add" in the Job offers section but because it is a test that my customer is running they want to keep the costs as low as possible for the initial testing phase.
I am not very fluent in Java and if someone can point me in a direction it would be greatly appreciated.
The device is a PDA3505, I have figured out the whole Library creation process using the tutorials but when I run my app and try to print the application just crashes "Unfortunately, PrinterApp has stopped".
From what I can figure out with my limited knowledge it feels like I am missing something with the Handler.
The example app that the supplier has sent me looks is..
B4X:
package com.qs.test3505demo;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.smartdevicesdk.printer.PrinterClassSerialPort3505;
import com.smartdevicesdk.printer.PrinterCommand;

public class PrintAcivity extends Activity {

    PrinterClassSerialPort3505 printerClass = null;

    private EditText editText1;

    private String device="/dev/ttyMT0";

    private int baudrate=115200;

    MediaPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.print_layout);

        init();

        findViewById(R.id.button_print).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String str=editText1.getText().toString();
                if(str==null||str.length()==0){
                    return;
                }
                printerClass.write(new byte[] { 0x1b, 0x23 });

                printerClass.printText(str+"\n");
                //æ¤æ–¹æ³•è¡¨ç¤ºæ‰“å�°å›¾ç‰‡
//                printerClass.printImage(bitmap);
            }
        });

    }

    private void init() {
        // TODO Auto-generated method stub
        editText1=(EditText) findViewById(R.id.editText_print);

        Handler mhandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case PrinterCommand.MESSAGE_READ:
                        byte[] readBuf = (byte[]) msg.obj;
                        if (readBuf[0] == 0x08) {
                            Toast.makeText(getApplicationContext(), "打�缺纸", 0).show();
                        }
                }
                super.handleMessage(msg);
            }
        };

        printerClass = new PrinterClassSerialPort3505(device, baudrate,mhandler);

        printerClass.device = device;
        printerClass.baudrate = baudrate;
        printerClass.open();
        //å…¶ä¸çš„0X1b,0x76为查询打å�°æœºçŠ¶æ€�指令,方便检测打å�°æœºä¾‹å¦‚缺纸ã€�低电压ç‰çŠ¶æ€�
        printerClass.write(new byte[] { 0x1b, 0x76 });
        Toast.makeText(getApplicationContext(), "串�已�打开,现在�以进行打�", 0).show();

    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        printerClass.close();
    }
}

and my code currently looks like this ...
B4X:
package legend.printer.wrapper;



import com.smartdevicesdk.printer.PrinterClassSerialPort;
import com.smartdevicesdk.printer.PrinterClassSerialPort3505;
import com.smartdevicesdk.printer.PrinterCommand;

import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@Version(1.0f)
@Permissions(values={"android.permission.INTERNET"})
@ShortName("printerwrapper")
@DependsOn(values={"smartdevicesdk"})
@ActivityObject

public class wrapper {
    PrinterClassSerialPort3505 printerClass = null;

    private String device="/dev/ttyMT0";

    private int baudrate=115200;
       
       
    public int add(int x, int y) {
        return x + y;
    }
   
    public void Print_text(String theText)
    {
       
        String str=theText.toString();
       
        if(str==null||str.length()==0){
            return;
        }
        printerClass.write(new byte[] { 0x1b, 0x23 });

        printerClass.printText(str+"\n");
    }
   
    public void init(BA ba) {
        // TODO Auto-generated method stub
       
       
        Handler mhandler = BA.handler;

        printerClass = new PrinterClassSerialPort3505(device, baudrate,mhandler);

        printerClass.device = device;
        printerClass.baudrate = baudrate;
        printerClass.open();

        printerClass.write(new byte[] { 0x1b, 0x76 });
           

    }
   
   
}

I have tried changing Init to ...
B4X:
public void init(BA ba) {
        // TODO Auto-generated method stub
       

        Handler mhandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case PrinterCommand.MESSAGE_READ:
                        byte[] readBuf = (byte[]) msg.obj;
                        if (readBuf[0] == 0x08) {
                            Toast.makeText(getApplicationContext(), "Connected", 0).show();
                        }
                }
                super.handleMessage(msg);
            }
        };
       
        printerClass = new PrinterClassSerialPort3505(device, baudrate,mhandler);

        printerClass.device = device;
        printerClass.baudrate = baudrate;
        printerClass.open();
       
        printerClass.write(new byte[] { 0x1b, 0x76 });
       

    }
But I get the same result.
I have public int add in my code just to test that my library is working, and that works fine. I think my problem has to do with the handler() section, but I have no idea how to get around it.
Any help would be greatly appreciated, and as a token of my appreciation I will share the library when I get it working.
 

stevel05

Expert
Licensed User
Longtime User
I am not a java expert, and don't have the target device so probably can't help with the coding, but there should be some useful information in the IDE Log pane when it crashes.
 

mrjaw

Active Member
Licensed User
Longtime User
Hi!
I think you and me are in the same problem. I have the same device and I am trying to print equal like you. I did the print but I need to init the printer yet but I am for correct way. I think we must join effort
 

Danie Steyn

Member
Licensed User
Longtime User
Hi!
I think you and me are in the same problem. I have the same device and I am trying to print equal like you. I did the print but I need to init the printer yet but I am for correct way. I think we must join effort
Keep me posted if you come up with something, I have tried so many different things, inline java, java objects etc. But just cannot get it going.
 

mrjaw

Active Member
Licensed User
Longtime User
Can you send me this library or SDK for this device ?
this class PrinterClassSerialPort3505
 

Johan Schoeman

Expert
Licensed User
Longtime User

Attachments

  • b4aPDA3505Printer.zip
    7.2 KB · Views: 531
  • LibraryFiles.zip
    201.2 KB · Views: 606

Thupheo

Member
Licensed User
Hi Johan/Danie. Thanks for making this available to the forum. I was wondering if it would be possible to include other functions on the device such as
printImage(Bitmap bitmap), printUnicode(String textStr) ?
regards, Thupheo
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan/Danie. Thanks for making this available to the forum. I was wondering if it would be possible to include other functions on the device such as
printImage(Bitmap bitmap), printUnicode(String textStr) ?
regards, Thupheo
Will look I to it sometime in the next week or two...
 

ctd

Member
Licensed User
Dear Johan
I have bought this device since 2 month and i am looking for the way to wrap the library scan.
Can You help me wrapping The PDA 3505 scanning function
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi Johan/Danie. Thanks for making this available to the forum. I was wondering if it would be possible to include other functions on the device such as
printImage(Bitmap bitmap), printUnicode(String textStr) ?
regards, Thupheo
I have added methods to print Unicode and Bitmap - see if it works.

B4X:
#Region  Project Attributes
    #ApplicationLabel: b4aPDA3505Printer
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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.

    Private Button1 As Button
   
    Dim prt As NewPrint
    Dim bm As Bitmap
   
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("main")
    bm.Initialize(File.DirAssets,"icon.png")
   
    prt.Initialize("")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   
    prt.ClosePrinter

End Sub


Sub Button1_Click
   
    prt.PrintText = "Hello Danie"
    prt.printCRLF
   
    prt.printUnicode = "\u00a5123"
   
    prt.PrintImage = bm
       
   
End Sub
 

Attachments

  • PDA3505.xml
    2.3 KB · Views: 512
  • PDA3505.jar
    26.8 KB · Views: 507
Last edited:

ctd

Member
Licensed User
Print unicode and bitmap are working well on my device. Thank can you help me wrapping the scan class.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Print unicode and bitmap are working well on my device. Thank can you help me wrapping the scan class.
Can you please check what OS version is on your PDA3505? Is it 4.2 or 6.0?
 
Top