Gprinter Android SDK V2.0 使用說明


     Gprinter Android SDK旨在佳博用戶更快速,更高效的在Android平台下開發和使用佳博打印機。如果您在使用SDK中碰到問題,或者發現BUG,請留言

一、下載GprinterSDKV2.0 

  GprinterSDKV2.0可打電話到0756-3866865,填寫客戶資料后,即可獲得。

二、安裝Gplink打印機驅動

  在GprinterSDKV2.0文件夾中,可以看到Gplink.apk的軟件,在手機或平板上安裝此軟件。Gplink提供打印服務。

三、導入Android例程

  在eclipse,導入GprinterClient工程,以下對此例程進行說明,用戶開發時可以先研究此例程,再進行開發

四、gprinter-v2.0.jar和commons-lang-2.6文件

  可以看到在GprinterClient工程中可以開到libs文件夾中有兩個文件gprinter-v2.0.jar和commons-lang-2.6,如圖1

 

圖1

  gprinter-v2.0.jar,是用來提供打印時所需的API,API的詳細說明可以查看手冊

  commons-lang-2.6.JAR,是用來處理base64的操作。

  如果是用eclipse開發,新建工程,將gprinter-v2.0.jar和commons-lang-2.6拷貝到工程的libs文件夾下即可。

五、GpService.aidl文件

  可以看到在scr中,com.gprinter.aidl包中有個GpService.aidl的文件,是用來和Gplink提供的服務進行交互的,如圖2

圖2

  GpService.aidl文件說明,具體的方法說明請看 GprinterSDKV2.0文件夾中的GpService.aidl說明文檔

package com.gprinter.aidl;
interface GpService{

  void openPortConfigurationDialog();  //打開端口連接對話框
  int printeTestPage(int PrinterId);  // 打印測試頁
  int queryPrinterStatus(int PrinterId); //查詢打印機狀態
  int getPrinterCommandType(int PrinterId); //查詢打印機指令類型
  int sendEscCommand(int PrinterId, String b64);//發送ESC指令
  int sendTscCommand(int PrinterId, String b64); //發送TSC指令
}

  如果是用eclipse開發,在工程的src中增加com.gprinter.aidl包,在包中加入GpService.aidl文件即可

六、綁定打印服務

  可以看到,在MainAcitivty.java的onCreate方法中的對GpService進行了綁定

 
         

 private GpService mGpService;
 private static final String DEBUG_TAG = "MainActivity";
 private PrinterServiceConnection conn = null;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(DEBUG_TAG, "onCreate");
        connection();
    }    
    private void connection() {
        conn = new PrinterServiceConnection();
        Intent intent = new Intent("com.gprinter.aidl.GpPrintService");
        bindService(intent, conn, Context.BIND_AUTO_CREATE); // 綁定服務
    }    
    class PrinterServiceConnection implements ServiceConnection {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("ServiceConnection", "onServiceDisconnected() called");
            mGpService = null;
        }
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mGpService =GpService.Stub.asInterface(service);
        }
    };

七、實現打印機的操作

  1、在openPortDialogueClicked方法,可以打開端口連接對話框

    public void openPortDialogueClicked(View view) {
        try {
            mGpService.openPortConfigurationDialog(); // 點擊連接打印機時,需要打印機錯誤燈不閃爍的情況下才能連接
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

  打開對話框后,可以看到如下畫面,如圖3,此對話框可以對藍牙、USB、網口打印機進行連接操作。

圖3

打印機開機,選擇正確的端口,如果是usb接口則需要otg的轉接線連接usb打印機。再點擊連接按鈕,則可以連接到佳博打印機。

  2、在printTestPageClicked實現打印測試頁

    public void printTestPageClicked(View view) {
        try {
            int rel = mGpService.printeTestPage(mPrinterIndex); //
            Log.i("ServiceConnection", "rel " + rel);
            GpCom.ERROR_CODE r=GpCom.ERROR_CODE.values()[rel];
            if(r != GpCom.ERROR_CODE.SUCCESS){ //顯示錯誤信息 
                Toast.makeText(getApplicationContext(),GpCom.getErrorText(r),
                        Toast.LENGTH_SHORT).show();    
        }
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

  如果連接的是票據打印機則測試頁內容如圖4:

圖4

  如果連接的是標簽打印機則測試頁內容如圖5:

圖5

     3、在getPrinterStatusClicked中可以獲取打印機當前狀態。每次執行打印之前需獲取打印機狀態,打印機正常才能夠再發送命令給打印機

    public void getPrinterStatusClicked(View view) {
        try {
            int status = mGpService.queryPrinterStatus(mPrinterIndex);
            String str = new String();
            if (status == GpCom.STATE_NO_ERR) {
                str = "打印機正常";
            } else if ((byte) (status & GpCom.STATE_OFFLINE) > 0) {
                str = "打印機脫機";
            } else if ((byte) (status & GpCom.STATE_PAPER_ERR) > 0) {
                str = "打印機缺紙";
            } else if ((byte) (status & GpCom.STATE_COVER_OPEN) > 0) {
                str = "打印機開蓋";
            } else if ((byte) (status & GpCom.STATE_ERR_OCCURS) > 0) {
                str = "打印機出錯";
            }
            Toast.makeText(getApplicationContext(),
                    "打印機:" + '0' + " 狀態:" + str, Toast.LENGTH_SHORT).show();
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

   4、在getPrinterCommandTypeClicked中可以獲取打印機的指令類型,票據打印機為ESC指令,標簽打印機為TSC指令

    public void getPrinterCommandTypeClicked(View view) {
        try {
            int type = mGpService.getPrinterCommandType(mPrinterIndex);
            if (type == GpCom.ESC_COMMAND) {
                Toast.makeText(getApplicationContext(), "打印機使用ESC命令",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "打印機使用TSC命令",
                        Toast.LENGTH_SHORT).show();
            }
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

  5、在printReceiptClicked發送票據,每次打印之前查詢打印機狀態

    public void printReceiptClicked(View view) {
        try {
            int type = mGpService.getPrinterCommandType(mPrinterIndex);
            if (type == GpCom.ESC_COMMAND) {
                int status = mGpService.queryPrinterStatus(mPrinterIndex);  
                if (status == GpCom.STATE_NO_ERR) {
                    sendReceipt();
                }
                else{
                    Toast.makeText(getApplicationContext(),
                            "打印機錯誤!", Toast.LENGTH_SHORT).show();        
                }
            }    
        } 
        catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

  票據的內容編輯,按照ESC指令編寫,可以參考EscComand API說明文件

void sendReceipt(){
        EscCommand esc = new EscCommand();
        esc.addPrintAndFeedLines((byte)3);
        esc.addSelectJustification(JUSTIFICATION.CENTER);//設置打印居中
        esc.addSelectPrintModes(FONT.FONTA, ENABLE.OFF,ENABLE.ON, ENABLE.ON, ENABLE.OFF);//設置為倍高倍寬
        esc.addText("Sample\n");   //  打印文字
        esc.addPrintAndLineFeed();

        /*打印文字*/
        esc.addSelectPrintModes(FONT.FONTA, ENABLE.OFF,ENABLE.OFF, ENABLE.OFF, ENABLE.OFF);//取消倍高倍寬
        esc.addSelectJustification(JUSTIFICATION.LEFT);//設置打印左對齊
        esc.addText("Print text\n");   //  打印文字
        esc.addText("Welcome to use Gprinter!\n");   //  打印文字
        esc.addPrintAndLineFeed();
        /*打印圖片*/
        esc.addText("Print bitmap!\n");   //  打印文字
        Bitmap b = BitmapFactory.decodeResource(getResources(),
                R.drawable.gprinter);
        esc.addRastBitImage(b,b.getWidth(),0);   //打印圖片
    
        /*打印一維條碼*/
        esc.addText("Print code128\n");   //  打印文字
        esc.addSelectPrintingPositionForHRICharacters(HRI_POSITION.BELOW);//設置條碼可識別字符位置在條碼下方
        esc.addSetBarcodeHeight((byte)60); //設置條碼高度為60點
        esc.addCODE128("Gprinter");  //打印Code128碼
        esc.addPrintAndLineFeed();
    
        /*QRCode命令打印
            此命令只在支持QRCode命令打印的機型才能使用。
            在不支持二維碼指令打印的機型上,則需要發送二維條碼圖片
        */
        esc.addText("Print QRcode\n");   //  打印文字    
        esc.addSelectErrorCorrectionLevelForQRCode((byte)0x31); //設置糾錯等級
        esc.addSelectSizeOfModuleForQRCode((byte)3);//設置qrcode模塊大小
        esc.addStoreQRCodeData("www.gprinter.com.cn");//設置qrcode內容
        esc.addPrintQRCode();//打印QRCode
        esc.addPrintAndLineFeed();
        
        /*打印文字*/
        esc.addSelectJustification(JUSTIFICATION.CENTER);//設置打印左對齊
        esc.addText("Completed!\r\n");   //  打印結束
        esc.addPrintAndLineFeed();
        
        Vector<Byte> datas = esc.getCommand(); //發送數據
        Byte[] Bytes = datas.toArray(new Byte[datas.size()]);
        byte[] bytes = ArrayUtils.toPrimitive(Bytes);
        String str = Base64.encodeToString(bytes, Base64.DEFAULT);
        int rel;
        try {
            rel = mGpService.sendEscCommand(mPrinterIndex, str);
            GpCom.ERROR_CODE r=GpCom.ERROR_CODE.values()[rel];
            if(r != GpCom.ERROR_CODE.SUCCESS){
                Toast.makeText(getApplicationContext(),GpCom.getErrorText(r),
                        Toast.LENGTH_SHORT).show();    
                  }            
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 打印效果如圖6 

圖6

6、在printLabelClicked發送標簽,每次打印之前查詢打印機狀態

    public void printLabelClicked(View view) {
        try {
            int type = mGpService.getPrinterCommandType(mPrinterIndex);
            if (type == GpCom.TSC_COMMAND) {
                int status = mGpService.queryPrinterStatus(mPrinterIndex);  
                if (status ==GpCom.STATE_NO_ERR) {
                    sendLabel();
                }
                else{
                    Toast.makeText(getApplicationContext(),
                            "打印機錯誤!", Toast.LENGTH_SHORT).show();        
                }
            }    
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

  標簽的內容編輯,按照TSC指令發送,可以參考TscComand API說明文件

void sendLabel(){
        TscCommand tsc = new TscCommand();
        tsc.addSize(60, 60); //設置標簽尺寸,按照實際尺寸設置
        tsc.addGap(0);           //設置標簽間隙,按照實際尺寸設置,如果為無間隙紙則設置為0
        tsc.addDirection(DIRECTION.BACKWARD,MIRROR.NORMAL);//設置打印方向
        tsc.addReference(0, 0);//設置原點坐標
         tsc.addTear(ENABLE.ON); //撕紙模式開啟
        tsc.addCls();// 清除打印緩沖區
        //繪制簡體中文
         tsc.addText(20,20,FONTTYPE.SIMPLIFIED_CHINESE,ROTATION.ROTATION_0,FONTMUL.MUL_1,FONTMUL.MUL_1,"Welcome to use Gprinter!");
         //繪制圖片
        Bitmap b = BitmapFactory.decodeResource(getResources(),
                R.drawable.gprinter);
        tsc.addBitmap(20,50, BITMAP_MODE.OVERWRITE, b.getWidth(),b);
        //繪制二維條碼,此命令只在部分機型上適用
        tsc.addQRCode(250, 80, EEC.LEVEL_L,5,ROTATION.ROTATION_0, " www.gprinter.com.cn");    
         //繪制一維條碼
         tsc.add1DBarcode(20,250, BARCODETYPE.CODE128, 100, READABEL.EANBEL, ROTATION.ROTATION_0, "Gprinter");
        tsc.addPrint(1,1); // 打印標簽
        tsc.addSound(2, 100); //打印標簽后 蜂鳴器響

        Vector<Byte> datas = tsc.getCommand(); //發送數據
        Byte[] Bytes = datas.toArray(new Byte[datas.size()]);
        byte[] bytes = ArrayUtils.toPrimitive(Bytes);
        String str = Base64.encodeToString(bytes, Base64.DEFAULT);
        int rel;
        try {
            rel = mGpService.sendEscCommand(mPrinterIndex, str);
            GpCom.ERROR_CODE r=GpCom.ERROR_CODE.values()[rel];
            if(r != GpCom.ERROR_CODE.SUCCESS){
                Toast.makeText(getApplicationContext(),GpCom.getErrorText(r),
                        Toast.LENGTH_SHORT).show();    
                  }            
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 

圖7

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM