日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 佳博打印机,佳博蓝牙打印的java 连接

發布時間:2024/7/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 连接的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。