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
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLwAjM4QjMzUDM0UTOwATMvw1MwUTMwIzLcNzM2YDNz8CX1EDMyc2bsJ2Lc12bj5ycn9Gbi52YuAzcldWYtl2Lc9CX6MHc0RHaiojIsJye.png)
圖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
轉載于:https://www.cnblogs.com/MrDing/p/4325618.html