Commit 13d6bbf2 authored by pengguangpu's avatar pengguangpu

添加商米的打印驱动,测试发现无法正常打印条码和二维码,待调试

parent eb5a9693
......@@ -6,7 +6,7 @@ import android.graphics.Bitmap;
/**
* 打印基类
*/
public class BasePrinter implements IPrinter {
public abstract class BasePrinter implements IPrinter {
/**
* 80mm打印纸允许打印的最大图片的宽度,此为经验值
......
......@@ -43,13 +43,14 @@ public class ChuangjiePrinter extends BasePrinter {
@Override
public boolean init(Context context) {
D.i(TAG, "开始初始化创捷打印机");
this.context = context;
if (mUsbDriver == null) {
getUsbDriverService(context);
}
// USB线已经连接
getUsbDriverService();
return connet() == 0;
return connet() == PrinterStatusEnum.CODE_SUCCESS.status;
}
@Override
......
......@@ -17,7 +17,7 @@ public class PrinterManager {
enum Type {
//这里添加扩展的驱动
// TYPE_HISENSE("hisense", ChuangjiePrinter.class.getName()),
TYPE_SUNMI("shangmik1", ShangmiK1Printer.class.getName()),
TYPE_SUNMI("sunmiK1", SunmiK1Printer.class.getName()),
TYPE_YINGTAI("yingtai", YingtaiPrinter.class.getName()),
TYPE_CHUANGJIE("chuangjie", ChuangjiePrinter.class.getName()),
TYPE_SANGDA("sangda", SangdaPrinter.class.getName());
......@@ -73,10 +73,10 @@ public class PrinterManager {
*/
public boolean init(Context context) {
for (Type type : Type.values()) {
try {
if (printer != null) {
printer.release();
}
try {
Class printerCls = Class.forName(type.clsName);
printer = (IPrinter) printerCls.newInstance();
boolean result = printer.init(context);
......@@ -91,7 +91,11 @@ public class PrinterManager {
}
//没有找到对应的打印机
if (printer != null) {
try {
printer.release();
} catch (Exception ex) {
ex.printStackTrace();
}
}
D.i(TAG, "没找到对应的打印机器,请检查");
return false;
......@@ -104,16 +108,58 @@ public class PrinterManager {
*/
public boolean release() {
if (isConnected()) {
try {
boolean result = printer.release();
if (result == false) {
return false;
}
printer = null;
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
}
return true;
}
/**
* 保险起见提供指定硬件的初始化
* ps:谨慎调用
*
* @param context
* @param type 指定硬件类型
* @return
*/
public boolean init(Context context, Type type) {
if (type == null) {
return false;
}
//是否支持指定硬件
boolean isSupported = false;
for (Type tmpType : Type.values()) {
if (type.typeName.equals(tmpType.typeName)) {
isSupported = true;
}
}
if (isSupported == false) {
return false;
}
//反射新建实例
try {
Class printerCls = Class.forName(type.clsName);
printer = (IPrinter) printerCls.newInstance();
boolean result = printer.init(context);
if (result == true) {
printer.setPrinterName(type.typeName);
D.i(TAG, "初始化打印机成功,打印机器为:" + type.typeName);
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 获取当前打印接口
*
......
......@@ -30,6 +30,7 @@ public class SangdaPrinter extends BasePrinter {
@Override
public boolean init(Context context) {
D.i(TAG, "开始初始化桑达打印机");
super.init(context);
mPrinter = PrinterAPI.getInstance();
if (isConnect()) {
......@@ -45,8 +46,9 @@ public class SangdaPrinter extends BasePrinter {
return false;
}
} catch (Exception e) {
D.e(TAG, "桑达打印机连接失败");
e.printStackTrace();
}
D.e(TAG, "桑达打印机连接失败");
return false;
}
......
......@@ -26,6 +26,7 @@ public class YingtaiPrinter extends BasePrinter {
@Override
public boolean init(Context context) {
D.i(TAG,"开始初始化英泰打印机");
this.context = context;
mPrinter = PrinterAPI.getInstance();
io = new USBAPI(context);
......
......@@ -31,6 +31,11 @@ public class SunmiWeighing extends BaseWeighing {
*/
ScaleService scaleService;
/**
* 服务连接器
*/
ServiceConnection serviceConnection;
/**
* 称重回调监听
*/
......@@ -61,32 +66,7 @@ public class SunmiWeighing extends BaseWeighing {
@Override
public boolean init(Context context) {
this.context = context;
Intent intent = new Intent();
intent.setPackage(SERVICE_PACKAGE);
intent.setAction(SERVICE_ACTION);
return this.context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void setWeighingCallback(WeighingCallback weighingCallback) {
this.weighingCallback = weighingCallback;
}
@Override
public boolean resetZero() {
try {
scaleService.zero();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 服务连接器
*/
ServiceConnection serviceConnection = new ServiceConnection() {
serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
scaleService = ScaleService.Stub.asInterface(service);
......@@ -113,6 +93,27 @@ public class SunmiWeighing extends BaseWeighing {
scaleService = null;
}
};
Intent intent = new Intent();
intent.setPackage(SERVICE_PACKAGE);
intent.setAction(SERVICE_ACTION);
return this.context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void setWeighingCallback(WeighingCallback weighingCallback) {
this.weighingCallback = weighingCallback;
}
@Override
public boolean resetZero() {
try {
scaleService.zero();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean release() {
......
......@@ -4,6 +4,8 @@ import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import com.miya.weighing.utils.D;
public class WeighingManager {
final static String TAG = WeighingManager.class.getSimpleName();
......@@ -80,10 +82,10 @@ public class WeighingManager {
//轮询所有的称重服务,注意是同步方法
for (Type type : Type.values()) {
//确保之前的称重实例已被销毁
try {
if (weighing != null) {
weighing.release();
}
try {
//反射新建实例
Class weighingClass = Class.forName(type.clsName);
weighing = (Weighing) weighingClass.newInstance();
......@@ -155,6 +157,10 @@ public class WeighingManager {
Class weighingClass = Class.forName(type.clsName);
weighing = (Weighing) weighingClass.newInstance();
boolean result = weighing.init(context);
if (result == true) {
weighing.setWeighingName(type.clsName);
D.i(TAG, "称重硬件初始化成功==>" + weighing.getWeighingName());
}
return result;
} catch (Exception e) {
e.printStackTrace();
......
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