佳博蓝牙打印的java 连接

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/rocklee/article/details/84890744

        适用于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);
        Vector<Byte> datas = 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);
        Vector<Byte> datas = 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() {

    }
}

猜你喜欢

转载自blog.csdn.net/rocklee/article/details/84890744