Android Question Sumni Printing via Sumni Printing API

Hi guys I been trying to print on the sumni devices I spoke to guys who have got it working on andriod studio and they shared there code would anyone know how to get it work I have got the printing working with wangpos and I think its similar I share that code in my next post its half java and b4x but I really hoping someone has wrapped the SUMNI Printing API
Java:
import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.InnerResultCallback;
import com.sunmi.peripheral.printer.SunmiPrinterService;

public class Printer {

private static String TAG = "Printer";
private String printerExceptionMessage;
private String lastFunctionProcessed;
private SunmiPrinterService mSunmiPrinterService;

public int init(Context context) {
try {
boolean result = InnerPrinterManager.getInstance().bindService(context, innerPrinterCallback);
} catch (InnerPrinterException e) {
Log.e(TAG, "Printer initialise error: " + e);
return -1;
}
return 0;
}

private InnerPrinterCallback innerPrinterCallback = new InnerPrinterCallback() {
@override
protected void onConnected(SunmiPrinterService service) {
Log.i(TAG, "Printer connected");
mSunmiPrinterService = service;
}

@override
protected void onDisconnected() {
Log.i(TAG, "Printer disconnected");
}
};

private InnerResultCallback printFinishedCallback = new InnerResultCallback() {
@override
public void onRunResult(boolean isSuccess) throws RemoteException {
Log.i(TAG, "printFinishedCallback: onRunResult=" + isSuccess);
}

@override
public void onReturnString(String result) throws RemoteException {
Log.i(TAG, "printFinishedCallback: onReturnString=" + result);
}

@override
public void onRaiseException(int code, String msg) throws RemoteException {
Log.i(TAG, "printFinishedCallback: onRaiseException=" + msg);
}

@override
public void onPrintResult(int code, String msg) throws RemoteException {
Log.i(TAG, "printFinishedCallback: onPrintResult=" + msg);
}
};

private InnerResultCallback printResultCallback = new InnerResultCallback() {
@override
public void onRunResult(boolean isSuccess) throws RemoteException {
Log.i(TAG, "printResultCallback: onRunResult = " + isSuccess);
}

@override
public void onReturnString(String result) throws RemoteException {
Log.i(TAG, "printResultCallback: onReturnString = " + result);
}

@override
public void onRaiseException(int code, String msg) throws RemoteException {
Log.i(TAG, "printResultCallback: onRaiseException = " + msg);
printerExceptionMessage = msg;
}

@override
public void onPrintResult(int code, String msg) throws RemoteException {
Log.i(TAG, "printResultCallback: onPrintResult = " + msg);
}
};

public String getPrinterExceptionMessage() {
return printerExceptionMessage;
}

private int getPrinterStatus() {
printerExceptionMessage = "";

if (mSunmiPrinterService == null) {
Log.i(TAG, "Error, mSunmiPrinterService == null");
printerExceptionMessage = "Printer service not initialised";
return -1;
}

try {
int printerStatus = mSunmiPrinterService.updatePrinterState();

Log.i(TAG, "printerStatus=" + printerStatus);
switch (printerStatus) {
case 1: // Normal
case 2: // under preparation
break;
case 3:
printerExceptionMessage = "Printer comms error";
return -2;
case 4:
printerExceptionMessage = "Printer is out of paper";
return -3;
case 5:
printerExceptionMessage = "Printer overheated";
return -4;
case 6:
printerExceptionMessage = "Printer cover open";
return -5;
case 7:
printerExceptionMessage = "Cutter error";
return -6;
case 8: // cutter recovered
case 9: // black mark not detected
break;
case 505:
printerExceptionMessage = "No printer detected";
return -7;
case 507:
printerExceptionMessage = "Printer firmware update failed";
return -8;
}
}
catch (Exception e) {
printerExceptionMessage = e.getMessage();
Log.e(TAG, "getPrinterStatus exception: " + e);
return -10;
}

return 0;
}

public int printBitmap(Bitmap bitmap) {
Log.i(TAG, "printBitmap:" + bitmap.toString());

int printerStatus = getPrinterStatus();
if (printerStatus != 0) return printerStatus;

try {
mSunmiPrinterService.lineWrap(1, printResultCallback);
mSunmiPrinterService.printBitmap(bitmap, printResultCallback);
mSunmiPrinterService.lineWrap(1, printResultCallback);
mSunmiPrinterService.commitPrinterBufferWithCallback(printFinishedCallback);
} catch (RemoteException e) {
Log.e(TAG, "printBitmap Exception:" + e);
return -10;
}

return 0;
}

}
 
for referance here is the wangpos in java and b4x x if anyone wants a working example
B4X:
#AdditionalJar: SDK4BaseBinderV2.2.12.jar
#AdditionalJar: SDK4EmvBinderV2.2.13.jar
#AdditionalJar: SDK4KeyManagerBinderV2.2.06.jar



Sub InitPrinter()
    NativeMe.RunMethod("_initprint",Null)
End Sub

Sub PrintStringLeft(s As String)
    NativeMe.RunMethod("_printLeft",Array As Object(s,FontSize,Bold))
    FontSize = 22
    Bold = False
End Sub


Sub PrintStringLeftFontAndBold(s As String)
    FontSize = 26
    Bold = True
    NativeMe.RunMethod("_printLeft",Array As Object(s,FontSize,Bold))
    FontSize = 22
    Bold = False
End Sub


Sub PrintStringRight(s As String)
    NativeMe.RunMethod("_printRight",Array As Object(s,FontSize,Bold))
    FontSize = 22
    Bold = False
End Sub

Sub PrintStringCenter(s As String)
    NativeMe.RunMethod("_printCenter",Array As Object(s,FontSize,Bold))
    FontSize = 22
    Bold = False
End Sub


Sub PrintLeftandRight(s As String,s1 As String)
    
    Log("logS1=" & s1 & "logs=" & s)
    
    NativeMe.RunMethod("_printLeftandRight",Array As Object(s,s1,FontSize))
    FontSize = 22
    Bold = False
End Sub




Sub EndPrinter()
    NativeMe.RunMethod("_postprint",Null)
    Reprint = "Reprint"
End Sub



#if Java

import android.content.Context;
import android.os.RemoteException;


import wangpos.sdk4.libbasebinder.Printer;
import wangpos.sdk4.libbasebinder.Printer.Align;


public boolean _onCreateOptionsMenu(android.view.Menu menu) {
     processBA.raiseEvent(null, "create_menu", menu);
     return true;
    
}


Printer mPrinter;

public void init()
{
      new Thread()
      {     
      @Override
          public void run()
          {
          
            BA.Log("Printer A");   
            mPrinter = new Printer(getApplicationContext());
            BA.Log("Printer B");
          }
    }.start();
}

public void _initprint()
{   
    try
    {
        mPrinter.printInit();
        mPrinter.clearPrintDataCache();
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
}

public void _printLeft(String _text,int _fontsize ,boolean  _bold)
{
    try
    {
        mPrinter.printString(_text, _fontsize,  Printer.Align.LEFT, _bold, false);
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
}

public void _printRight(String _text,int _fontsize ,boolean  _bold)
{
    try
    {
        mPrinter.printString(_text, _fontsize,  Printer.Align.RIGHT, _bold, false);
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
}

public void _printCenter(String _text,int _fontsize ,boolean  _bold)
{
    try
    {
        mPrinter.printString(_text, _fontsize,  Printer.Align.CENTER, _bold, false);
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
}

public void _printLeftandRight(String _text, String _textx, int _fontsize)
{
    try
    {
        BA.Log("Start");   
        mPrinter.print2StringInLine(_text,_textx,1,Printer.Font.DEFAULT, _fontsize, Printer.Align.LEFT, false, false, false);       
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
}

public void _postprint()
{
    try
    {
        mPrinter.printPaper(100);
        mPrinter.printFinish();
    }
    catch (RemoteException e)
    {
        e.printStackTrace();
    }
}

#End If
#End Region

'Program code should go into B4XMainPage and other pages.
 
Upvote 0
Top