Gprinter Android SDK旨在佳博用戶更快速,更高效的在Android平台下開發和使用佳博打印機。如果您在使用SDK中碰到問題,或者發現BUG,請留言
一、下載GprinterSDKV2.1
GprinterSDKV2.1可打電話到0756-3866865,填寫客戶資料后,即可獲得。
二、新建工程,導入gprinter-2.1.jar包
向工程的libs文件中拷貝gprinter-2.1.jar,android sdk API >= 14
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
三、注冊服務和權限
注冊服務在AndroidManifest文件中添加服務,gprinter-2.1.jar中提供了打印服務
<service android:name="com.gprinter.service.GpPrintService" android:label="GpPrintService" android:process=":remote" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="com.gprinter.aidl.GpPrintService" /> </intent-filter> </service>
注冊權限
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-feature android:name="android.hardware.usb.host" /> <uses-permission android:name="android.hardware.usb.accessory" /> <uses-permission android:name="android.permission.DEVICE_POWER"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
四、添加aidl文件
新建包,包名為com.gprinter.aidl,向包中添加文件,文件名為GpService。aidl內容為
package com.gprinter.aidl; interface GpService{ int openPort(int PrinterId,int PortType,String DeviceName,int PortNumber); void closePort(int PrinterId); int getPrinterConnectStatus(int PrinterId); int printeTestPage(int PrinterId); int queryPrinterStatus(int PrinterId,int Timesout); int getPrinterCommandType(int PrinterId); int sendEscCommand(int PrinterId, String b64); int sendTscCommand(int PrinterId, String b64); }
五、啟動並綁定GpPrintService服務
在OnCreate中啟動並綁定服務
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e(DEBUG_TAG, "onCreate"); startService(); connection(); }
啟動服務
private void startService() { Intent i= new Intent(this, GpPrintService.class); startService(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }
綁定服務
private GpService mGpService = null; private PrinterServiceConnection conn = null;
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); } }; private void connection() { conn = new PrinterServiceConnection(); Intent intent = new Intent("com.gprinter.aidl.GpPrintService"); bindService(intent, conn, Context.BIND_AUTO_CREATE); // bindService }
六、使用打印服務的AIDL接口,接口的詳細描述請看GpService.aidl說明
打開和關閉端口操作
1、 注冊狀態接收廣播,通過此廣播可以獲取當前端口的連接狀態
廣播接收的Intent,
GpPrintService.PRINTER_ID 返回打印機的ID序號
GpPrintService.CONNECT_STATUS 返回狀態
GpPrintService可以同時連接三台打印機,可以通過此廣播獲取到哪台打印機處於何種狀態
public static final String ACTION_CONNECT_STATUS = "action.connect.status";
private void registerBroadcast() { IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_CONNECT_STATUS); this.registerReceiver(PrinterStatusBroadcastReceiver, filter); } private BroadcastReceiver PrinterStatusBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (ACTION_CONNECT_STATUS.equals(intent.getAction())) { int type = intent.getIntExtra(GpPrintService.CONNECT_STATUS, 0); int id = intent.getIntExtra(GpPrintService.PRINTER_ID, 0); Log.d(DEBUG_TAG, "connect status " + type); if (type == GpDevice.STATE_CONNECTING) {
} else if (type == GpDevice.STATE_NONE) { } else if (type == GpDevice.STATE_VALID_PRINTER) { } else if (type == GpDevice.STATE_INVALID_PRINTER) { } } } };
2、調用打開端口 int openPort(int PrinterId,int PortType,String DeviceName,int PortNumber);如果端口已經連接則會返回ERROR_CODE.DEVICE_ALREADY_OPEN,端口打開過程中的連接狀態通過上面的廣播返回
3、調用關閉端口int closePort(int PrinterId);端口關閉過程中的連接狀態通過上面的廣播返回,斷開連接后再次調用連接,需延時1-10s時間再次連接,否則可能連接不上,具體情況視平板固件而定
獲取打印機狀態
1、通過調用 int getPrinterConnectStatus(int PrinterId);可以獲取打印機的連接狀態
2、通過調用 int queryPrinterStatus(int PrinterId,int Timesout);可以獲取打印機的錯誤狀態
try { int status = mGpService.queryPrinterStatus(mPrinterIndex,500); 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(); }
3、通過調用 int getPrinterCommandType(int PrinterId);可以獲取打印機的命令類型,佳博打印機分為票據打印機和標簽打印機
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(); }
打印測試頁
1、如果打印機連接成功后可以通過int printeTestPage(int PrinterId); 打印測試頁。
向打印機發送數據
1、如果連接的為標簽打印機則調用int sendTscCommand(int PrinterId, String b64); 發送數據
2、如果連接的為票據打印機則調用int sendEscCommand(int PrinterId, String b64); 發送數據
發送數據之前 需查詢打印機的狀態
票據打印機測試頁內容如圖1:
圖1
標簽打印機測試頁內容如圖2:
圖2
七、打印機命令格式說明
1、票據的內容編輯,按照ESC指令編寫,可以參考EscComand API說明文件打印效果如圖3
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.addPrintAndFeedLines((byte)8); 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(); }
圖3
2、標簽的內容編輯,按照TSC指令發送,可以參考TscComand API說明文件
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.sendTscCommand(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(); }
圖4