SunmiPrinter
Wed Aug 10 2022 01:54:50 GMT+0000 (Coordinated Universal Time)
package com.sunmi.printerhelper.utils; import android.content.Context; import android.os.RemoteException; import android.util.Log; import com.sunmi.peripheral.printer.ExceptionConst; import com.sunmi.peripheral.printer.InnerPrinterCallback; import com.sunmi.peripheral.printer.InnerPrinterException; import com.sunmi.peripheral.printer.InnerPrinterManager; import com.sunmi.peripheral.printer.SunmiPrinterService; import com.sunmi.peripheral.printer.WoyouConsts; /** * @author M. Ali Biag * @since create at Wednesday, August 10, 2022 */ public class SunmiPrinter { public static String TAG = "SunmiPrinter"; public enum ALIGNMENT {LEFT, CENTER, RIGHT} private static final SunmiPrinter printer = new SunmiPrinter(); private SunmiPrinter() { } public static SunmiPrinter instance() { return printer; } /** * SunmiPrinterService is an interface * It contain all the Printer API (Functionalities) * <p> * InnerPrinterCallback gives us the implementation of * SunmiPrinterService interface */ private SunmiPrinterService sunmiPrinterService; private final InnerPrinterCallback innerPrinterCallback = new InnerPrinterCallback() { @Override protected void onConnected(SunmiPrinterService service) { sunmiPrinterService = service; } @Override protected void onDisconnected() { sunmiPrinterService = null; } }; /** * Check {@link ExceptionConst} */ private void handleRemoteException(RemoteException e) { Log.e(TAG, "ERROR FROM SUNMI PRINTER CUSTOM IMPLEMENTATION: " + e.getMessage()); } /** * Establish link with Sunmi Printer. * NOTE: * If you do not call this method then * SunmiPrinterService will be null * and you will not be able to access any * functionality of printer */ public void connectWithSunmiPrinter(Context context) { try { boolean ret = InnerPrinterManager.getInstance().bindService(context, innerPrinterCallback); if (!ret) { // Connection not established } } catch (InnerPrinterException e) { e.printStackTrace(); } } /** * Disconnect from Sunmi printer from device * and release resources */ public void disconnectFromSunmiPrinter(Context context) { try { if (sunmiPrinterService != null) { InnerPrinterManager.getInstance().unBindService(context, innerPrinterCallback); sunmiPrinterService = null; } } catch (InnerPrinterException e) { e.printStackTrace(); } } /** * All style settings will be restored to default */ public void setPrinterToDefaultStyle() { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.printerInit(null); } catch (RemoteException e) { handleRemoteException(e); } } /** * send esc cmd */ public void sendRawData(byte[] data) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter(); return; } try { sunmiPrinterService.sendRAWData(data, null); } catch (RemoteException e) { handleRemoteException(e); } } /** * Set printer alignment */ public void setAlignment(ALIGNMENT align) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.setAlignment(align.ordinal(), null); } catch (RemoteException e) { handleRemoteException(e); } } public void setFontSize(float fontSize) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.setFontSize(fontSize, null); } catch (RemoteException e) { handleRemoteException(e); } } /** * @param text text you want to print */ public void printText(String text) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.printText(text, null); } catch (RemoteException e) { e.printStackTrace(); } } /** * @param boldText After printing given text bold feature will be off. */ public void printTextBold(String boldText) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.sendRAWData(ESCUtil.boldOn(), null); sunmiPrinterService.printText(boldText, null); sunmiPrinterService.sendRAWData(ESCUtil.boldOff(), null); } catch (RemoteException e) { e.printStackTrace(); } } public void printTextInCenter(String centerText) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.setAlignment(ALIGNMENT.CENTER.ordinal(), null); sunmiPrinterService.printText(centerText, null); sunmiPrinterService.setAlignment(ALIGNMENT.LEFT.ordinal(), null); } catch (RemoteException e) { e.printStackTrace(); } } /** * @param leftText will be Left Align * @param rightText will be Right Align */ public void printTextLeftRight(String leftText, String rightText) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } String[] txts = new String[]{leftText, rightText}; int[] width = new int[]{1, 1}; int[] align = new int[]{0, 2}; try { sunmiPrinterService.printColumnsString(txts, width, align, null); } catch (RemoteException e) { e.printStackTrace(); } } public void printTextFontSize(String text, float fontSize) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.printTextWithFont(text, null, fontSize, null); } catch (RemoteException e) { e.printStackTrace(); } } public void printOneLine() { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.lineWrap(1, null); } catch (RemoteException e) { handleRemoteException(e); } } /** * paper feed three lines * Not disabled when line spacing is set to 0 */ public void print3Line() { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.lineWrap(3, null); } catch (RemoteException e) { handleRemoteException(e); } } /** * Print dashes on one complete line * Handle both 58mm and 80mm paper */ public void dashesPlusNextLine() { try { int paper = sunmiPrinterService.getPrinterPaper(); if (paper == 1) { // 32 Dashes = 58mm sunmiPrinterService.printText("--------------------------------\n", null); } else { // 48 Dashes = 80mm sunmiPrinterService.printText("------------------------------------------------\n", null); } } catch (RemoteException e) { e.printStackTrace(); } } public String getPaperSize() { String result = "Unknown"; try { int paper = sunmiPrinterService.getPrinterPaper(); if (paper == 1) { result = "58mm"; } else { result = "80mm"; } } catch (RemoteException e) { e.printStackTrace(); } return result; } /** * print Qr Code */ public void printQr(String data, int modulesize, int errorlevel) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { sunmiPrinterService.printQRCode(data, modulesize, errorlevel, null); } catch (RemoteException e) { e.printStackTrace(); } } /** * print text * setPrinterStyle:Api require V4.2.22 or later, So use esc cmd instead when not supported * More settings reference documentation {@link WoyouConsts} * printTextWithFont: * Custom fonts require V4.14.0 or later! * You can put the custom font in the 'assets' directory and Specify the font name parameters * in the Api. */ public void printText(String content, float size, boolean isBold, boolean isUnderLine, String typeface) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return; } try { try { sunmiPrinterService.setPrinterStyle(WoyouConsts.ENABLE_BOLD, isBold ? WoyouConsts.ENABLE : WoyouConsts.DISABLE); } catch (RemoteException e) { if (isBold) { sunmiPrinterService.sendRAWData(ESCUtil.boldOn(), null); } else { sunmiPrinterService.sendRAWData(ESCUtil.boldOff(), null); } } try { sunmiPrinterService.setPrinterStyle(WoyouConsts.ENABLE_UNDERLINE, isUnderLine ? WoyouConsts.ENABLE : WoyouConsts.DISABLE); } catch (RemoteException e) { if (isUnderLine) { sunmiPrinterService.sendRAWData(ESCUtil.underlineWithOneDotWidthOn(), null); } else { sunmiPrinterService.sendRAWData(ESCUtil.underlineOff(), null); } } sunmiPrinterService.printTextWithFont(content, typeface, size, null); } catch (RemoteException e) { e.printStackTrace(); } } public boolean doesPrinterExist(Context context) { if (sunmiPrinterService == null) { // disconnectFromSunmiPrinter() return false; } boolean result = false; try { int code = sunmiPrinterService.updatePrinterState(); if (code > 0 && code < 505) { // "Printer exist" result = true; } else { // "Printer does not exist" result = false; } } catch (RemoteException e) { e.printStackTrace(); } return result; } }
Comments