大多数相关答案和谷歌都包含很多旧的贡献,指的是这个主题。所以我正在寻找一种通过蓝牙热敏收据打印机制作我的Android-Application打印收据(宽度为58毫米)的方法。是否有必要使用带有第三方API的打印机?我想购买一台普通的蓝牙打印机,它可以连接到我的设备并使用默认的Android打印管理器为收据制作布局。那可能吗?有没有样本,文档或教程?会很感激。提前致谢
答案 0 :(得分:0)
是的,使用第三方SDK是必要的。我建议您使用Zebra打印机,它具有良好的文档和代码示例,与其他品牌相比,它更容易。从here
下载SDK要制作标签,您可以使用ZebraDesigner,它可以让您直观地创建布局,并为您提供输出ZPL代码,您可以将其从应用程序发送到蓝牙打印机。点击here即可查看。
答案 1 :(得分:0)
这里有一个如何使用来自cordova应用程序的zebra打印的例子
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