本文基于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,否则会关闭本次连接。注:android下USB具体的用法可以查看Usb通信之USB Host。
在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;
}
}
}
public void openUsb() {
if (mDevice != null) {
setDevice(mDevice);
if (mConnection == null) {
HashMap deviceList = mUsbManager.getDeviceList();
Iterator deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
mUsbManager.requestPermission(device, mPermissionIntent);
}
}
} else {
HashMap deviceList = mUsbManager.getDeviceList();
Iterator deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
mUsbManager.requestPermission(device, mPermissionIntent);
}
}
}
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
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通信机制的文章有提到要放到异步线程,这里需要注意。