Hi,
this is my first question. This forum is very good and i solve very much first problems.
I need a library for the mobile Bluetooth-Printer Citizen CMP-20. In native code it was no problem print the samples. Now i can build the library but the printer doesen't print the sample. I can print a single line with the class AsyncStreamsText from Erel. So i think i can only print with a asyncon Task.
But i haven't any idee.
For my Tests, i use the Bluetooth-Sample-Code. I made some changes.
Main
PrnActivity
Library CitizenESCP
Best regards from Germany, good Night!
this is my first question. This forum is very good and i solve very much first problems.
I need a library for the mobile Bluetooth-Printer Citizen CMP-20. In native code it was no problem print the samples. Now i can build the library but the printer doesen't print the sample. I can print a single line with the class AsyncStreamsText from Erel. So i think i can only print with a asyncon Task.
But i haven't any idee.
For my Tests, i use the Bluetooth-Sample-Code. I made some changes.
Main
B4X:
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: Bluetooth Example
#VersionCode: 1
#VersionName:
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
'Activity module
Sub Process_Globals
Dim admin As BluetoothAdmin
Dim serial1 As CitizenBtPort
Dim foundDevices As List
Type NameAndMac (Name As String, Mac As String)
Dim connectedDevice As NameAndMac
End Sub
Sub Globals
Dim btnSearchForDevices As Button
Dim btnAllowConnection As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
admin.Initialize("admin")
serial1.Bluetoothportinit
End If
Activity.LoadLayout("1")
End Sub
Sub Activity_Resume
btnSearchForDevices.Enabled = False
btnAllowConnection.Enabled = False
If admin.IsEnabled = False Then
If admin.Enable = False Then
ToastMessageShow("Error enabling Bluetooth adapter.", True)
Else
ToastMessageShow("Enabling Bluetooth adapter...", False)
'the StateChanged event will be soon raised
End If
Else
Admin_StateChanged(admin.STATE_ON, 0)
End If
End Sub
Sub Admin_StateChanged (NewState As Int, OldState As Int)
btnSearchForDevices.Enabled = (NewState = admin.STATE_ON)
btnAllowConnection.Enabled = btnSearchForDevices.Enabled
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed = True Then
serial1.Disconnect
End If
End Sub
Sub btnSearchForDevices_Click
foundDevices.Initialize
If admin.StartDiscovery = False Then
ToastMessageShow("Error starting discovery process.", True)
Else
ProgressDialogShow("Searching for devices...")
End If
End Sub
Sub Admin_DiscoveryFinished
ProgressDialogHide
If foundDevices.Size = 0 Then
ToastMessageShow("No device found.", True)
Else
Dim l As List
l.Initialize
For i = 0 To foundDevices.Size - 1
Dim nm As NameAndMac
nm = foundDevices.Get(i)
l.Add(nm.Name)
Next
Dim res As Int
res = InputList(l, "Choose device to connect", -1)
If res <> DialogResponse.CANCEL Then
connectedDevice = foundDevices.Get(res)
ProgressDialogShow("Trying to connect to: " & connectedDevice.Name & " (" & connectedDevice.Mac & ")")
serial1.ConnectAdress(connectedDevice.Mac)
Serial1_Connected(serial1.IsConnected)
End If
End If
End Sub
Sub Admin_DeviceFound (Name As String, MacAddress As String)
Log(Name & ":" & MacAddress)
Dim nm As NameAndMac
nm.Name = Name
nm.Mac = MacAddress
foundDevices.Add(nm)
ProgressDialogShow("Searching for devices (~ device found)...".Replace("~", foundDevices.Size))
End Sub
Sub btnAllowConnection_Click
'this intent makes the device discoverable for 300 seconds.
Dim i As Intent
i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
StartActivity(i)
'serial1.Listen
End Sub
Sub Serial1_Connected (Success As Boolean)
ProgressDialogHide
Log("connected: " & Success)
If Success = False Then
Log(LastException.Message)
ToastMessageShow("Error connecting: " & LastException.Message, True)
Else
StartActivity(PrnActivity)
End If
End Sub
PrnActivity
B4X:
#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
'Activity module
Sub Process_Globals
Private CMP20 As CitizenESCP
Private ast As AsyncStreamsText
Private serial2 As CitizenBtPort
End Sub
Sub Globals
Dim txtInput As EditText
Dim txtLog As EditText
Dim btnSend As Button
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("2")
Activity.Title = "Citizen CMP-20 Activity"
serial2 = Main.serial1
If ast.IsInitialized = False Then
ast.Initialize(Me, "ast", serial2.GetInputStream, serial2.GetOutputStream)
End If
txtLog.Width = 100%x
End Sub
Sub ast_Error
ToastMessageShow("Connection is broken.", True)
btnSend.Enabled = False
txtInput.Enabled = False
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
ast.Close
End If
End Sub
Sub txtInput_EnterPressed
If btnSend.Enabled = True Then btnSend_Click
End Sub
Sub btnSend_Click
ast.Write(CMP20.OneRow("Dies ist ein Test..."))
Prn_test2
'txtInput.SelectAll
'txtInput.RequestFocus
LogMessage("Me", "Drucken...")
End Sub
Sub LogMessage(From As String, Msg As String)
txtLog.Text = txtLog.Text & From & ": " & Msg & CRLF
txtLog.SelectionStart = txtLog.Text.Length
End Sub
Sub Prn_test2
CMP20.barcode2DTest
End Sub
Sub ast_NewText(Text As String)
Log("Text: " & Text)
Log(Text.Length)
End Sub
Sub ast_Terminated
Log("Connection terminated")
End Sub
Library CitizenESCP
B4X:
package com.mcdreck;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import android.util.Log;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import com.citizen.jpos.command.ESCPOS;
import com.citizen.jpos.command.ESCPOSConst;
import com.citizen.jpos.printer.CMPPrint;
import com.citizen.jpos.printer.ESCPOSPrinter;
@Permissions(values = {"android.permission.BLUETOOTH", "android.permission.BLUETOOTH_ADMIN"})
@DependsOn(values={ "Citizen_Android_1056" })
@Author("Roland Nyga")
@Version((float) 1.08)
@ShortName("CitizenESCP")
public class CitizenESCP {
public int CMP_ALIGNMENT_LEFT = CMPPrint.CMP_ALIGNMENT_LEFT;
public int CMP_ALIGNMENT_CENTER = CMPPrint.CMP_ALIGNMENT_CENTER;
public int CMP_ALIGNMENT_RIGHT = CMPPrint.CMP_ALIGNMENT_RIGHT;
public int CMP_FNT_DEFAULT = CMPPrint.CMP_FNT_DEFAULT;
public int CMP_FNT_FONTB = CMPPrint.CMP_FNT_FONTB;
public int CMP_FNT_BOLD = CMPPrint.CMP_FNT_BOLD;
public int CMP_FNT_UNDERLINE = CMPPrint.CMP_FNT_UNDERLINE;
public int CMP_BCS_UPCA1 = CMPPrint.CMP_BCS_UPCA;
public int CMP_BCS_UPCE1 = CMPPrint.CMP_BCS_UPCE;
public int CMP_BCS_EAN8 = CMPPrint.CMP_BCS_EAN8;
public int CMP_BCS_EAN13 = CMPPrint.CMP_BCS_EAN13;
public int CMP_BCS_JAN8 = CMPPrint.CMP_BCS_JAN8;
public int CMP_BCS_JAN13 = CMPPrint.CMP_BCS_JAN13;
public int CMP_BCS_ITF = CMPPrint.CMP_BCS_ITF;
public int CMP_BCS_Codabar = CMPPrint.CMP_BCS_Codabar;
public int CMP_BCS_Code39 = CMPPrint.CMP_BCS_Code39;
public int CMP_BCS_Code93 = CMPPrint.CMP_BCS_Code93;
public int CMP_BCS_Code128 = CMPPrint.CMP_BCS_Code128;
public int CMP_TXT_1WIDTH = CMPPrint.CMP_TXT_1WIDTH;
public int CMP_TXT_2WIDTH = CMPPrint.CMP_TXT_2WIDTH;
public int CMP_TXT_3WIDTH = CMPPrint.CMP_TXT_3WIDTH;
public int CMP_TXT_4WIDTH = CMPPrint.CMP_TXT_4WIDTH;
public int CMP_TXT_5WIDTH = CMPPrint.CMP_TXT_5WIDTH;
public int CMP_TXT_6WIDTH = CMPPrint.CMP_TXT_6WIDTH;
public int CMP_TXT_7WIDTH = CMPPrint.CMP_TXT_7WIDTH;
public int CMP_TXT_8WIDTH = CMPPrint.CMP_TXT_8WIDTH;
public int CMP_TXT_1HEIGHT = CMPPrint.CMP_TXT_1HEIGHT;
public int CMP_TXT_2HEIGHT = CMPPrint.CMP_TXT_2HEIGHT;
public int CMP_TXT_3HEIGHT = CMPPrint.CMP_TXT_3HEIGHT;
public int CMP_TXT_4HEIGHT = CMPPrint.CMP_TXT_4HEIGHT;
public int CMP_TXT_5HEIGHT = CMPPrint.CMP_TXT_5HEIGHT;
public int CMP_TXT_6HEIGHT = CMPPrint.CMP_TXT_6HEIGHT;
public int CMP_TXT_7HEIGHT = CMPPrint.CMP_TXT_7HEIGHT;
public int CMP_TXT_8HEIGHT = CMPPrint.CMP_TXT_8HEIGHT;
public int CMP_HRI_TEXT_NONE = CMPPrint.CMP_HRI_TEXT_NONE;
public int CMP_HRI_TEXT_ABOVE = CMPPrint.CMP_HRI_TEXT_ABOVE;
public int CMP_HRI_TEXT_BELOW = CMPPrint.CMP_HRI_TEXT_BELOW;
public int CMP_STS_NORMAL = CMPPrint.CMP_STS_NORMAL;
public int CMP_STS_PAPER_EMPTY = CMPPrint.CMP_STS_PAPER_EMPTY;
public int CMP_STS_COVER_OPEN = CMPPrint.CMP_STS_COVER_OPEN;
public int CMP_STS_MSR_READ = CMPPrint.CMP_STS_MSR_READ;
public int CMP_BITMAP_NORMAL = CMPPrint.CMP_BITMAP_NORMAL;
public int CMP_BITMAP_DOUBLE_WIDTH = CMPPrint.CMP_BITMAP_DOUBLE_WIDTH;
public int CMP_BITMAP_DOUBLE_HEIGHT = CMPPrint.CMP_BITMAP_DOUBLE_HEIGHT;
public int CMP_BITMAP_QUADRUPLE = CMPPrint.CMP_BITMAP_QUADRUPLE;
public int CMP_QRCODE_EC_LEVEL_L = CMPPrint.CMP_QRCODE_EC_LEVEL_L;
public int CMP_QRCODE_EC_LEVEL_M = CMPPrint.CMP_QRCODE_EC_LEVEL_M;
public int CMP_QRCODE_EC_LEVEL_Q = CMPPrint.CMP_QRCODE_EC_LEVEL_Q;
public int CMP_QRCODE_EC_LEVEL_H = CMPPrint.CMP_QRCODE_EC_LEVEL_H;
private ESCPOSPrinter posPtr;
private ESCPOSPrinter posPtr1;
// 0x1B
private final char ESC = ESCPOS.ESC;
public CitizenESCP()
{
posPtr = new ESCPOSPrinter();
}
public String OneRow(String Test) {
return ESC+"|cA" + Test + "\n";
}
public void sample1() throws UnsupportedEncodingException {
Log.i("sample1", "für printNormal");
posPtr.printNormal(ESC+"|cA"+ESC+"|2CReceipt\r\n\r\n\r\n");
posPtr.printNormal(ESC+"|rATEL (123)-456-7890\n\n\n");
posPtr.printNormal(ESC+"|cAThank you for coming to our shop!\n");
posPtr.printNormal(ESC+"|cADate\n\n");
posPtr.printNormal("Chicken $10.00\n");
posPtr.printNormal("Hamburger $20.00\n");
posPtr.printNormal("Pizza $30.00\n");
posPtr.printNormal("Lemons $40.00\n");
posPtr.printNormal("Drink $50.00\n");
posPtr.printNormal("Excluded tax $150.00\n");
posPtr.printNormal(ESC+"|uCTax(5%) $7.50\n");
posPtr.printNormal(ESC+"|bC"+ESC+"|2CTotal $157.50\n\n");
posPtr.printNormal("Payment $200.00\n");
posPtr.printNormal("Change $42.50\n\n");
posPtr.printBarCode("{Babc456789012", CMPPrint.CMP_BCS_Code128, 40, 512, CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_HRI_TEXT_BELOW); // Print Barcode
}
public void sample2() throws UnsupportedEncodingException {
posPtr.printText("Receipt\r\n\r\n\r\n", CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_2WIDTH);
posPtr.printText("TEL (123)-456-7890\r\n", CMPPrint.CMP_ALIGNMENT_RIGHT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Thank you for coming to our shop!\r\n", CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Chicken $10.00\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Hamburger $20.00\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Pizza $30.00\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Lemons $40.00\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Drink $50.00\r\n\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Excluded tax $150.00\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Tax(5%) $7.50\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_UNDERLINE, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Total $157.50\r\n\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_2WIDTH);
posPtr.printText("Payment $200.00\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printText("Change $42.50\r\n\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
posPtr.printBarCode("0123456789", CMPPrint.CMP_BCS_Code39, 40, 512, CMPPrint.CMP_ALIGNMENT_CENTER, CMPPrint.CMP_HRI_TEXT_BELOW);
}
public void imageTest() throws IOException {
posPtr.printBitmap("//sdcard//temp//citizen//car_s.jpg", CMPPrint.CMP_ALIGNMENT_CENTER);
posPtr.printBitmap("//sdcard//temp//citizen//danmark_windmill.jpg", CMPPrint.CMP_ALIGNMENT_LEFT);
posPtr.printBitmap("//sdcard//temp//citizen//denmark_flag.jpg", CMPPrint.CMP_ALIGNMENT_RIGHT);
}
public void westernLatinCharTest() throws UnsupportedEncodingException {
final char [] diff = {0x23,0x24,0x40,0x5B,0x5C,0x5D,0x5E,0x6C,0x7B,0x7C,0x7D,0x7E,
0xA4,0xA6,0xA8,0xB4,0xB8,0xBC,0xBD,0xBE};
String ad = new String(diff);
posPtr.printText(ad+"\r\n\r\n", CMPPrint.CMP_ALIGNMENT_LEFT, CMPPrint.CMP_FNT_DEFAULT, CMPPrint.CMP_TXT_1WIDTH);
}
public int multiply(int x){
return 10*(x+25);
}
@SuppressWarnings("static-access")
public void barcode2DTest(final BA ba) throws UnsupportedEncodingException {
Runnable r = new Runnable() {
@Override
public void run() {
posPtr1 = new ESCPOSPrinter();
String data = "ABCDEFGHIJKLMN";
try {
posPtr1.printString("PDF417 1\r\n");
posPtr1.printPDF417(data, data.length(), 2, 20, ESCPOSConst.CMP_ALIGNMENT_LEFT);
posPtr1.printString("PDF417 2\r\n");
posPtr1.printPDF417(data, data.length(), 10, 30, ESCPOSConst.CMP_ALIGNMENT_LEFT);
posPtr1.printString("QR 1\r\n");
posPtr1.printQRCode(data, data.length(), 3, ESCPOSConst.CMP_QRCODE_EC_LEVEL_L, ESCPOSConst.CMP_ALIGNMENT_CENTER);
posPtr1.printString("QR 2\r\n");
posPtr1.printQRCode(data, data.length(), 11, ESCPOSConst.CMP_QRCODE_EC_LEVEL_L, ESCPOSConst.CMP_ALIGNMENT_CENTER);
}
catch (Exception e) {
ba.setLastException(e);
}
}
};
ba.submitRunnable(r, this, 0);
}
}
Nothing works :BangHead:
Ill hope somebody can help me. I don't want to give up!!!:cool:
Link to Citizen Download for Android
[Code]
http://www.citizen-europe.com/de/printer/support/software/Mobile
Best regards from Germany, good Night!