From dca769861e3bfb1938fcabb5e17c4f65e657e97d Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 22 Aug 2017 22:35:36 +0800 Subject: [PATCH] update demo object --- .idea/modules.xml | 3 ++- .../java/xyz/mtfos/btdemo/MainActivity.kt | 17 +++++++++++++- .../java/xyz/mtfos/btdemo/PrinterBle.java | 23 ++++++++++++++----- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/.idea/modules.xml b/.idea/modules.xml index e23fdf9..a822a05 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,8 +2,9 @@ - + + \ No newline at end of file diff --git a/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt b/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt index 89d28d7..f4fafd1 100644 --- a/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt +++ b/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt @@ -9,6 +9,7 @@ import android.os.Bundle import android.os.Handler import android.widget.Button import android.widget.EditText +import kotlinx.android.synthetic.main.activity_main.* import xyz.mtfos.btdemo.objectTool.bind /** @@ -37,6 +38,19 @@ class MainActivity : Activity() { ble?.setOnStateChangeListener { System.out.println("Now State is >>>> " + it) + if(it == PrinterBle.CONNECTING) { + disconnectBtn.isEnabled = true + } + if(it == PrinterBle.CONNECTED) { + btn.isEnabled = true + connectBtn.isEnabled = false + disconnectBtn.isEnabled = true + } + if(it == PrinterBle.DISCONNECTED) { + btn.isEnabled = false + connectBtn.isEnabled = true + disconnectBtn.isEnabled = false + } } var permissionCheck: Int = 0 @@ -50,7 +64,8 @@ class MainActivity : Activity() { btn.setOnClickListener { val txt: String = intxt.text.toString() - + ble?.setAlign(PrinterBle.ALIGN_CENTER)?.addText(txt) + ble?.sendQueue() } connectBtn.setOnClickListener { diff --git a/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java b/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java index 1944086..76e3a0a 100644 --- a/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java +++ b/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java @@ -15,6 +15,7 @@ import android.bluetooth.le.ScanResult; import android.bluetooth.le.ScanSettings; import android.content.Context; import android.os.Build; +import android.os.Handler; import android.os.ParcelUuid; import android.util.Log; @@ -71,6 +72,8 @@ public class PrinterBle { private stateChangeListener mStateListener; private notifyListener mNotifyListener; + private Handler mHandler = new Handler(); + public interface stateChangeListener { void onChange(int state); } @@ -96,7 +99,12 @@ public class PrinterBle { mState = CONNECTED; Log.d(TAG, "Connected"); } - mStateListener.onChange(mState); + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + mStateListener.onChange(mState); + } + }, 100); if(mState == CONNECTED) { mBtGatt.discoverServices(); } @@ -137,6 +145,7 @@ public class PrinterBle { break; } sending = false; + runQueue(); } @Override @@ -283,7 +292,7 @@ public class PrinterBle { Log.d(TAG, String.format("setVal: %s / sendVal: %s", String.valueOf(setVal), String.valueOf(sendVal))); } - private void sendQueue() { + public void sendQueue() { if (sendingQueue == null) sendingQueue = new ArrayList<>(); if (dataQueue == null) dataQueue = new ArrayList<>(); if (dataQueue.size() == 0) return; @@ -350,9 +359,10 @@ public class PrinterBle { addText("\n"); } - public void addText(String txt) { - if (txt == null || txt.isEmpty()) return; + public PrinterBle addText(String txt) { + if (txt == null || txt.isEmpty()) return this; dataQueue.add(txt); + return this; } public void addTextln(String txt) { @@ -360,7 +370,7 @@ public class PrinterBle { addText(txt + "\n"); } - public void setAlign(int align) { + public PrinterBle setAlign(int align) { String strAlign = ""; switch (align) { case ALIGN_CENTER: @@ -371,9 +381,10 @@ public class PrinterBle { case ALIGN_RIGHT: strAlign = "rt"; default: - return; + return this; } addText("__a" + strAlign + "__"); + return this; } public void setSize(int w, int h) {