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
1 changed files with 30 additions and 14 deletions

View File

@ -21,6 +21,7 @@ import android.os.ParcelUuid;
import android.util.Log; import android.util.Log;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -57,6 +58,8 @@ public class PrinterBle {
final public static int ALIGN_CENTER = 0x01; final public static int ALIGN_CENTER = 0x01;
final public static int ALIGN_RIGHT = 0x02; final public static int ALIGN_RIGHT = 0x02;
private int lastAlign = 0x00;
// 藍芽狀態 // 藍芽狀態
public int mState = 0x00; public int mState = 0x00;
private boolean isConnected = false; private boolean isConnected = false;
@ -247,7 +250,7 @@ public class PrinterBle {
mAdapter.startLeScan(mLeScanCB); mAdapter.startLeScan(mLeScanCB);
} }
mState = CONNECTING; mState = SCANNING;
if(this.mStateListener != null) { if(this.mStateListener != null) {
this.mStateListener.onChange(mState); this.mStateListener.onChange(mState);
} }
@ -263,7 +266,7 @@ public class PrinterBle {
mAdapter.stopLeScan(mLeScanCB); mAdapter.stopLeScan(mLeScanCB);
} }
if (mState == CONNECTING) { if (mState == SCANNING) {
mState = DISCONNECTED; mState = DISCONNECTED;
if(this.mStateListener != null) { if(this.mStateListener != null) {
this.mStateListener.onChange(mState); this.mStateListener.onChange(mState);
@ -289,6 +292,9 @@ public class PrinterBle {
if (device == null) return; if (device == null) return;
mBtGatt = device.connectGatt(ctx, false, mBtGattCB); mBtGatt = device.connectGatt(ctx, false, mBtGattCB);
mState = CONNECTING; mState = CONNECTING;
if(this.mStateListener != null){
this.mStateListener.onChange(mState);
}
} }
public void setOnStateChangeListener(stateChangeListener listener) { public void setOnStateChangeListener(stateChangeListener listener) {
@ -366,12 +372,12 @@ public class PrinterBle {
dataQueue.clear(); dataQueue.clear();
} }
public void textTab() { public PrinterBle textTab() {
addText("\t"); return addText("\t");
} }
public void textNewLine() { public PrinterBle textNewLine() {
addText("\n"); return addText("\n");
} }
public PrinterBle addText(String txt) { public PrinterBle addText(String txt) {
@ -380,9 +386,9 @@ public class PrinterBle {
return this; return this;
} }
public void addTextln(String txt) { public PrinterBle addTextln(String txt) {
if (txt == null || txt.isEmpty()) return; if (txt == null || txt.isEmpty()) return this;
addText(txt + "\n"); return addText(txt + "\n");
} }
public PrinterBle setAlign(int align) { public PrinterBle setAlign(int align) {
@ -393,18 +399,28 @@ public class PrinterBle {
break; break;
case ALIGN_LEFT: case ALIGN_LEFT:
strAlign = "lt"; strAlign = "lt";
break;
case ALIGN_RIGHT: case ALIGN_RIGHT:
strAlign = "rt"; strAlign = "rt";
break;
default: default:
return this; return this;
} }
addText("__&a" + strAlign + "__"); this.lastAlign = align;
return this; return addText("__&a" + strAlign + "__");
} }
public void setSize(int w, int h) { public PrinterBle setSize(int w, int h) {
if (w < 1 || h < 1 || w > 3 || h > 3) return; if (w < 1 || h < 1 || w > 3 || h > 3) return this;
addText(String.format("__&s%d,%d__", w, h)); 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() { public void resetSize() {