Commit 3506d520 authored by pengguangpu's avatar pengguangpu

添加打印sdk相关代码;编写标准打印接口以及状态码

parent 24f2fafb
...@@ -31,4 +31,10 @@ dependencies { ...@@ -31,4 +31,10 @@ dependencies {
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
/**
* 创捷机器打印jar包
*/
implementation files('libs/CJusbjar.jar')
implementation files('libs/PrintCmd.jar')
} }
package com.miya.print;
import android.content.Context;
/**
* 打印基类
*/
public class BasePrinter implements IPrinter {
/**
* 初始化调用者的引用
*/
Context context;
/**
* 当前状态码,具体值参考全局状态码
*/
int status;
@Override
public boolean init(Context context) {
return false;
}
@Override
public boolean release() {
return false;
}
@Override
public int beginPrint() {
return 0;
}
}
package com.miya.print;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import com.miya.print.utils.D;
import com.printsdk.cmd.PrintCmd;
import com.printsdk.usbsdk.UsbDriver;
/**
* 创捷打印驱动
*/
public class ChuangjiePrinter extends BasePrinter {
private final static String TAG = ChuangjiePrinter.class.getSimpleName();
private final static int PID11 = 8211;
private final static int PID13 = 8213;
private final static int PID15 = 8215;
private final static int VENDORID = 1305;
private UsbDriver mUsbDriver;
private UsbManager mUsbManager;
public UsbDevice mUsbDev1; // 内置打印机
public UsbDevice mUsbDev2; // usb标签打印机
public UsbDevice mUsbDev; // 公共打印机
private static final String ACTION_USB_PERMISSION = "com.usb.sample.USB_PERMISSION";
/**
* 0 打印机正常
* 1 打印机未连接或未上电
* 2 打印机和调用库不匹配
* 3 打印头打开
* 4 切刀未复位
* 5 打印头过热
* 6 黑标错误
* 7 纸尽
* 8 纸将尽
*
* @return
*/
public synchronized int connet() {
int conState = -1;
//有时候会出现mUsbDev为空,但是mUsbDriver还在连接的情况
if (null == mUsbDev) {
if (mUsbDriver.isConnected()) {
mUsbDriver.disconnet();
}
}
getUsbDriverService();
//获取连接状态
if (null != mUsbDev) {
conState = getPrinterStatus(mUsbDev);
}
return conState;
}
/**
* 0 打印机正常
* 1 打印机未连接或未上电
* 2 打印机和调用库不匹配
* 3 打印头打开
* 4 切刀未复位
* 5 打印头过热
* 6 黑标错误
* 7 纸尽
* 8 纸将尽
*
* @param usbDev
* @return
*/
private int getPrinterStatus(UsbDevice usbDev) {
int iRet = -1;
byte[] bRead1 = new byte[1];
byte[] bWrite1 = PrintCmd.getStatus1();
if (mUsbDriver.read(bRead1, bWrite1, usbDev) > 0) {
iRet = PrintCmd.checkStatus1(bRead1[0]);
}
if (iRet != 0)
return iRet;
byte[] bRead2 = new byte[1];
byte[] bWrite2 = PrintCmd.getStatus2();
if (mUsbDriver.read(bRead2, bWrite2, usbDev) > 0) {
iRet = PrintCmd.checkStatus2(bRead2[0]);
}
if (iRet != 0)
return iRet;
byte[] bRead3 = new byte[1];
byte[] bWrite3 = PrintCmd.getStatus3();
if (mUsbDriver.read(bRead3, bWrite3, usbDev) > 0) {
iRet = PrintCmd.checkStatus3(bRead3[0]);
}
if (iRet != 0)
return iRet;
byte[] bRead4 = new byte[1];
byte[] bWrite4 = PrintCmd.getStatus4();
if (mUsbDriver.read(bRead4, bWrite4, usbDev) > 0) {
iRet = PrintCmd.checkStatus4(bRead4[0]);
}
return iRet;
}
/**
* 获取UsbDriverService服务
*
* @return
*/
private boolean getUsbDriverService() {
boolean blnRtn = false;
try {
if (!mUsbDriver.isConnected()) {
// USB线已经连接
for (UsbDevice device : mUsbManager.getDeviceList().values()) {
if ((device.getProductId() == PID11 && device.getVendorId() == VENDORID)
|| (device.getProductId() == PID13 && device.getVendorId() == VENDORID)
|| (device.getProductId() == PID15 && device.getVendorId() == VENDORID)) {
blnRtn = mUsbDriver.usbAttached(device);
if (blnRtn == false) {
break;
}
blnRtn = mUsbDriver.connect(device);
// 打开设备
if (blnRtn) {
//内置打印机
if (device.getProductId() == PID11) {
mUsbDev1 = device;
mUsbDev = mUsbDev1;
} else {//USB打印机
mUsbDev2 = device;
mUsbDev = mUsbDev2;
D.i(TAG, "USB连接成功");
break;
}
} else {
D.e(TAG, "USB连接失败");
break;
}
}
}
} else {
blnRtn = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return blnRtn;
}
}
package com.miya.print; package com.miya.print;
import android.content.Context;
/** /**
* 标准打印接口 * 标准打印接口
* *
...@@ -8,4 +10,78 @@ package com.miya.print; ...@@ -8,4 +10,78 @@ package com.miya.print;
*/ */
public interface IPrinter { public interface IPrinter {
} //================全局状态码定义start===============//
/**
* 操作成功
*/
int CODE_SUCCESS = 0;
/**
* 操作失败
*/
int CODE_FAILED = -1;
/**
* 打印机未连接或未上电
*/
int CODE_NOT_CONNECTED = 1;
/**
* 打印机和调用库不匹配
*/
int CODE_NOT_MATCHED = 2;
/**
* 打印头打开
*/
int CODE_PRINT_HEAD_OPENED = 3;
/**
* 切刀未复位
*/
int CODE_KNIFE_ERROR = 4;
/**
* 打印头过热
*/
int CODE_PRINT_HEAD_HOT = 5;
/**
* 黑标错误
*/
int CODE_BLACK_ERROR = 6;
/**
* 纸尽
*/
int CODE_NO_PAPER = 7;
/**
* 纸将尽
*/
int CODE_FEW_PAPER = 8;
//================全局状态码定义end===============//
/**
* 初始化驱动
*
* @param context 调用者上下文索引
* @return true==>初始化成功;false==>初始化失败
*/
boolean init(Context context);
/**
* 销毁资源
*
* @return
*/
boolean release();
/**
* 开始新一轮打印
*
* @return 状态码
*/
int beginPrint();
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment