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..
and my code currently looks like this ...
I have tried changing Init to ...
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.
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 });
}
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.