Usage instructions of the Android SDK

Source: Internet
Author: User

Usage instructions of the Android SDK

The Android SDK of GPRS inter is designed to develop and use Jiabo printers on the Android platform faster and more efficiently. If you encounter a problem or find a BUG when using the SDK, leave a message.

1. Download maid

You can call 0756-3866865 to obtain the information of the customer.

2. Install the Gplink Printer Driver

  Install gplink.apk on your mobile phone or tablet. Gplink provides the printing service, which can solve the problem that multiple applications on a mobile phone call a printer at the same time.

3. Import Android routine

  In eclipse, import the GPRS interclient project. The following describes this routine. You can study this routine before development.

4. gprinter-v2.0.jar and commons-lang-2.6 documents

  You can see that there are two files gprinter-v2.0.jar and commons-lang-2.6, 1

Figure 1

  Gprinter-v2.0.jar is used to provide the API required for printing, detailed descriptions of the API can be viewed Manual

  The commons-lang-2.6.JAR is used to handle base64 operations.

If you are using eclipse development, create a project, copy the gprinter-v2.0.jar and commons-lang-2.6 to the libs folder of the project.

5. GpService. aidl File

  In scr, there is a GpService. aidl file in the com. uplinter. aidl package, which is used to interact with the services provided by Gplink. 2

Figure 2

For details about the GpService. aidl file, refer to the GpService. aidl instruction document in the folder of GPRS intersdkv2.0.

Package com. uplinter. aidl;
Interface GpService {

Void openPortConfigurationDialog (); // open the port connection dialog box
Int printeTestPage (int PrinterId); // print the test page
Int queryPrinterStatus (int PrinterId); // query the printer status
Int getPrinterCommandType (int PrinterId); // query the printer instruction type
Int sendEscCommand (int PrinterId, String b64); // send the ESC command
Int sendTscCommand (int PrinterId, String b64); // send the TSC command
}

  If you are using eclipse for development, add the com. uplinter. aidl package to src of the project and add the GpService. aidl file to the package.

6. Bind a printing service

As you can see, GpService is bound in the onCreate method of MainAcitivty. java.

 

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. gpinter. aidl. gpPrintService "); bindService (intent, conn, Context. BIND_AUTO_CREATE); // bind Service} class PrinterServiceConnection implements ServiceConnection {@ Override public void onServiceDisconnected (ComponentName name) {Log. I ("ServiceConnection", "onServiceDisconnected () called"); mGpService = null ;}@ Override public void onServiceConnected (ComponentName, IBinder service) {mGpService = GpService. stub. asInterface (service );}};

7. Printer operations

1. In the openPortDialogueClicked method, you can open the port connection dialog box.

Public void openPortDialogueClicked (View view) {try {mGpService. openPortConfigurationDialog (); // when you click to connect to the printer, you must connect to the printer without flashing the wrong light.} catch (RemoteException e1) {// TODO Auto-generated catch block e1.printStackTrace ();}}

After the dialog box is opened, you can see the following figure. 3. This dialog box can be used to connect Bluetooth, USB, and network port printers.

Figure 3

When the printer is started, select the correct port. If it is a usb interface, connect the otg adapter to the usb printer. Click the connect button to connect to the Jiabo printer.

2. Print the test page in 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) {// display the error message Toast. makeText (getApplicationContext (), GpCom. getErrorText (r), Toast. LENGTH_SHORT ). show () ;}} catch (RemoteException e1) {// TODO Auto-generated catch block e1.printStackTrace ();}}

Test page content 4 if a ticket printer is connected:

Figure 4

If a label printer is connected, test page content 5:

Figure 5

3. You can get the current printer status in getPrinterStatusClicked. The printer status needs to be obtained before each print operation. The printer can send the command to the printer normally.

Public void getPrinterStatusClicked (View view) {try {int status = mGpService. queryPrinterStatus (mPrinterIndex); String str = new String (); if (status = GpCom. STATE_NO_ERR) {str = "Printer normal";} else if (byte) (status & GpCom. STATE_OFFLINE)> 0) {str = "Printer offline";} else if (byte) (status & GpCom. STATE_PAPER_ERR)> 0) {str = "printer paper shortage";} else if (byte) (status & GpCom. STATE_COVER_OPEN)> 0) {str = "";} else if (byte) (status & GpCom. STATE_ERR_OCCURS)> 0) {str = "Printer error";} Toast. makeText (getApplicationContext (), "printer:" + '0' + "status:" + str, Toast. LENGTH_SHORT ). show () ;} catch (RemoteException e1) {// TODO Auto-generated catch block e1.printStackTrace ();}}

4. In getPrinterCommandTypeClicked, you can obtain the printer command type. The ticket printer is an ESC command, and the label printer is a TSC command.

Public void getPrinterCommandTypeClicked (View view) {try {int type = mGpService. getPrinterCommandType (mPrinterIndex); if (type = GpCom. ESC_COMMAND) {Toast. makeText (getApplicationContext (), "the printer uses the ESC command", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (getApplicationContext (), "printer uses TSC command", Toast. LENGTH_SHORT ). show () ;}} catch (RemoteException e1) {// TODO Auto-generated catch block e1.printStackTrace ();}}

5. Send the ticket in printReceiptClicked and query the printer status before each printing.

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) {sendreceept ();} else {Toast. makeText (getApplicationContext (), "Printer error! ", Toast. LENGTH_SHORT). show () ;}} catch (RemoteException e1) {// TODO Auto-generated catch block e1.printStackTrace ();}}

Edit the content of the ticket. Follow the ESC command. For details, refer to the EscComand API description file.

Void sendReceipt () {EscCommand esc = new EscCommand (); esc. addPrintAndFeedLines (byte) 3); esc. addSelectJustification (JUSTIFICATION. CENTER); // set to print the CENTER esc. addSelectPrintModes (FONT. FONTA, ENABLE. OFF, ENABLE. ON, ENABLE. ON, ENABLE. OFF); // set it to double the height of the esc. addText ("Sample \ n"); // print the text esc. addPrintAndLineFeed ();/* print text */esc. addSelectPrintModes (FONT. FONTA, ENABLE. OFF, ENABLE. OFF, ENABLE. OFF, ENABLE. OFF); // cancel the double height e SC. addSelectJustification (JUSTIFICATION. LEFT); // set to print the LEFT-aligned esc. addText ("Print text \ n"); // Print the text esc. addText ("Welcome to use uplinter! \ N "); // Print the text esc. addPrintAndLineFeed ();/* Print the image */esc. addText (" Print bitmap! \ N "); // print the text Bitmap B = BitmapFactory. decodeResource (getResources (), R. drawable. gpinter); esc. addRastBitImage (B, B. getWidth (), 0); // print an image/* print a one-dimensional bar code */esc. addText ("Print code128 \ n"); // Print the text esc. addselectprintingpositionforhrichard acters (HRI_POSITION.BELOW); // set the bar code to recognize the character position under the bar code esc. addSetBarcodeHeight (byte) 60); // set the bar code height to 60 o'clock esc. addCODE128 ("uplinter"); // print the Code128 code esc. addPrintAndLineFeed ();/* QRCode command This command can only be used on models that support QRCode command printing. On a model that does not support QR code command printing, you need to send a two-dimensional barcode image */esc. addText ("Print QRcode \ n"); // Print the text esc. addSelectErrorCorrectionLevelForQRCode (byte) 0x31); // sets the error correction level (esc. addSelectSizeOfModuleForQRCode (byte) 3); // you can specify the size of the qrcode module. addStoreQRCodeData ("www.uplinter.com.cn"); // sets the qrcode content esc. addPrintQRCode (); // print QRCode esc. addPrintAndLineFeed ();/* print text */esc. addSelectJustification (JUSTIFICATION. CENTER); // set to print the left-aligned esc. addText ("Complet Ed! \ R \ n "); // print the end esc. addPrintAndLineFeed (); Vector <Byte> datas = esc. getCommand (); // send Data 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 ();}}

Print effect 6

Figure 6

6. Send the label in printLabelClicked and query the printer status before each printing.

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 (), "Printer error! ", Toast. LENGTH_SHORT). show () ;}} catch (RemoteException e1) {// TODO Auto-generated catch block e1.printStackTrace ();}}

Edit the TAG content and send it according to the TSC command. For details, refer to the TscComand API description file.

Void sendLabel () {TscCommand tsc = new TscCommand (); tsc. addSize (60, 60); // set the label size and tsc according to the actual size. addGap (0); // set the label gap according to the actual size. If there is no gap paper, set it to 0 tsc. addDirection (DIRECTION. BACKWARD, MIRROR. NORMAL); // set the printing direction to tsc. addReference (0, 0); // sets the origin coordinate tsc. addTear (ENABLE. ON); // enable tsc In the tearing mode. addCls (); // clear the print buffer // draw a simplified Chinese tsc. addText (20, 20, FONTTYPE. SIMPLIFIED_CHINESE, ROTATION. ROTATION_0, FONTMUL. MUL_1, FONTMUL. MUL_1, "Welcome Use uplinter! "); // Draw the image Bitmap B = BitmapFactory. decodeResource (getResources (), R. drawable. gpinter); tsc. addBitmap (20, 50, BITMAP_MODE.OVERWRITE, B. getWidth (), B); // draw a two-dimensional barcode. This command is only applicable to some models. addQRCode (250, 80, EEC. LEVEL_L, 5, ROTATION. ROTATION_0, "www.uplinter.com.cn"); // draw a one-dimensional Barcode tsc. add1DBarcode (20,250, BARCODETYPE. CODE128, 100, READABEL. EANBEL, ROTATION. ROTATION_0, "uplinter"); tsc. addPrint (); // print the tag tsc. addS Ound (2,100); // print the tag and the buzzer rings the Vector <Byte> datas = tsc. getCommand (); // send Data 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 ();}}

Figure 7

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.