add hr method and fix ble state

This commit is contained in:
Jay 2017-08-23 23:29:59 +08:00
parent aac20b696d
commit 1c27469f0f

View File

@ -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() {