GitHub项目地址

条件:1安卓主板上有USB口,2,rom内置了打印驱动

不行就用其他方式或者换主板吧(本人当初使用的工控主板坑得不要不要的)

本文介绍的是使用USB方式

佳博提供两个函数打印

1:只支持单字符 不然中文乱码

printString(String string, FONT font, Boolean bold, Boolean underlined, Boolean doubleHeight, Boolean doubleWidth)

2:  打印需要满一行才执行 如果不满一行需要在data最后添加 (byte)0x0A

sendData(Vector<Byte> data)
package com.gainscha.sample;import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;public class TestPrintActivity extends Activity implements Handler.Callback, PrintUtils.PrintListener {private static final int MSG_ERROR = 0;private static final int MSG_OPENED = 1;private static final int MSG_STATUS = 2;private static final int MSG_PRINT = 3;private static final int MSG_FINISHED = 4;private Button mBtn;private TextView mTv;private Handler mHandler;private PrintUtils mPrintUtils;private boolean isDeviceOpened = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_print);mHandler = new Handler(this);mPrintUtils = new PrintUtils(this);mPrintUtils.initPrint(this);initView();}private void initView() {mBtn = (Button) findViewById(R.id.btn_print);mTv = (TextView) findViewById(R.id.tv_status);mBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {print();}});findViewById(R.id.btn_esc).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}private void print() {EditText et = (EditText) findViewById(R.id.et_text);final String str = et.getText().toString();if (TextUtils.isEmpty(str)) {Toast.makeText(this, "请输入文字", Toast.LENGTH_SHORT).show();return;}if (isDeviceOpened) {mPrintUtils.print(str);//mHandler.obtainMessage(MSG_PRINT, str).sendToTarget();}}@Overrideprotected void onDestroy() {super.onDestroy();mPrintUtils.closeDevice();}@Overridepublic boolean handleMessage(Message msg) {switch (msg.what) {case MSG_ERROR:Toast.makeText(this, msg.obj.toString(), Toast.LENGTH_SHORT).show();break;case MSG_OPENED:isDeviceOpened = (boolean) msg.obj;mTv.setText(isDeviceOpened ? "已开启" : "未开启");break;case MSG_PRINT:mPrintUtils.print(msg.obj.toString());break;case MSG_STATUS:PrintUtils.Status s = (PrintUtils.Status) msg.obj;mTv.setText(s.name());break;case MSG_FINISHED:mTv.setText("打印完成");break;}return false;}@Overridepublic void onError(String errorMessage) {mHandler.obtainMessage(MSG_ERROR, errorMessage).sendToTarget();}@Overridepublic void onDeviceOpened(boolean isOpened) {mHandler.obtainMessage(MSG_OPENED, isOpened).sendToTarget();}@Overridepublic void onStatus(PrintUtils.Status status) {mHandler.obtainMessage(MSG_STATUS, status).sendToTarget();}@Overridepublic void onPrintFinished() {mHandler.obtainMessage(MSG_FINISHED).sendToTarget();}
}

资源文件

activity_test_print.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.gainscha.sample.TestPrintActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="状态:" /><TextViewandroid:id="@+id/tv_status"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="准备就绪"android:textColor="@android:color/holo_red_light" /></LinearLayout><EditTextandroid:id="@+id/et_text"android:layout_width="match_parent"android:layout_height="100dp"android:gravity="left"android:layout_marginTop="@dimen/activity_horizontal_margin"android:hint="请输入文字" /><Buttonandroid:id="@+id/btn_print"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/activity_horizontal_margin"android:text="打印" /><Buttonandroid:id="@+id/btn_esc"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/activity_horizontal_margin"android:text="退出" />
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.gainscha.sample"><uses-permission android:name="android.permission.INTERNET" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"><activityandroid:name=".SampleCodeActivity"android:configChanges="keyboardHidden|orientation"android:label="@string/app_name"android:screenOrientation="landscape"></activity><activity android:name="com.posin.filebrowser.FileBrowser" /><activityandroid:name=".TestPrintActivity"android:configChanges="keyboardHidden|orientation"android:screenOrientation="landscape"android:label="@string/title_activity_test_print"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><intent-filter><action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /></intent-filter><meta-dataandroid:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"android:resource="@xml/device_filter" /></activity></application></manifest>

PrintUtils

package com.gainscha.sample;import android.content.Context;import com.gainscha.GpCom.CallbackInterface;
import com.gainscha.GpCom.GpCom;
import com.gainscha.GpCom.GpComASBStatus;
import com.gainscha.GpCom.GpComCallbackInfo;
import com.gainscha.GpCom.GpComDevice;
import com.gainscha.GpCom.GpComDeviceParameters;
import com.gainscha.GpCom.USBPort;import java.util.Vector;/*** 仅适用于 USB口连接打印机* android 主板必须底层内置驱动* Created by henry  16/1/7.*/
public class PrintUtils implements CallbackInterface {private Context mContext;private GpComDevice mDevice;//Status Threadprivate Thread mThread;private boolean isThreadStop;private Boolean isOpened = false;//-------style-------private GpCom.FONT mTextFont;private boolean mTextIsBold = false;private boolean mTextIsUnderline = false;private GpCom.ALIGNMENT mTextAlignment;private boolean mTextIsWidth_x2 = false;private boolean mTextIsHeight_x2 = false;private EncodeType mTextEncodeType;//-------style-------private PrintListener mListener;public enum Status {Online, CoverOpen, PaperOut, PaperNearEnd, SlipSelectedAsActiveSheet}public enum EncodeType {GBK, GB2312}/*** construct** @param context context*/public PrintUtils(Context context) {mContext = context;}/*** initPrint** @param listener callback*/public void initPrint(PrintListener listener) {this.mListener = listener;init();}/*** initDevice*/private void init() {if (!isOpened) {USBPort.requestPermission(mContext);//initialize GpCommDevice = new GpComDevice();//设置回调mDevice.registerCallback(this);//监听开启状态listenDeviceStatus();//开启设备openDevice();}}/*** OpenDevices* step1: init parameters* step2: set parameters* step3: open device* step3: activate ASB sending*/public void openDevice() {GpCom.ERROR_CODE code = GpCom.ERROR_CODE.SUCCESS;//fill in some parametersGpComDeviceParameters parameters = new GpComDeviceParameters();parameters.PortType = GpCom.PORT_TYPE.USB;parameters.PortName = "";parameters.ApplicationContext = mContext;if (code == GpCom.ERROR_CODE.SUCCESS) {//set the parameters to the devicecode = mDevice.setDeviceParameters(parameters);if (code != GpCom.ERROR_CODE.SUCCESS) {String errorString = GpCom.getErrorText(code);showErrorMsg("setDeviceParameters Error\n" + errorString);}if (code == GpCom.ERROR_CODE.SUCCESS) {//open the devicecode = mDevice.openDevice();if (code != GpCom.ERROR_CODE.SUCCESS) {String errorString = GpCom.getErrorText(code);showErrorMsg("openDevice Error\n" + errorString);}}if (code == GpCom.ERROR_CODE.SUCCESS) {//activate ASB sendingcode = mDevice.activateASB(true, true, true, true, true, true);if (code != GpCom.ERROR_CODE.SUCCESS) {String errorString = GpCom.getErrorText(code);showErrorMsg("openDevice Error from activateASB:\n" + errorString);}}}}@Overridepublic GpCom.ERROR_CODE CallbackMethod(GpComCallbackInfo gpComCallbackInfo) {GpCom.ERROR_CODE code = GpCom.ERROR_CODE.SUCCESS;try {switch (gpComCallbackInfo.ReceivedDataType) {case GENERAL://do nothing, ignore any general incoming databreak;case ASB:    //new ASB data came inshowASBStatus(mDevice.getASB());break;}} catch (Exception e) {showErrorMsg("callback method threw exception: Callback Error");}return code;}/*** showASBStatus*/private void showASBStatus(GpComASBStatus m_ASBData) {try {if (m_ASBData != null) {//light up indicatorsif (m_ASBData.Online) {showStatus(Status.Online);}if (m_ASBData.CoverOpen) {showStatus(Status.CoverOpen);}if (m_ASBData.PaperOut) {showStatus(Status.PaperOut);} else {if (m_ASBData.PaperNearEnd) {showStatus(Status.PaperNearEnd);}if (m_ASBData.SlipSelectedAsActiveSheet) {showStatus(Status.SlipSelectedAsActiveSheet);}}}} catch (Exception e) {showErrorMsg("receiveAndShowASBData threw exception:ReceiveAndShowASBData Error");}}/*** 监听打印机开启与否*/private void listenDeviceStatus() {isThreadStop = false;mThread = new Thread(new Runnable() {public void run() {while (!isThreadStop) try {isOpened = mDevice.isDeviceOpen();if (isListenerNotNull()) {mListener.onDeviceOpened(isOpened);}Thread.sleep(200);} catch (InterruptedException e) {isThreadStop = true;showErrorMsg("Exception in status thread: createAndRunStatusThread Error");}}});mThread.start();}/*** 打印*/public void print(String text) {try {if (mDevice.isDeviceOpen()) {//设置默认样式GpCom.ERROR_CODE code = setDefaultTextStyle(GpCom.FONT.FONT_A, GpCom.ALIGNMENT.LEFT, EncodeType.GB2312, false, false, false, false);if (!isCodeSuccess(code)) {String errorString = GpCom.getErrorText(code);showErrorMsg("Error setTextStyle: " + errorString);}//print stringif (isCodeSuccess(code)) {byte[] bs = (text).getBytes(mTextEncodeType.name());Vector<Byte> data = new Vector<>(bs.length);for (int i = 0; i < bs.length; i++) {data.add(bs[i]);}//打印输出必须满行//换行符byte lineFeed = (byte) (0x0A);data.add(lineFeed);code = mDevice.sendData(data);if (isCodeSuccess(code)) {//cut pagercutPaper();if (isListenerNotNull()) {mListener.onPrintFinished();}} else {String errorString = GpCom.getErrorText(code);showErrorMsg("printString Error :" + errorString);}}} else {showErrorMsg("Error printString Device is not open  ");}} catch (Exception e) {showErrorMsg("Error printString  Exception: " + e.toString() + " - " + e.getMessage());}}/*** 设置打印字体风格** @param font 字体 A  B*/public void setTextFont(GpCom.FONT font) {mTextFont = font;}/*** 设置字体是否加粗** @param isBold true false*/public void setTextBold(boolean isBold) {this.mTextIsBold = isBold;}/*** 设置字体下划线** @param isUnderline true false*/public void setTextUnderline(boolean isUnderline) {this.mTextIsUnderline = isUnderline;}/*** 设置2x大小字体** @param width_x2  2倍宽* @param height_x2 2倍高*/public void setText_x2(boolean width_x2, boolean height_x2) {this.mTextIsWidth_x2 = width_x2;this.mTextIsHeight_x2 = height_x2;}/*** 设置排列方式** @param alignment 排列*/public void setTextAlignment(GpCom.ALIGNMENT alignment) {mTextAlignment = alignment;}/*** 设置字体编码** @param type exp: GBK*/public void setTextEncodeType(EncodeType type) {this.mTextEncodeType = type;}/*** 设置默认风格** @param font        字体* @param alignment   对齐* @param type        编码* @param isBold      加粗* @param isUnderline 下划线* @param isWidth_x2  2倍宽* @param isHeight_x2 2倍高* @return code*/private GpCom.ERROR_CODE setDefaultTextStyle(GpCom.FONT font, GpCom.ALIGNMENT alignment, EncodeType type, boolean isBold, boolean isUnderline, boolean isWidth_x2, boolean isHeight_x2) {setTextFont(font);setTextBold(isBold);setTextUnderline(isUnderline);setTextAlignment(alignment);setTextEncodeType(type);setText_x2(isWidth_x2, isHeight_x2);GpCom.ERROR_CODE code;//set print alignmentcode = mDevice.selectAlignment(mTextAlignment);String command;//set fontswitch (mTextFont.ordinal()) {default:case 1:command = "ESC M 0";break;case 2:command = "ESC M 1";break;}if (isCodeSuccess(code)) {code = mDevice.sendCommand(command);}//set boldcommand = this.mTextIsBold ? "ESC E 1" : "ESC E 0";if (isCodeSuccess(code)) {code = mDevice.sendCommand(command);}command = this.mTextIsUnderline ? "ESC - 49" : "ESC - 48";if (isCodeSuccess(code)) {code = mDevice.sendCommand(command);}//set width heightif (isCodeSuccess(code)) {int options1 = 0;if (this.mTextIsHeight_x2) {options1 |= 1;}if (this.mTextIsWidth_x2) {options1 |= 16;}command = String.format("GS ! %d", options1);code = mDevice.sendCommand(command);}return code;}/*** 切纸命令*/private void cutPaper() {try {if (mDevice.isDeviceOpen()) {GpCom.ERROR_CODE code = mDevice.cutPaper();if (code != GpCom.ERROR_CODE.SUCCESS) {String errorString = GpCom.getErrorText(code);showErrorMsg("cutPaper Error\n" + errorString);}} else {showErrorMsg(" cutPaper Error :Device is not open ");}} catch (Exception e) {showErrorMsg(" cutPaper Error : Exception:" + e.getMessage());}}/*** closeDevice*/public void closeDevice() {GpCom.ERROR_CODE err = mDevice.closeDevice();if (err != GpCom.ERROR_CODE.SUCCESS) {String errorString = GpCom.getErrorText(err);showErrorMsg("closeDevice Error" + errorString);}}/*** interface callback*/public interface PrintListener {void onError(String errorMessage);void onDeviceOpened(boolean isOpened);void onStatus(Status status);void onPrintFinished();}private void showErrorMsg(String msg) {if (isListenerNotNull()) {mListener.onError(msg);}}private void showStatus(Status status) {if (isListenerNotNull()) {mListener.onStatus(status);}}private boolean isListenerNotNull() {return null != mListener;}private boolean isCodeSuccess(GpCom.ERROR_CODE code) {return code == GpCom.ERROR_CODE.SUCCESS;}}

android通过USB连接佳博80打印机相关推荐

  1. 【Android】Android 集成佳博80打印机打印票据

    文章目录 [Android]Android 集成佳博80打印机打印票据 1.集成佳博80打印机依赖 2.规范调用接口 3.使用到的相关对象以及工具类 4.MainActivity初始化接口 5.Uni ...

  2. 佳博80系列打印机驱动开发DLL支持C#的过程

    最近小编的公司需要使用到佳博的打印机进行一系列的打印开发以及智能驱动,所以小编联系了佳博的官方客服,然后找到的他们的官方SDK开发包,进行开发,由于小编使用的是.net平台的C#语言,而官方的开发包里 ...

  3. Uniapp Android 佳博 小票打印机 插件

    Uniapp Android 佳博 小票打印机 插件 Uniapp Android 佳博小票打印机插件:  支持图片.条型码.二维码 打印. 1. 实例化插件 const gp= uni.requir ...

  4. 硬件系列(二)-------------wifi打印机之佳博wifi打印机踩坑之路

    一.前言 之前做过USB打印机,但是现在需求变了,不是使用收银台进行打印机的连接了,而是使用手机与打印机进行打印.手机又无法像收银机一样直接使用USB直接与打印机直接连接进行打印.所以只能使用蓝牙打印 ...

  5. Android 收银机Wifi 连接厨房厨单打印机

    Android 收银机Wifi 连接厨房厨单打印机 说明 第一次集成热敏打印机,对此相关知识为零,以快速接入为目的. 这里主要记录说明在集成过程中遇到的问题以及排查解决的办法.完整可用Android ...

  6. 59、佳博wifi打印机怎么配置

    1.去这里下载配置软件(注意,需要再windows下进行)http://pan.baidu.com/s/1bn1y4FX,并解压安装程序 2.连上wifi打印机的热点,比如说佳博打印机的默认为Gpri ...

  7. 佳博 TSC打印机 TSPL指令开发

    如何在电脑上使用C# 调用佳博 TSC打印机指令控制打印 踩了不少坑 跟大家分享下开发需要注意的地方 1. 佳博和TSC的 BARCODE 指令有出入,TSC多了一个参数,直接用佳博的打印是扫码不出二 ...

  8. 用代码实现断开Android手机USB连接【转】

    本文转载自:https://blog.csdn.net/phoebe_2012/article/details/47025309 用代码实现断开Android手机USB连接 用代码 实现了一个小功能: ...

  9. uniapp连接佳博打印机实现蓝牙打印票据功能

    开始实现搜索蓝牙.获取蓝牙设备.连接蓝牙设备等操作.代码如下 <template><view class="content"><button clas ...

最新文章

  1. 网站服务器怎么组件,网站服务器搭建与配置详解!
  2. C++ algorithm库中的几个常用函数(swap,reverse,sort)
  3. linux中正则表达式、find、xargs、grep以及sed等命令的用法
  4. python sftp模块_python下载paramiko模块准备使用SFTP的坑!!!
  5. SAP Spartacus里几个和Focus相关的directive的继承关系以及元素focus是如何实现的
  6. MongoDB复制集技术
  7. English trip -- VC(情景课)5 Around Town
  8. Servlet第四篇【request对象常用方法、应用】
  9. 最近目标检测新范式汇总SparseRCNN,OneNet,DeFCN等
  10. python支持complex吗_Python中complex函数有什么用?
  11. Unity2020.2中支持的C#8有什么新特性?
  12. windows vs2012 cuda6.5 caffe 简单安装方法
  13. 离散数学期末复习—学习笔记
  14. 计算机慢怎么解决6,电脑运行速度慢怎么回事 电脑运行速度慢的解决方法
  15. 联通手机卡网速的修改
  16. 【毕业设计】深度学习YOLO安检管制物品识别与检测 - python opencv
  17. 2021“智荟杯”浦发百度高校极客挑战赛——比赛总结
  18. UPCOJ-5344 - 被子 - 瞎搞
  19. volatile原理:happen before
  20. 给软件添加鼠标右键快捷方式

热门文章

  1. 数据透视:减半真的会导致币价上涨吗?
  2. python修饰器classmate_初学 python 两周小结
  3. kotlin Unresolved reference报错解决记录
  4. Linux下手动查杀木马过程
  5. 我发现凡是给offer的公司,面试时基本不问技术细节,那些问得又多又细的公司,后面就没下文了
  6. 使用ADK(windows评估和部署工具包)制作PE
  7. 用“AI核弹”饱和攻击的英伟达,如何赢下AI计算新赛场?
  8. 看魔乐科技消息传送笔记
  9. 移动端前端框架UI库(Frozen UI、WeUI、SUI Mobile)
  10. swoole 框架swoft使用