From 74159a1b47a12ed9482ad8cf319aa9b40b3b1231 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 31 Aug 2017 17:11:15 +0800 Subject: [PATCH] update --- .../java/xyz/mtfos/btdemo/MainActivity.kt | 91 ++++++++-- .../java/xyz/mtfos/btdemo/PrinterBle.java | 16 +- app/src/main/res/layout/activity_main.xml | 156 ++++++++++++++++-- 3 files changed, 228 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt b/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt index 2cf35f3..c256f82 100644 --- a/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt +++ b/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt @@ -7,29 +7,46 @@ import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.os.Handler -import android.widget.Button -import android.widget.EditText -import kotlinx.android.synthetic.main.activity_main.* +import android.widget.* import xyz.mtfos.btdemo.objectTool.bind /** * Created by jay on 2017/8/9. */ class MainActivity : Activity() { + val mainLay: LinearLayout by bind(R.id.mainlay) + val intxt: EditText by bind(R.id.intxt) val btn: Button by bind(R.id.bt) val connectBtn: Button by bind(R.id.connect) val disconnectBtn: Button by bind(R.id.disconnect) var ble: PrinterBle? = null + val alignSpinner: Spinner by bind(R.id.align) + val sizeSpinner: Spinner by bind(R.id.size) + val alignBtn: Button by bind(R.id.btn_align) + val sizeBtn: Button by bind(R.id.btn_size) + + val addHR: Button by bind(R.id.addHR) + val addNL: Button by bind(R.id.addNL) + val addTAB: Button by bind(R.id.addTAB) + val addTXT: Button by bind(R.id.addTXT) + + val preview: TextView by bind(R.id.preview) + val uiHandler: Handler = Handler() var th: Thread? = null var thrun: Boolean = false var isInit: Boolean = false + val alignArr: Array = arrayOf("Left", "Center", "Right") + val sizeArr: Array = arrayOf("1,1", "1,2", "2,1", "2,2") + @SuppressLint("WrongConstant") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + mainLay.visibility = LinearLayout.GONE + btn.isEnabled = false connectBtn.isEnabled = true disconnectBtn.isEnabled = false @@ -45,18 +62,47 @@ class MainActivity : Activity() { btn.isEnabled = true connectBtn.isEnabled = false disconnectBtn.isEnabled = true + uiHandler.post { + mainLay.visibility = LinearLayout.VISIBLE + } } if (it == PrinterBle.DISCONNECTED) { btn.isEnabled = false connectBtn.isEnabled = true disconnectBtn.isEnabled = false + uiHandler.post { + mainLay.visibility = LinearLayout.GONE + } } } - ble?.setOnNotifyListener{ + ble?.setOnNotifyListener { System.out.println("Get Notify :::::: " + it) } + alignSpinner.adapter = ArrayAdapter(this@MainActivity, android.R.layout.simple_spinner_item, alignArr) + alignBtn.setOnClickListener { + val pos = alignSpinner.selectedItemPosition + when (pos) { + 0 -> ble?.setAlign(PrinterBle.ALIGN_LEFT) + 1 -> ble?.setAlign(PrinterBle.ALIGN_CENTER) + 2 -> ble?.setAlign(PrinterBle.ALIGN_RIGHT) + } + refreshPreview() + } + + sizeSpinner.adapter = ArrayAdapter(this@MainActivity, android.R.layout.simple_spinner_item, sizeArr) + sizeBtn.setOnClickListener { + val pos = sizeSpinner.selectedItemPosition + when (pos) { + 0 -> ble?.setSize(1, 1) + 1 -> ble?.setSize(1, 2) + 2 -> ble?.setSize(2, 1) + 3 -> ble?.setSize(2, 2) + } + refreshPreview() + } + var permissionCheck: Int = 0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { permissionCheck += this@MainActivity.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) @@ -66,17 +112,28 @@ class MainActivity : Activity() { } } - btn.setOnClickListener { - val txt: String = intxt.text.toString() - ble?.setSize(2,1) - ble?.setAlign(PrinterBle.ALIGN_CENTER) - ble?.addTextln("MTFoS Shop") - ble?.resetSize() - ble?.setAlign(PrinterBle.ALIGN_RIGHT) - ble?.addTextln("MTFoS Shop") + addHR.setOnClickListener { ble?.addHR() - ble?.setAlign(PrinterBle.ALIGN_LEFT) + refreshPreview() + } + + addNL.setOnClickListener { + ble?.textNewLine() + refreshPreview() + } + + addTAB.setOnClickListener { + ble?.textTab() + refreshPreview() + } + + addTXT.setOnClickListener { + val txt: String = intxt.text.toString() ble?.addText(txt) + refreshPreview() + } + + btn.setOnClickListener { ble?.sendQueue() } @@ -111,4 +168,12 @@ class MainActivity : Activity() { ble?.disconnect() } } + + fun refreshPreview() { + val data = ble?.dataQueue ?: return + preview.text = "" + for (str: String in data) { + preview.text = preview.text.toString() + "\n" + str + } + } } \ No newline at end of file diff --git a/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java b/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java index edeedc8..db4a24d 100644 --- a/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java +++ b/app/src/main/java/xyz/mtfos/btdemo/PrinterBle.java @@ -100,10 +100,10 @@ public class PrinterBle { super.onConnectionStateChange(gatt, status, newState); Log.d(TAG, "Connect State Change to " + status + " new State : " + newState); if (status == BluetoothGatt.GATT_SUCCESS) { - if(mState != DISCONNECTED) { + if (mState != DISCONNECTED) { mState = CONNECTED; Log.d(TAG, "Connected"); - if(!isConnected) { + if (!isConnected) { isConnected = true; mBtGatt.discoverServices(); } @@ -251,7 +251,7 @@ public class PrinterBle { } mState = SCANNING; - if(this.mStateListener != null) { + if (this.mStateListener != null) { this.mStateListener.onChange(mState); } } @@ -268,7 +268,7 @@ public class PrinterBle { if (mState == SCANNING) { mState = DISCONNECTED; - if(this.mStateListener != null) { + if (this.mStateListener != null) { this.mStateListener.onChange(mState); } } @@ -292,7 +292,7 @@ public class PrinterBle { if (device == null) return; mBtGatt = device.connectGatt(ctx, false, mBtGattCB); mState = CONNECTING; - if(this.mStateListener != null){ + if (this.mStateListener != null) { this.mStateListener.onChange(mState); } } @@ -415,7 +415,7 @@ public class PrinterBle { return addText(String.format("__&s%d,%d__", w, h)); } - public PrinterBle addHR(){ + public PrinterBle addHR() { int align = this.lastAlign; setAlign(ALIGN_CENTER); addTextln(StringUtils.repeat('-', 20)); @@ -426,4 +426,8 @@ public class PrinterBle { public void resetSize() { setSize(1, 1); } + + public ArrayList getDataQueue() { + return dataQueue; + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4621ea2..4e1b4e2 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,31 +8,155 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> +