java 蓝牙打印_Android - 将收据打印到蓝牙打印机

本文介绍了一种在Android应用中通过Java实现蓝牙打印的方法,特别是在使用Zebra打印机时。主要涉及获取已配对蓝牙设备的MAC地址,并发送ZPL代码进行打印。代码示例展示了如何打开蓝牙连接、检查打印机状态并发送打印指令。
摘要由CSDN通过智能技术生成

嗨这里是一个如何使用来自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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值