java 佳博打印机,佳博蓝牙打印的java 连接
適用于gp-3120tu機(jī)型通過藍(lán)牙協(xié)議與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="佳博藍(lán)牙打印機(jī)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 { //當(dāng)藍(lán)牙斷開時,重新連接后再打印
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();
}
});
}
/**
* 發(fā)送標(biāo)簽
*/
void sendLabel1() {
LabelCommand tsc = new LabelCommand();
// 設(shè)置標(biāo)簽尺寸,按照實(shí)際尺寸設(shè)置
tsc.addSize(65, 35);
// 設(shè)置標(biāo)簽間隙,按照實(shí)際尺寸設(shè)置,如果為無間隙紙則設(shè)置為0
tsc.addGap(2);
// 設(shè)置打印方向
tsc.addDirection(LabelCommand.DIRECTION.BACKWARD, LabelCommand.MIRROR.NORMAL);
// 開啟帶Response的打印,用于連續(xù)打印
tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);
// 設(shè)置原點(diǎn)坐標(biāo)
tsc.addReference(0, 10);
// 撕紙模式開啟
tsc.addTear(EscCommand.ENABLE.ON);
// 清除打印緩沖區(qū)
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");
// 打印標(biāo)簽
tsc.addPrint(1, 1);
// 打印標(biāo)簽后 蜂鳴器響
tsc.addSound(2, 100);
tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);
Vectordatas = tsc.getCommand();
// 發(fā)送數(shù)據(jù)
mUtil.printContent(0, datas);
}
/**
* 發(fā)送標(biāo)簽
*/
void sendLabel() {
LabelCommand tsc = new LabelCommand();
// 設(shè)置標(biāo)簽尺寸,按照實(shí)際尺寸設(shè)置
tsc.addSize(65, 35);
// 設(shè)置標(biāo)簽間隙,按照實(shí)際尺寸設(shè)置,如果為無間隙紙則設(shè)置為0
tsc.addGap(2);
// 設(shè)置打印方向
tsc.addDirection(LabelCommand.DIRECTION.BACKWARD, LabelCommand.MIRROR.NORMAL);
// 開啟帶Response的打印,用于連續(xù)打印
tsc.addQueryPrinterStatus(LabelCommand.RESPONSE_MODE.ON);
// 設(shè)置原點(diǎn)坐標(biāo)
tsc.addReference(0, 10);
// 撕紙模式開啟
tsc.addTear(EscCommand.ENABLE.ON);
// 清除打印緩沖區(qū)
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 佳博打印機(jī)!");
// 繪制圖片
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");
// 打印標(biāo)簽
tsc.addPrint(1, 1);
// 打印標(biāo)簽后 蜂鳴器響
tsc.addSound(2, 100);
tsc.addCashdrwer(LabelCommand.FOOT.F5, 255, 255);
Vectordatas = tsc.getCommand();
// 發(fā)送數(shù)據(jù)
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() {
}
}
總結(jié)
以上是生活随笔為你收集整理的java 佳博打印机,佳博蓝牙打印的java 连接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python天天向上的力量三天打鱼两天晒
- 下一篇: js数组截取前5个_想用好 Node.j