java 佳博打印机,佳博蓝牙打印的java 连接
适用于gp-3120tu机型通过蓝牙协议与android app的对接, 用的是TSC指令.
package com.freestyle.demo;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.freestyle.R;
import com.freestyle.common.btprinter.SharedPreferencesUtil;
import com.gprinter.command.EscCommand;
import com.gprinter.command.LabelCommand;
import com.freestyle.common.btprinter.BTPrinterUtil;
import com.freestyle.common.btprinter.DeviceConnFactoryManager;
import com.freestyle.common.btprinter.PrinterCommand;
import com.freestyle.common.btprinter.ThreadPool;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import java.util.Vector;
@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements BTPrinterUtil.BTPrinterInterface {
BTPrinterUtil mUtil;
@ViewById
protected View disconnectBtn;
@ViewById
protected TextView info;
private SharedPreferencesUtil mSetting;
//private ThreadPool threadPool;
private String mLastDeviceMac = null;
HandlerThread mBgThread=null;
Handler mPrintHandler=null;
final static String mTitle="佳博蓝牙打印机SDK演示";
@Click
protected void connectBtn() {
mUtil.selectDevice();
}
@Click
protected void disconnectBtn() {
mUtil.disconnect();
}
private Runnable mPrintTestRunnable = new Runnable() {
@Override
public void run() {
if (mUtil.deviceIsSupportTSC(0)) {
sendLabel1();
} else {
Toast.makeText(MainActivity.this, "printer command failed.", (int) 2000).show();
}
}
};
@Click
protected void testBtn() {
/*if (!mUtil.checkConnect(0)) {
Toast.makeText(this, "Connect a printer first.", (int) 2000).show();
return;
}*/
mPrintHandler.post(new Runnable() {
@Override
public void run() {
if (DeviceConnFactoryManager.getDeviceConnFactoryManagers()[0] == null ||
!DeviceConnFactoryManager.getDeviceConnFactoryManagers()[0].getConnState()) {
if (mLastDeviceMac == null) {
Toast.makeText(MainActivity.this, "Connect a printer first.", (int) 2000).show();
return;
} else { //当蓝牙断开时,重新连接后再打印
mUtil.connectDevice(mLastDeviceMac);
new Thread(new Runnable() {
@Override
public void run() {
for (int i=1;i<=15000;i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!mUtil.waitFlag.get()) break;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!mUtil.checkConnect(0)) {
Toast.makeText(MainActivity.this, "Connect printer failed.", (int) 2000).show();
} else {
mPrintTestRunnable.run();
}
}
});
}
}).start();
}
}
mPrintTestRunnable.run();
}
});
}
/**
* 发送标签
*/
void sendLabel1() {
LabelCommand tsc = new LabelCommand();
// 设置标签尺寸,按照实际尺寸设置
tsc.addSize(65, 35);
// 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0
tsc.addGap(2);
// 设置打印方向
tsc.addDirection(LabelCommand.DIRECTION.BACKWARD, LabelCommand.MIRROR.NORMAL);
// 开启带Response的打印,用于连续打印
tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);
// 设置原点坐标
tsc.addReference(0, 10);
// 撕纸模式开启
tsc.addTear(EscCommand.ENABLE.ON);
// 清除打印缓冲区
tsc.addCls();
tsc.addText(20, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE,
LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"PICK0NUM0AA");
tsc.addText(20, 70, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE,
LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"JB0123456");
tsc.addText(20, 120, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE,
LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"(15)");
/*for (int i=1;i<=4;i++) {
tsc.addText(20, 20+(i-1)*40, LabelCommand.FONTTYPE.valueOf("FONT_"+String.valueOf(i)),
LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"PICK0NUM0");
}*/
tsc.addQRCode(300, 20, LabelCommand.EEC.LEVEL_L, 7,
LabelCommand.ROTATION.ROTATION_0, "PICK0NUM0,12351253,123542");
// 绘制一维条码
//tsc.add1DBarcode(20, 250, LabelCommand.BARCODETYPE.CODE128, 100, LabelCommand.READABEL.EANBEL, LabelCommand.ROTATION.ROTATION_0, "SMARNET");
// 打印标签
tsc.addPrint(1, 1);
// 打印标签后 蜂鸣器响
tsc.addSound(2, 100);
tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);
Vectordatas = tsc.getCommand();
// 发送数据
mUtil.printContent(0, datas);
}
/**
* 发送标签
*/
void sendLabel() {
LabelCommand tsc = new LabelCommand();
// 设置标签尺寸,按照实际尺寸设置
tsc.addSize(65, 35);
// 设置标签间隙,按照实际尺寸设置,如果为无间隙纸则设置为0
tsc.addGap(2);
// 设置打印方向
tsc.addDirection(LabelCommand.DIRECTION.BACKWARD, LabelCommand.MIRROR.NORMAL);
// 开启带Response的打印,用于连续打印
tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);
// 设置原点坐标
tsc.addReference(0, 10);
// 撕纸模式开启
tsc.addTear(EscCommand.ENABLE.ON);
// 清除打印缓冲区
tsc.addCls();
// 绘制简体中文
tsc.addText(20, 20, LabelCommand.FONTTYPE.SIMPLIFIED_CHINESE,
LabelCommand.ROTATION.ROTATION_0, LabelCommand.FONTMUL.MUL_1, LabelCommand.FONTMUL.MUL_1,
"Welcome to use 佳博打印机!");
// 绘制图片
Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.gprinter);
tsc.addBitmap(20, 50, LabelCommand.BITMAP_MODE.OVERWRITE, b.getWidth(), b);
tsc.addQRCode(250, 50, LabelCommand.EEC.LEVEL_L, 5,
LabelCommand.ROTATION.ROTATION_0, " www.smarnet.cc");
// 绘制一维条码
//tsc.add1DBarcode(20, 250, LabelCommand.BARCODETYPE.CODE128, 100, LabelCommand.READABEL.EANBEL, LabelCommand.ROTATION.ROTATION_0, "SMARNET");
// 打印标签
tsc.addPrint(1, 1);
// 打印标签后 蜂鸣器响
tsc.addSound(2, 100);
tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);
Vectordatas = tsc.getCommand();
// 发送数据
mUtil.printContent(0, datas);
}
@AfterViews
public void initWidgets() {
mUtil.checkPermission();
mUtil.requestPermission();
disconnectBtn.setEnabled(false);
setTitle(mLastDeviceMac!=null?mTitle+" - "+mLastDeviceMac:mTitle);
if (mLastDeviceMac!=null){
mUtil.connectDevice(mLastDeviceMac);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUtil = new BTPrinterUtil(this, this);
mSetting=SharedPreferencesUtil.getInstantiation(this);
mLastDeviceMac=mSetting.getString(null,"device");
DeviceConnFactoryManager.context = this;
mBgThread=new HandlerThread("PrintThread");
mBgThread.start();
mPrintHandler=new Handler(mBgThread.getLooper());
}
@Override
protected void onDestroy(){
mBgThread.quit();
super.onDestroy();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mUtil.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onACLDisconnected() {
mUtil.disconnect();
}
@Override
public void onDisconnected() {
Toast.makeText(this, "disconnected", (int) 1000);
disconnectBtn.setEnabled(false);
info.setText("Diconnected");
}
@Override
public void onConnecting() {
}
@Override
public void onConnected() {
disconnectBtn.setEnabled(true);
info.setText("connected:\n" + mUtil.getConnDeviceInfo());
mLastDeviceMac = mUtil.getLastPrinterMac();
mSetting.putString(mLastDeviceMac,"device");
setTitle(mTitle+" - "+mLastDeviceMac);
}
@Override
public void onConnectFailed() {
}
}
java 佳博打印机,佳博蓝牙打印的java 连接相关推荐
- 热敏打印机 java实现根据打印机名字实现分别打印小票
目标任务: 用两台打印机分别实现打印不容的小票 1. 实体类:商品信息 /*** 商品信息类* @author admin**/ public class Commodity {// 商品名称priv ...
- JAVA使用springboot整合佳博标签打印机(一)
呕心沥血啊,这个打印机真好玩(反话)! 佳博打印机官网:http://cn.gainscha.com/gjxz.html 1.标题打印机品牌:佳博打印机 佳博有个官网,二次开发文件可以去管网下载,需要 ...
- gprinter佳博打印机androidSDK
最近在用佳博的SDK做打印的功能,由于一直做的是.net,没有android的基础,这个功能我做了一个多月,包括前期调研佳博打印机的打印方式.佳博打印机有两种打印方式,一种是标签打印,要用到TscCo ...
- Android打印机--蓝牙打印
关于Android蓝牙打印和网络打印,其实都是利用Socket通信机制,只是蓝牙打印将socket做了一层封装BluetoothSocket ,打印的数据都是以流的方式进行传输或保存的,程序需要数据的 ...
- java 打印机类printer_GitHub - 505058216/thermal_printer: Java实现网络小票打印机自定义无驱打印...
Java实现POS打印机自定义无驱打印 热敏打印机使用越来越广泛,而安装驱动相当复杂,万幸的是,几乎所有的热敏打印机都支持ESC/P指令,参考网络上一些资料后,在此整理了一份自定义打印的方案 • 打印 ...
- java 小票打印_GitHub - SubLuLu/thermal_printer: Java实现网络小票打印机自定义无驱打印...
Java实现POS打印机自定义无驱打印 热敏打印机使用越来越广泛,而安装驱动相当复杂,万幸的是,几乎所有的热敏打印机都支持ESC/P指令,参考网络上一些资料后,在此整理了一份自定义打印的方案 • 打印 ...
- Android蓝牙相关—蓝牙打印
一.概述 最近公司刚好遇到个蓝牙打印的功能,以前实习时看到过类似功能,刚好这次自己实现,顺便记录一下. 二.基本环境 权限: <uses-permission android:name=&quo ...
- 蓝牙热敏打印开发(佳博打印机)
最近在研究蓝牙打印,这是个大坑啊,不同的打印机会有不同的设置,这边用的是佳博的热敏打印机,就以它为例吧. 一,首先,蓝牙打印当然得要连接蓝牙,这边在demo里有两个文件,ConnectViewCont ...
- java使用佳博打印机打印条形码
在实际开发中我们使用条形码打印机打印条形码,这里介绍下使用动态链接库TSClib.dll编辑方式打印条形码,还有js实现方式,这里不进行介绍,会给相关的demo 准备工作: 1.佳博打印机,如佳博GP ...
最新文章
- java将按钮添加到指定位置_在运行时将按钮添加到布局
- 白嫖一时爽!一直白嫖一直爽!程序员必备神器!
- python int str_python int str
- python检测文件夹中新增文件_python检测文件夹变化,并拷贝有更新的文件到对应目录...
- Python 查看pip安装的包的位置(查看pip安装包的路径)
- java堆缓冲区,Java NIO之Buffer(缓冲区)
- 历时3个月,我们是如何为一个开源项目集资300万美元的?
- Spring的bean管理(注解创建对象)
- python界面设计实例qt_Python GUI教程(六):使用Qt设计师进行窗口布局
- 1044: 不及格率 C语言
- C语言 --- sizeof() 7种使用详解
- mysql报错error during_MySQL数据库之MYSQL报Fatal error encountered during command execution.错误的解决方法...
- 微软服务器系统突然要求密钥,买了Win10新电脑?小心微软偷走你的设备加密密钥...
- 新一代 OIer 的快速入门命令行教程
- 平面直角坐标系中的旋转公式_中考难点,旋转+动点的最值问题的构建
- [代码审计]Weiphp5.0 前台文件任意读取分析
- android 监听短信并获取验证码
- Mac android原生工程中潜入react-native混合开发项目搭建
- 苯乙烯丁烯苯乙烯(SBS)的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- 用java做小学数学系统_小学生数学练习题目自动生成系统——java课程设计
热门文章
- spring手动回滚事务_Spring总结---gt;03
- 二叉排序树的后序遍历序列必然是递增的_剑指offer 33——二叉搜索树的后序遍历序列...
- atoi函数_每日一道 LeetCode (50):字符串转换整数 (atoi)
- linux的accept函数源码,accept函数
- 吴恩达发起新型竞赛范式!模型固定,只调数据?!
- 全栈深度学习第5期: 神经网络调试技巧
- “小众”之美——Ruby在QA自动化中的应用
- 分布式数据层中间件详解:如何实现分库分表+动态数据源+读写分离
- 阿里架构师进阶23期精讲:Redis、Kafka、Dubbo、Docker等
- Java远程通讯技术及原理分析