Table of Content

Install & Usage

Develop

F&Q

SpiritMobile Android SDK user manual

For SpiritMobile Android SDK, please use the following link download.

At the same time, we provide the open source Native demo program and H5 demo program on gitee

Catalog

Android Native SDK


Configure AndroidStudio

Obtain the SDK and unzip it, copy spirit-release.arr to the libs directory of the android project, such as: project directory/app/libs.

Add in build.gradle:

android {
  ...
    repositories {
        flatDir {dirs 'libs'}
    }
  ...
}

dependencies {
...
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation (name:'spirit-release', ext:'aar')
...
}

Add license

In the appliction section of the AndroidManifest.xml of the project, add:

<meta-data android:name="cn.printspirit.LICENSE_ID" android:value="your sdk license id" />

SDK LICENSE_ID needs to apply on this website. For detailed operation, please refer to How to obtain SDK LICENSE_ID

Write printing program

The typical code is as follows:

import com.printspirit.spirit.Printer;
...

//Set the available printer model, if not set, the printer supported by the system can be used.
Printer.addPrinter("bluetooth id", new Printer.PrnOpt(203, 76f, 0, Printer.INS_SET.CPCL));

//Set the URL of the label template server, If you don't set it, it means this website
Printer.setUrl("http://192.168.1.1/api/load-template?id=");
final AsyncTask<Integer,Integer,Integer> th=new AsyncTask<Integer, Integer, Integer>() {
    String errmsg;
        
    @Override
    protected Integer doInBackground(Integer...params) {
        try {
            /* get printer instance */
            Printer prn=Printer.getInstance();
            if (prn.open(MainActivity.this, false)) {
                //Print labels, you can also perform other printing actions.
                Map<String, Object> vars=new HashMap<String, Object>();
                vars.put("var1", "value1");
                vars.put("var2", "value1");
                prn.printLabel("label-id", vars);
                //Close the device
                prn.close();
            } else {
                errmsg = "Please connect the printer first";
                Intent intent = new Intent(MainActivity.this, ConnectActivity.class);
                startActivity(intent);
            }
        } catch (SpiritException e) {
            errmsg = e.getMessage();
        }
        return 0;
    }

    @Override
    protected void onPostExecute(Integer result) {
        showToast(errmsg);
    }
};
...

Connect the printer Printer#open()

Before printing, you must use the Printer#open() method to open the printing device. The SDK will automatically try to connect to print. If you encounter the first use (no printer is configured) or the printer is shut down, the open() method will return false. Intent starts the com.printspirit.spirit.ConnectActivity Bluetooth printer connection interface, searches for, selects, and connects to the printer. Note: Some mobile phones need to turn on the Bluetooth device and pair the printer in advance.

The printers currently supported by the platform include: BTP-P33, BTP-P36, QY-386, etc. The SDK will automatically determine the printing type through the Bluetooth identification of the printer, including: instruction set, resolution DPI, effective width, built-in fonts and other parameters. If your printer is out of the above range, but the instruction set is ZPL/CPLC, you can manually set the printer parameters through addPorinter API.

Note: Printer#open, Printer#PrintLabel and other functions have network operations and cannot be run in the UI thread.

prn.printLabel(tpid, vars) Finish label printing.

The first parameter of this function is the template ID, which is the ID generated after editing and saving the template on this website, for example: 3310aadd-086a-42b7-a591-7265f7ec00e8, the SDK will automatically download the template definition from the website. The second parameter is a template variable, which will automatically replace the position in the template like {{.var1}} with the variable.

You can also directly call the original command to print without using the template printing method.

H5/JS SDK

In order to facilitate the use of H5/JS to develop mobile applications, SpiritMobile provides the function of injecting SPIRIT objects into the webview. You can use the javascript API to control the printer javascript, the API is basically the same as the desktop platform, which is convenient for program migration API function want to see

android program modification

At present, H5 hybrid development is more popular, and Android developers generally have their own relatively complete webview package in their hands. Therefore, we only provide programs related to the printSpirit, which are integrated into their own APP by the developers themselves. Just add the following three lines of code when initializing WebView.

...
import com.printspirit.spirit.JsInterface;
...
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.addJavascriptInterface(new JsInterface(mWebview), "_SPIRIT");

Among them: mWebview is an instance of Webview.

There is a complete example on the gitee website.

Note: When packaging the APP, you need to set the license in the same way as Native development.

js program development

  1. Introduce spirit.js.
  2. Call API to finish printing.

JAVA API


Printer Printer.getInstance();

Obtain a printer instance.

Printer prn=Printer.getInstance();

Printer.getInstance() returns an instance of the printer. The following operations are based on this instance. This manual will record it as Printer#method(), which is mainly different from calling static methods.

boolean Printer.addPrinter();

boolean Printer#open(boolean usecache, String fontname);

Printer.clearCache()

Clear the template cache. After the template is modified, call the modified method to clear the cache.


Leave Your Message

login