From 1c27469f0fbf776270fb6f9fa831f7d48e9813b8 Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 23 Aug 2017 23:29:59 +0800 Subject: [PATCH] add hr method and fix ble state --- .../java/xyz/mtfos/btdemo/PrinterBle.java | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java b/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java index 1a3e216..edeedc8 100644 --- a/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java +++ b/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java @@ -21,6 +21,7 @@ import android.os.ParcelUuid; import android.util.Log; import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.Arrays; @@ -57,6 +58,8 @@ public class PrinterBle { final public static int ALIGN_CENTER = 0x01; final public static int ALIGN_RIGHT = 0x02; + private int lastAlign = 0x00; + // 藍芽狀態 public int mState = 0x00; private boolean isConnected = false; @@ -247,7 +250,7 @@ public class PrinterBle { mAdapter.startLeScan(mLeScanCB); } - mState = CONNECTING; + mState = SCANNING; if(this.mStateListener != null) { this.mStateListener.onChange(mState); } @@ -263,7 +266,7 @@ public class PrinterBle { mAdapter.stopLeScan(mLeScanCB); } - if (mState == CONNECTING) { + if (mState == SCANNING) { mState = DISCONNECTED; if(this.mStateListener != null) { this.mStateListener.onChange(mState); @@ -289,6 +292,9 @@ public class PrinterBle { if (device == null) return; mBtGatt = device.connectGatt(ctx, false, mBtGattCB); mState = CONNECTING; + if(this.mStateListener != null){ + this.mStateListener.onChange(mState); + } } public void setOnStateChangeListener(stateChangeListener listener) { @@ -366,12 +372,12 @@ public class PrinterBle { dataQueue.clear(); } - public void textTab() { - addText("\t"); + public PrinterBle textTab() { + return addText("\t"); } - public void textNewLine() { - addText("\n"); + public PrinterBle textNewLine() { + return addText("\n"); } public PrinterBle addText(String txt) { @@ -380,9 +386,9 @@ public class PrinterBle { return this; } - public void addTextln(String txt) { - if (txt == null || txt.isEmpty()) return; - addText(txt + "\n"); + public PrinterBle addTextln(String txt) { + if (txt == null || txt.isEmpty()) return this; + return addText(txt + "\n"); } public PrinterBle setAlign(int align) { @@ -393,18 +399,28 @@ public class PrinterBle { break; case ALIGN_LEFT: strAlign = "lt"; + break; case ALIGN_RIGHT: strAlign = "rt"; + break; default: return this; } - addText("__&a" + strAlign + "__"); - return this; + this.lastAlign = align; + return addText("__&a" + strAlign + "__"); } - public void setSize(int w, int h) { - if (w < 1 || h < 1 || w > 3 || h > 3) return; - addText(String.format("__&s%d,%d__", w, h)); + public PrinterBle setSize(int w, int h) { + if (w < 1 || h < 1 || w > 3 || h > 3) return this; + return addText(String.format("__&s%d,%d__", w, h)); + } + + public PrinterBle addHR(){ + int align = this.lastAlign; + setAlign(ALIGN_CENTER); + addTextln(StringUtils.repeat('-', 20)); + setAlign(align); + return this; } public void resetSize() {