簡體   English   中英

Android-將收據打印到藍牙打印機

[英]Android - Printing receipt to Bluetooth Printer

大多數相關的答案和google都包含有關此主題的相當古老的貢獻。 因此,我正在尋找一種通過藍牙熱敏票據打印機制作我的Android應用程序打印票據(寬度58毫米)的方法。 是否需要使用帶有第三方API的打印機? 我打算購買一台常規的藍牙打印機,該打印機可以連接到我的設備,並使用默認的Android打印管理器為收據進行布局。 那可能嗎? 是否有任何樣本,文檔或教程? 會很感激的。 提前致謝

是的,必須使用第三方SDK。 我建議您使用Zebra打印機,該打印機具有良好的文檔和代碼示例,與其他品牌的打印機相比,它比較容易使用。 此處下載SDK

要制作標簽,您可以使用ZebraDesigner,它使您可以直觀地創建布局,並提供ZPL代碼作為輸出,您可以將其從應用程序發送到藍牙打印機。 單擊此處查看。

您好,這里是一個如何從Cordova應用程序使用斑馬線進行打印的示例

package com.custom.plugin;

import android.os.Build;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.Context;

import android.support.v4.app.ActivityCompat;
import android.os.Bundle;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;

import java.util.Set;

import com.zebra.android.discovery.*;
import com.zebra.sdk.comm.*;
import com.zebra.sdk.printer.*;
import com.zebra.sdk.printer.SGD;

/**
 * This class echoes a string called from JavaScript.
 */
public class PrinterDanBreakingNews extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("getListPairedDevices")) {
            this.getListPairedDevices(callbackContext);
            return true;
        }else if(action.equals("print"))
        {
            this.print(callbackContext,args);
            return true;
        }
        return false;
    }

    private void getListPairedDevices(CallbackContext callbackContext) 
    {    
        try
        {
            BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

            if( !bluetoothAdapter.isEnabled() )
            {
                callbackContext.error("Favor encienda el bluetooth");
            }

            Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

            if (pairedDevices.size() > 0) {

                JSONArray lista = new JSONArray();

                for (BluetoothDevice device : pairedDevices) {
                    String bname = device.getName();
                    String bmac = device.getAddress();
                    String btype
                            = getBTMajorDeviceClass(
                                device.getBluetoothClass().getMajorDeviceClass()
                            );

                    JSONObject item = new JSONObject();

                    item.put("name", bname);
                    item.put("type", btype);
                    item.put("mac", bmac);

                    lista.put(item);

                }

                callbackContext.success(lista.toString());
            }

            callbackContext.error("Error. no hay dispositivos registrados");
        }
        catch( Exception ex )
        {
            callbackContext.error("Error. "  + ex.toString());
        }
    }

    private String getBTMajorDeviceClass(int major){
        switch(major){
            case BluetoothClass.Device.Major.AUDIO_VIDEO:
                return "AUDIO_VIDEO";
            case BluetoothClass.Device.Major.COMPUTER:
                return "COMPUTER";
            case BluetoothClass.Device.Major.HEALTH:
                return "HEALTH";
            case BluetoothClass.Device.Major.IMAGING:
                return "IMAGING";
            case BluetoothClass.Device.Major.MISC:
                return "MISC";
            case BluetoothClass.Device.Major.NETWORKING:
                return "NETWORKING";
            case BluetoothClass.Device.Major.PERIPHERAL:
                return "PERIPHERAL";
            case BluetoothClass.Device.Major.PHONE:
                return "PHONE";
            case BluetoothClass.Device.Major.TOY:
                return "TOY";
            case BluetoothClass.Device.Major.UNCATEGORIZED:
                return "UNCATEGORIZED";
            case BluetoothClass.Device.Major.WEARABLE:
                return "WEARABLE";
            default: return "unknown!";
        }
    }

    public void print(final CallbackContext callbackContext, final JSONArray args){
        try
        {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {

                        String mac = "";
                        String template = "";
                        String length = "";

                        if( args.length() > 0 ) {

                            JSONArray row = args.getJSONArray(0);
                            JSONObject item = row.getJSONObject(0);

                            mac = item.getString("mac");
                            template = item.getString("template");
                            length = item.getString("length");
                        }

                        Connection thePrinterConn = new BluetoothConnectionInsecure(mac);

                        if (isPrinterReady(thePrinterConn)) {
                            thePrinterConn.open();

                            SGD.SET("device.languages", "zpl", thePrinterConn);
                            SGD.SET("ezpl.media_type", "continuous", thePrinterConn);
                            SGD.SET("zpl.label_length", length, thePrinterConn);

                            thePrinterConn.write(template.getBytes());
                            Thread.sleep(500);
                            thePrinterConn.close();

                            callbackContext.success("listo");
                        } else {
                            callbackContext.error("Error3, la impresora no esta lista");
                        }
                    } catch (Exception ex) {
                        callbackContext.error("Error2. "  + ex.toString());
                    }
                }
            }).start();
        }
        catch(Exception ex)
        {
            callbackContext.error("Error1. "  + ex.toString());
        }
    }

    private Boolean isPrinterReady(Connection connection)
            throws Exception {

        Boolean isOK = false;
        connection.open();

        ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
        PrinterStatus printerStatus = printer.getCurrentStatus();
        if (printerStatus.isReadyToPrint) {
            isOK = true;
        } else if (printerStatus.isPaused) {
            //throw new ConnectionException("Cannot print because the printer is paused");
        } else if (printerStatus.isHeadOpen) {
            //throw new ConnectionException("Cannot print because the printer media door is open");
        } else if (printerStatus.isPaperOut) {
            //throw new ConnectionException("Cannot print because the paper is out");
        } else {
            //throw new ConnectionException("Cannot print");
        }

        //connection.open();
        return isOK;
    }
}

在函數getListPairedDevices中,您可以獲取要使用的mac地址列表,然后使用函數print發送您的ZLP代碼。

這是一些ZPL測試。

CT~~CD,~CC^~CT~
^XA~TA000~JSN^LT0^MNW^MTD^PON^PMN^LH0,0^JMA^PR4,4~SD0^JUS^LRN^CI0^XZ
^XA
^MMT
^PW575
^LL0799
^LS0
^FT35,720^A0N,20,19^FH\^FDHORA^FS
^FT35,680^A0N,20,19^FH\^FDFECHA^FS
^FT34,644^A0N,20,19^FH\^FDCOURRIER^FS
^FT34,609^A0N,20,19^FH\^FDADR2^FS
^FT34,572^A0N,20,19^FH\^FDADR1^FS
^FT34,533^A0N,20,19^FH\^FDSUCURSAL^FS
^FT34,498^A0N,20,19^FH\^FDDESTINATARIO^FS
^FT34,461^A0N,20,19^FH\^FDREMITENTE^FS
^FT165,720^A0N,20,19^FH\^FD: VHORA^FS
^FT165,680^A0N,20,19^FH\^FD: VFECHA^FS
^FT165,644^A0N,20,19^FH\^FD: VCOURRIER^FS
^FT166,534^A0N,20,19^FH\^FD: VSUCURSAL^FS
^FT166,426^A0N,20,19^FH\^FD: VEMPAQUE^FS
^FT166,461^A0N,20,19^FH\^FD: VREMITENTE^FS
^FT166,497^A0N,20,19^FH\^FD: VDESTINATARIO^FS
^FT34,425^A0N,20,19^FH\^FDEMPAQUE^FS
^FT136,365^A0N,23,24^FH\^FD1138 CHO-CHO-1-1-1-1^FS
^FT185,325^A0N,23,24^FH\^FDGUIA: 11389942705^FS
^FT165,46^A0N,28,28^FH\^FDDIRECTO LOGISTICS^FS
^FT25,214^A0N,138,139^FH\^FD1/2^FS
^FT380,265^BQN,2,8
^FH\^FDLA,101010^FS
^FO20,281^GB536,0,3^FS
^PQ1,0,1,Y^XZ

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM