Android下的POS打印机调用的简单实现
本文基于gp58系列,它可以兼容esc/pos指令集,对epson的打印机通用.
android下的设备调试,如果设备提供了驱动,按照厂家的驱动调试即可;设备未提供驱动,只能按照通用的方法进行调试。这里采用的是调用usb接口来控制打印机输出。
1.首先获取usb管理器
public usbadmin(context context) { musbmanager = (usbmanager) context.getsystemservice(context.usb_service); mpermissionintent = pendingintent.getbroadcast(context, 0, new intent(action_usb_permission), 0); intentfilter filter = new intentfilter(action_usb_permission); context.registerreceiver(musbreceiver, filter); }
使用一个延迟意图来接收usb接入时的广播,当广播接收到时,说明有新的设备接入。
添加一个boardcast action
private static final string action_usb_permission = "com.android.example.usb_permission";
private final broadcastreceiver musbreceiver = new broadcastreceiver() { public void onreceive(context context, intent intent) { string action = intent.getaction(); if (action_usb_permission.equals(action)) { synchronized (this) { usbdevice device = (usbdevice) intent.getparcelableextra(usbmanager.extra_device); if (intent.getbooleanextra(usbmanager.extra_permission_granted, false)) { if (device != null) { setdevice(device); } else { closeusb(); // mdevice = device; } } else { log.d(tag, "permission denied for device " + device); } } } } };
取到usb设备的引用,android系统会询问你是否允许设备访问,默认为false;当允许了访问之后,会判断usb的引用是否为null,如果不为空则会调用setdevice来创建一个connection,否则会关闭本次连接。
在setdevice中,我们可以获取设备的功能集(usbinterface),也可以获取通信通道(usbendpoint),同时也创建了host与device的连接用来传输数据。
private void setdevice(usbdevice device) { if (device != null) { usbinterface intf = null; usbendpoint ep = null; int interfacecount = device.getinterfacecount(); int j; mdevice = device; for (j = 0; j < interfacecount; j++) { int i; intf = device.getinterface(j); log.i(tag, "接口是:" + j + "类是:" + intf.getinterfaceclass()); if (intf.getinterfaceclass() == 7) { int usbendpointcount = intf.getendpointcount(); for (i = 0; i < usbendpointcount; i++) { ep = intf.getendpoint(i); log.i(tag, "端点是:" + i + "方向是:" + ep.getdirection() + "类型是:" + ep.gettype()); if (ep.getdirection() == 0 && ep.gettype() == usbconstants.usb_endpoint_xfer_bulk) { log.i(tag, "接口是:" + j + "端点是:" + i); break; } } if (i != usbendpointcount) { break; } } } if (j == interfacecount) { log.i(tag, "没有打印机接口"); return; } mendpointintr = ep; usbdeviceconnection connection = musbmanager.opendevice(device); if (connection != null && connection.claiminterface(intf, true)) { log.i(tag, "打开成功! "); mconnection = connection; } else { log.i(tag, "打开失败! "); mconnection = null; } } }
2.在相关的类中新建一个usbadmin,调用openusb,这里首先是走了上面的setdevice()方法,获取到了设备的引用,当连接通道建立时列出所有usb设备,当设备的引用不存在时同样列出所有的usb设备,并且都请求获取usb权限。
public void openusb() { if (mdevice != null) { setdevice(mdevice); if (mconnection == null) { hashmap<string, usbdevice> devicelist = musbmanager.getdevicelist(); iterator<usbdevice> deviceiterator = devicelist.values().iterator(); while (deviceiterator.hasnext()) { usbdevice device = deviceiterator.next(); musbmanager.requestpermission(device, mpermissionintent); } } } else { hashmap<string, usbdevice> devicelist = musbmanager.getdevicelist(); iterator<usbdevice> deviceiterator = devicelist.values().iterator(); while (deviceiterator.hasnext()) { usbdevice device = deviceiterator.next(); musbmanager.requestpermission(device, mpermissionintent); } } }
3.当上面两部都走完了之后,我们就可以发送指令来控制已经建立连接的打印机了,这里我们使用的是标准的esc/pos指令集,为硬件默认,贴出代码,这里的指令集采用的是十进制表示形式,也可以替换成十六进制。
public class printercmdutils { /** * 这些数据源自爱普生指令集,为pos机硬件默认 */ public static final byte esc = 27;//换码 public static final byte fs = 28;//文本分隔符 public static final byte gs = 29;//组分隔符 public static final byte dle = 16;//数据连接换码 public static final byte eot = 4;//传输结束 public static final byte enq = 5;//询问字符 public static final byte sp = 32;//空格 public static final byte ht = 9;//横向列表 public static final byte lf = 10;//打印并换行(水平定位) public static final byte cr = 13;//归位键 public static final byte ff = 12;//走纸控制(打印并回到标准模式(在页模式下) ) public static final byte can = 24;//作废(页模式下取消打印数据 ) //------------------------打印机初始化----------------------------- /** * 打印机初始化 * @return */ public static byte[] init_printer() { byte[] result = new byte[2]; result[0] = esc; result[1] = 64; return result; } //------------------------换行----------------------------- /** * 换行 * @param linenum要换几行 * @return */ public static byte[] nextline(int linenum) { byte[] result = new byte[linenum]; for(int i=0;i<linenum;i++) { result[i] = lf; } return result; } //------------------------下划线----------------------------- /** * 绘制下划线(1点宽) * @return */ public static byte[] underlinewithonedotwidthon() { byte[] result = new byte[3]; result[0] = esc; result[1] = 45; result[2] = 1; return result; } /** * 绘制下划线(2点宽) * @return */ public static byte[] underlinewithtwodotwidthon() { byte[] result = new byte[3]; result[0] = esc; result[1] = 45; result[2] = 2; return result; } /** * 取消绘制下划线 * @return */ public static byte[] underlineoff() { byte[] result = new byte[3]; result[0] = esc; result[1] = 45; result[2] = 0; return result; } //------------------------加粗----------------------------- /** * 选择加粗模式 * @return */ public static byte[] boldon() { byte[] result = new byte[3]; result[0] = esc; result[1] = 69; result[2] = 0xf; return result; } /** * 取消加粗模式 * @return */ public static byte[] boldoff() { byte[] result = new byte[3]; result[0] = esc; result[1] = 69; result[2] = 0; return result; } //------------------------对齐----------------------------- /** * 左对齐 * @return */ public static byte[] alignleft() { byte[] result = new byte[3]; result[0] = esc; result[1] = 97; result[2] = 0; return result; } /** * 居中对齐 * @return */ public static byte[] aligncenter() { byte[] result = new byte[3]; result[0] = esc; result[1] = 97; result[2] = 1; return result; } /** * 右对齐 * @return */ public static byte[] alignright() { byte[] result = new byte[3]; result[0] = esc; result[1] = 97; result[2] = 2; return result; } /** * 水平方向向右移动col列 * @param col * @return */ public static byte[] set_ht_position( byte col ) { byte[] result = new byte[4]; result[0] = esc; result[1] = 68; result[2] = col; result[3] = 0; return result; } //------------------------字体变大----------------------------- /** * 字体变大为标准的n倍 * @param num * @return */ public static byte[] fontsizesetbig(int num) { byte realsize = 0; switch (num) { case 1: realsize = 0;break; case 2: realsize = 17;break; case 3: realsize = 34;break; case 4: realsize = 51;break; case 5: realsize = 68;break; case 6: realsize = 85;break; case 7: realsize = 102;break; case 8: realsize = 119;break; } byte[] result = new byte[3]; result[0] = 29; result[1] = 33; result[2] = realsize; return result; } //------------------------字体变小----------------------------- /** * 字体取消倍宽倍高 * @param num * @return */ public static byte[] fontsizesetsmall(int num) { byte[] result = new byte[3]; result[0] = esc; result[1] = 33; return result; } //------------------------切纸----------------------------- /** * 进纸并全部切割 * @return */ public static byte[] feedpapercutall() { byte[] result = new byte[4]; result[0] = gs; result[1] = 86; result[2] = 65; result[3] = 0; return result; } /** * 进纸并切割(左边留一点不切) * @return */ public static byte[] feedpapercutpartial() { byte[] result = new byte[4]; result[0] = gs; result[1] = 86; result[2] = 66; result[3] = 0; return result; } //------------------------切纸----------------------------- public static byte[] bytemerger(byte[] byte_1, byte[] byte_2){ byte[] byte_3 = new byte[byte_1.length+byte_2.length]; system.arraycopy(byte_1, 0, byte_3, 0, byte_1.length); system.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length); return byte_3; } public static byte[] bytemerger(byte[][] bytelist){ int length = 0; for(int i=0;i<bytelist.length;i++) { length += bytelist[i].length; } byte[] result = new byte[length]; int index = 0; for(int i=0;i<bytelist.length;i++) { byte[] nowbyte = bytelist[i]; for(int k=0;k<bytelist[i].length;k++) { result[index] = nowbyte[k]; index++; } } return result; } }
4.在以上都完成之后,就可以把你需要的字符串转换成byte数组并调用sendcommand方法来进行打印了
@suppresslint("newapi") public boolean sendcommand(byte[] content) { boolean result; synchronized (this) { int len = -1; if (mconnection != null) { len = mconnection.bulktransfer(mendpointintr, content, content.length, 10000); } if (len < 0) { result = false; log.i(tag, "发送失败! " + len); } else { result = true; log.i(tag, "发送" + len + "字节数据"); } } return result;
len = mconnection.bulktransfer(mendpointintr, content, content.length, 10000);
这一步仅仅加了同步锁,并未开启一个新的线程去处理,在本机上没有问题,但上面的usb通信机制的文章有提到要放到异步线程,这里需要注意。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: html5中嵌入视频自动播放的问题解决